🎉 First commit !!!
This commit is contained in:
75
frontend/src/search.tsx
Normal file
75
frontend/src/search.tsx
Normal file
@@ -0,0 +1,75 @@
|
||||
import { batch, createContext, createSignal } from 'solid-js';
|
||||
import { createStore } from 'solid-js/store';
|
||||
|
||||
import { Stop, Stops } from './types';
|
||||
|
||||
|
||||
interface Store {
|
||||
getMarkers: () => Markers;
|
||||
addMarkers?: (stopId, markers) => void;
|
||||
setMarkers?: (markers) => void;
|
||||
|
||||
getStops: () => Stops;
|
||||
setStops?: (stops) => void;
|
||||
removeStops?: (stopIds) => void;
|
||||
|
||||
getDisplayedStop: () => Stop;
|
||||
setDisplayedStop: (stop: Stop) => void;
|
||||
};
|
||||
|
||||
export const SearchContext = createContext<Store>();
|
||||
|
||||
export function SearchProvider(props: { children: JSX.Element }) {
|
||||
|
||||
const [store, setStore] = createStore({stops: {}, markers: {}, displayedStop: []});
|
||||
|
||||
const getStops = () => {
|
||||
return store.stops;
|
||||
};
|
||||
|
||||
const setStops = (stops) => {
|
||||
setStore((s) => {
|
||||
setStore('stops', stops);
|
||||
});
|
||||
};
|
||||
|
||||
const removeStops = (stopIds) => {
|
||||
batch(() => {
|
||||
for(const stopId of stopIds) {
|
||||
setStore('stops', stopId, undefined);
|
||||
setStore('markers', stopId, undefined);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const getMarkers = () => {
|
||||
return store.markers;
|
||||
};
|
||||
|
||||
const addMarkers = (stopId, markers) => {
|
||||
setStore('markers', stopId, 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>
|
||||
);
|
||||
}
|
Reference in New Issue
Block a user