Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions src/App.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
padding: 0;
margin: 0;
display: grid;
grid-template-columns: 25rem auto;
grid-template-columns: 25rem auto auto;
grid-template-rows: 100%;
justify-content: space-between;
}
.map {
grid-column: 1 / span 2;
grid-column: 1 / span 3;
grid-row: 1 / span 1;
}

Expand All @@ -22,20 +23,39 @@
}

.sidebarContent {
/* TODO: move the layout into the component themselves*/
display: flex;
flex-direction: column;
max-height: calc(100% - 2 * 0.5rem);

margin: 0.5rem;
background-color: white;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
border-radius: 0.1rem;

pointer-events: all; /* we want all the content to behave normally*/
}

.mapOptions {
grid-column: 3 / span 1;
grid-row: 1 / span 1;
background-color: transparent;
z-index: 400;
}

.mapOptionsContent {
display: flex;
margin: 0.5rem;
background-color: white;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
border-radius: 0.1rem;

pointer-events: all; /* we want all the content to behave normally*/
}

@media screen and (max-width: 44rem) {
.appWrapper {
grid-template-columns: 100%;
grid-template-columns: 1fr auto;
grid-template-rows: 100%;
}

Expand All @@ -45,10 +65,20 @@

.sidebar {
grid-row: 1 / span 1;
grid-column: 1 / span 2;
}

.mapOptions {
grid-row: 1 / span 1;
grid-column: 2 / span 1;
/* this has to be solved differently when small screen layouts are attempted in #44 */
align-self: end;
margin-bottom: 2rem;
}

.sidebarContent {
margin: 0;
max-height: 100%;
border-radius: 0;
}
}
13 changes: 12 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,38 @@
import React, { useEffect, useState } from 'react'
import Sidebar from '@/sidebar/Sidebar'
import styles from './App.module.css'
import { getApiInfoStore, getErrorStore, getQueryStore, getRouteStore } from '@/stores/Stores'
import { getApiInfoStore, getErrorStore, getMapOptionsStore, getQueryStore, getRouteStore } from '@/stores/Stores'
import MapComponent from '@/map/Map'
import { Bbox } from '@/api/graphhopper'
import MapOptions from '@/map/MapOptions'

export default function App() {
const [query, setQuery] = useState(getQueryStore().state)
const [info, setInfo] = useState(getApiInfoStore().state)
const [route, setRoute] = useState(getRouteStore().state)
const [error, setError] = useState(getErrorStore().state)
const [mapOptions, setMapOptions] = useState(getMapOptionsStore().state)
const [useInfoBbox, setUseInfoBbox] = useState(true)

useEffect(() => {
const onQueryChanged = () => setQuery(getQueryStore().state)
const onInfoChanged = () => setInfo(getApiInfoStore().state)
const onRouteChanged = () => setRoute(getRouteStore().state)
const onErrorChanged = () => setError(getErrorStore().state)
const onMapOptionsChanged = () => setMapOptions(getMapOptionsStore().state)

getQueryStore().register(onQueryChanged)
getApiInfoStore().register(onInfoChanged)
getRouteStore().register(onRouteChanged)
getErrorStore().register(onErrorChanged)
getMapOptionsStore().register(onMapOptionsChanged)

return () => {
getQueryStore().deregister(onQueryChanged)
getApiInfoStore().deregister(onInfoChanged)
getRouteStore().deregister(onRouteChanged)
getErrorStore().deregister(onErrorChanged)
getMapOptionsStore().deregister(onMapOptionsChanged)
}
})

Expand All @@ -50,13 +55,19 @@ export default function App() {
paths={route.routingResult.paths}
selectedPath={route.selectedPath}
bbox={bbox}
mapStyle={mapOptions.selectedStyle}
/>
</div>
<div className={styles.sidebar}>
<div className={styles.sidebarContent}>
<Sidebar info={info} query={query} route={route} error={error} />
</div>
</div>
<div className={styles.mapOptions}>
<div className={styles.mapOptionsContent}>
<MapOptions {...mapOptions} />
</div>
</div>
</div>
)
}
11 changes: 11 additions & 0 deletions src/actions/Actions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Action } from '@/stores/Dispatcher'
import { Coordinate, QueryPoint } from '@/stores/QueryStore'
import { ApiInfo, Path, RoutingArgs, RoutingResult, RoutingVehicle } from '@/api/graphhopper'
import { StyleOption } from '@/stores/MapOptionsStore'

