-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(map): improve map drawing synchronization
Details: * use optimisticResponses and only rely on apollo cache for features * improve selectedFeature state sync so that newly added features are always selected
- Loading branch information
1 parent
40e4e77
commit 31c46e8
Showing
4 changed files
with
566 additions
and
508 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,46 @@ | ||
import React, { createContext, useReducer } from "react"; | ||
import { LayersAction, layersReducer, selectedFeatureReducer, activeLayerReducer, drawReducer } from "./reducer"; | ||
import { Layer } from "types/layer"; | ||
import MapboxDraw from "@mapbox/mapbox-gl-draw"; | ||
|
||
import React, { createContext, useReducer } from 'react'; | ||
import { LayersAction, layersReducer, selectedFeatureReducer, activeLayerReducer, drawReducer } from './reducer'; | ||
import { Layer } from 'types/layer'; | ||
import MapboxDraw from '@mapbox/mapbox-gl-draw'; | ||
|
||
export type SelectedFeatureState = string | number | undefined; | ||
export type SelectedFeatureState = string | undefined; | ||
export type LayersState = Layer[]; | ||
export type ActiveLayerState = string | undefined; | ||
export type DrawState = MapboxDraw | undefined; | ||
|
||
|
||
export type LayerState = { | ||
layers: LayersState; | ||
activeLayer: string | undefined | ||
selectedFeature: SelectedFeatureState; | ||
draw: DrawState; | ||
} | ||
layers: LayersState; | ||
activeLayer: string | undefined; | ||
selectedFeature: SelectedFeatureState; | ||
draw: DrawState; | ||
}; | ||
|
||
const initialState: LayerState = { | ||
layers: [], | ||
activeLayer: undefined, | ||
selectedFeature: 0, | ||
draw: undefined, | ||
} | ||
layers: [], | ||
activeLayer: undefined, | ||
selectedFeature: undefined, | ||
draw: undefined, | ||
}; | ||
|
||
const LayerContext = createContext<{ | ||
state: LayerState; | ||
dispatch: React.Dispatch<LayersAction>; | ||
state: LayerState; | ||
dispatch: React.Dispatch<LayersAction>; | ||
}>({ | ||
state: initialState, | ||
dispatch: () => null, | ||
state: initialState, | ||
dispatch: () => null, | ||
}); | ||
|
||
const mainReducer = ({ layers, activeLayer, selectedFeature, draw }: LayerState, action: LayersAction) => ({ | ||
layers: layersReducer(layers, action), | ||
activeLayer: activeLayerReducer(activeLayer, action), | ||
selectedFeature: selectedFeatureReducer(selectedFeature, action), | ||
draw: drawReducer(draw, action), | ||
layers: layersReducer(layers, action), | ||
activeLayer: activeLayerReducer(activeLayer, action), | ||
selectedFeature: selectedFeatureReducer(selectedFeature, action), | ||
draw: drawReducer(draw, action), | ||
}); | ||
|
||
const LayersProvider = ({ children }: { children: React.ReactNode }) => { | ||
const [state, dispatch] = useReducer(mainReducer, initialState); | ||
const [state, dispatch] = useReducer(mainReducer, initialState); | ||
|
||
return ( | ||
<LayerContext.Provider value={{ state, dispatch }}> | ||
{children} | ||
</LayerContext.Provider> | ||
) | ||
} | ||
return <LayerContext.Provider value={{ state, dispatch }}>{children}</LayerContext.Provider>; | ||
}; | ||
|
||
export { LayerContext, LayersProvider }; | ||
export { LayerContext, LayersProvider }; |
Oops, something went wrong.