🚚 Remove business logic from Component instances
This commit is contained in:
@@ -21,55 +21,42 @@ export const SearchContext = createContext<Store>();
|
||||
|
||||
export function SearchProvider(props: { children: JSX.Element }) {
|
||||
|
||||
const [store, setStore] = createStore({stops: {}, markers: {}, displayedStop: []});
|
||||
const [store, setStore] = createStore({ stops: {}, markers: {}, displayedStops: [] });
|
||||
|
||||
const getStops = () => {
|
||||
return store.stops;
|
||||
};
|
||||
const getDisplayedStops = () => {
|
||||
return store.displayedStops;
|
||||
}
|
||||
|
||||
const setStops = (stops) => {
|
||||
setStore((s) => {
|
||||
setStore('stops', stops);
|
||||
});
|
||||
};
|
||||
const setDisplayedStops = (stops: Array<Stop>) => {
|
||||
setStore((s) => {
|
||||
setStore('displayedStops', stops);
|
||||
});
|
||||
}
|
||||
|
||||
const removeStops = (stopIds) => {
|
||||
batch(() => {
|
||||
for(const stopId of stopIds) {
|
||||
setStore('stops', stopId, undefined);
|
||||
setStore('markers', stopId, undefined);
|
||||
}
|
||||
});
|
||||
};
|
||||
const removeStops = (stopIds: Array<number>) => {
|
||||
batch(() => {
|
||||
for (const stopId of stopIds) {
|
||||
setStore('stops', stopId, undefined);
|
||||
setStore('markers', stopId, undefined);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const getMarkers = () => {
|
||||
return store.markers;
|
||||
};
|
||||
const getMarkers = () => {
|
||||
return store.markers;
|
||||
}
|
||||
|
||||
const addMarkers = (stopId, markers) => {
|
||||
setStore('markers', stopId, markers);
|
||||
};
|
||||
const addMarkers = (stopId: number, markers) => {
|
||||
setStore('markers', stopId, markers);
|
||||
}
|
||||
|
||||
const setMarkers = (markers) => {
|
||||
setStore('markers', markers);
|
||||
};
|
||||
const setMarkers = (markers) => {
|
||||
setStore('markers', markers);
|
||||
}
|
||||
|
||||
const getDisplayedStop = () => {
|
||||
/* console.log(store.displayedStop); */
|
||||
return store.displayedStop;
|
||||
};
|
||||
const setDisplayedStop = (stop: Stop) => {
|
||||
/* console.log(stop); */
|
||||
setStore((s) => {
|
||||
console.log("s.displayedStop=", s.displayedStop);
|
||||
setStore('displayedStop', [stop]);
|
||||
});
|
||||
/* console.log(store.displayedStop); */
|
||||
};
|
||||
|
||||
return (
|
||||
<SearchContext.Provider value={{addMarkers, getMarkers, setMarkers, getStops, removeStops, setStops, getDisplayedStop, setDisplayedStop}}>
|
||||
{props.children}
|
||||
</SearchContext.Provider>
|
||||
);
|
||||
return (
|
||||
<SearchContext.Provider value={{ getDisplayedStops, setDisplayedStops, removeStops, getMarkers, addMarkers, setMarkers }}>
|
||||
{props.children}
|
||||
</SearchContext.Provider>
|
||||
);
|
||||
}
|
||||
|
Reference in New Issue
Block a user