export class InfoReceived implements Action {
readonly result: ApiInfo
Expand Down Expand Up @@ -87,3 +88,13 @@ export class SetSelectedPath implements Action {
}

export class DismissLastError implements Action {}

export class SelectMapStyle implements Action {
readonly styleOption: StyleOption

constructor(styleOption: StyleOption) {
this.styleOption = styleOption
}
}

export class MapIsLoaded implements Action {}
12 changes: 11 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@ import React from 'react'
import ReactDOM from 'react-dom'

import App from '@/App'
import { getApiInfoStore, getErrorStore, getQueryStore, getRouteStore, setStores } from '@/stores/Stores'
import {
getApiInfoStore,
getErrorStore,
getMapOptionsStore,
getQueryStore,
getRouteStore,
setStores,
} from '@/stores/Stores'
import Dispatcher from '@/stores/Dispatcher'
import RouteStore from '@/stores/RouteStore'
import ApiInfoStore from '@/stores/ApiInfoStore'
import { createUrl, parseUrl } from '@/./QueryUrl'
import QueryStore from '@/stores/QueryStore'
import { ApiImpl } from '@/api/Api'
import ErrorStore from '@/stores/ErrorStore'
import MapOptionsStore from '@/stores/MapOptionsStore'

// set up state management
const api = new ApiImpl()
Expand All @@ -19,13 +27,15 @@ setStores({
routeStore: new RouteStore(queryStore),
infoStore: new ApiInfoStore(),
errorStore: new ErrorStore(),
mapOptionsStore: new MapOptionsStore(),
})

// register stores at dispatcher to receive actions
Dispatcher.register(getQueryStore())
Dispatcher.register(getRouteStore())
Dispatcher.register(getApiInfoStore())
Dispatcher.register(getErrorStore())
Dispatcher.register(getMapOptionsStore())

api.infoWithDispatch() // get infos about the api as soon as possible

Expand Down
13 changes: 10 additions & 3 deletions src/map/Map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ import React, { useEffect, useRef, useState } from 'react'
import styles from '@/map/Map.module.css'
import Mapbox from '@/map/Mapbox'
import Dispatcher from '@/stores/Dispatcher'
import { ClearPoints, SetPoint } from '@/actions/Actions'
import { ClearPoints, MapIsLoaded, SetPoint } from '@/actions/Actions'
import { Bbox, Path } from '@/api/graphhopper'
import { StyleOption } from '@/stores/MapOptionsStore'

type MapProps = {
selectedPath: Path
paths: Path[]
queryPoints: QueryPoint[]
bbox: Bbox
mapStyle: StyleOption
}

export default function ({ selectedPath, paths, queryPoints, bbox }: MapProps) {
export default function ({ selectedPath, paths, queryPoints, bbox, mapStyle }: MapProps) {
const mapContainerRef: React.RefObject<HTMLDivElement> = useRef(null)
const queryPointsRef = useRef(queryPoints)
const [map, setMap] = useState<Mapbox | null>(null)
Expand All @@ -25,10 +27,14 @@ export default function ({ selectedPath, paths, queryPoints, bbox }: MapProps) {
})

useEffect(() => {
if (map) map.remove()

const mapWrapper = new Mapbox(
mapContainerRef.current!,
mapStyle,
() => {
setMap(mapWrapper)
Dispatcher.dispatch(new MapIsLoaded())
},
e => {
let point = queryPointsRef.current.find(point => !point.isInitialized)
Expand All @@ -46,8 +52,9 @@ export default function ({ selectedPath, paths, queryPoints, bbox }: MapProps) {
)
}
)
mapWrapper.fitBounds(bbox)
return () => map?.remove()
}, [])
}, [mapStyle])
useEffect(() => map?.drawPaths(paths, selectedPath), [paths, selectedPath, map])
useEffect(() => map?.showPathDetails(selectedPath), [selectedPath, map])
useEffect(() => map?.drawMarkers(queryPoints), [queryPoints, map])
Expand Down
27 changes: 27 additions & 0 deletions src/map/MapOptions.modules.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
.mapOptionsContainer {
display: flex;
}

.options {
display: flex;
flex-direction: column;
margin: 0.5rem;
}

.option {
display: flex;
flex-direction: row;
gap: 0.5rem;
align-items: center;
}

.layerButton {
width: 1.5rem;
height: 1.5rem;
margin: 0.7rem;
}

.layerButton svg {
width: 100%;
display: block;
}
64 changes: 64 additions & 0 deletions src/map/MapOptions.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import React, { useState } from 'react'
import styles from './MapOptions.modules.css'
import { MapOptionsStoreState, StyleOption } from '@/stores/MapOptionsStore'
import Dispatcher from '@/stores/Dispatcher'
import { SelectMapStyle } from '@/actions/Actions'
import PlainButton from '@/PlainButton'
import LayerImg from './layer-group-solid.svg'

export interface MapOptionsProps {}

export default function (props: MapOptionsStoreState) {
const [isOpen, setIsOpen] = useState(false)
return (
<div
className={styles.mapOptionsContainer}
onMouseEnter={() => setIsOpen(true)}
onMouseLeave={() => setIsOpen(false)}
>
{isOpen ? (
<Options storeState={props} notifyChanged={() => setIsOpen(false)} />
) : (
<PlainButton className={styles.layerButton} onClick={() => setIsOpen(true)}>
<LayerImg />
</PlainButton>
)}
</div>
)
}

interface OptionsProps {
storeState: MapOptionsStoreState
notifyChanged: { (): void }
}
const Options = function ({ storeState, notifyChanged }: OptionsProps) {
return (
<div
className={styles.options}
onChange={e => {
notifyChanged()
onChange(e.target as HTMLInputElement, storeState.styleOptions)
}}
>
{storeState.styleOptions.map(option => (
<div className={styles.option} key={option.name}>
<input
type="radio"
id={option.name}
name="layer"
value={option.name}
defaultChecked={option === storeState.selectedStyle}
disabled={!storeState.isMapLoaded}
/>
<label htmlFor={option.name}>{option.name}</label>
</div>
))}
</div>
)
}

function onChange(target: HTMLInputElement, options: StyleOption[]) {
const option = options.find(option => option.name === target.value)

if (option) Dispatcher.dispatch(new SelectMapStyle(option))
}
Loading