🚚 Remove business logic from Component instances
This commit is contained in:
@@ -94,7 +94,7 @@ const Map: Component = (props) => {
|
||||
|
||||
const mapCenter = [48.853, 2.35];
|
||||
|
||||
const { addMarkers, getStops } = useContext(SearchContext);
|
||||
const { addMarkers } = useContext(SearchContext);
|
||||
|
||||
let mapDiv: any;
|
||||
let map = null;
|
||||
@@ -147,40 +147,24 @@ const Map: Component = (props) => {
|
||||
|
||||
export const StopsManager: Component = (props) => {
|
||||
|
||||
const [minCharactersNb, setMinCharactersNb] = createSignal<int>(4);
|
||||
const [_inProgress, _setInProgress] = createSignal<bool>(false);
|
||||
export const StopsManager: Component = () => {
|
||||
|
||||
const { serverUrl } = useContext(BusinessDataContext);
|
||||
const { getStops, removeStops, setStops, setDisplayedStop } = useContext(SearchContext);
|
||||
const [minCharactersNb, setMinCharactersNb] = createSignal<number>(4);
|
||||
const [inProgress, setInProgress] = createSignal<boolean>(false);
|
||||
const [foundStops, setFoundStops] = createSignal<Array<number>>([]);
|
||||
|
||||
async function _fetchStopByName(name) {
|
||||
const data = await fetch(`${serverUrl()}/stop/?name=${name}`, {
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
});
|
||||
const stops = await data.json();
|
||||
const stopIds = stops.map((stop) => stop.id);
|
||||
const { getStop, searchStopByName } = useContext(BusinessDataContext);
|
||||
const { setDisplayedStops } = useContext(SearchContext);
|
||||
|
||||
const stopIdsToRemove = Object.keys(getStops()).filter(stopId => !(stopId in stopIds));
|
||||
|
||||
const byIdStops = {};
|
||||
for (const stop of stops) {
|
||||
byIdStops[stop.id] = stop;
|
||||
}
|
||||
|
||||
batch(() => {
|
||||
removeStops(stopIdsToRemove);
|
||||
setStops(byIdStops);
|
||||
});
|
||||
}
|
||||
|
||||
async function _onStopNameInput(event) {
|
||||
const onStopNameInput = async (event) => {
|
||||
/* TODO: Add a tempo before fetching stop for giving time to user to finish his request */
|
||||
const stopName = event.target.value;
|
||||
if (stopName.length >= minCharactersNb()) {
|
||||
console.log(`Fetching data for ${stopName}`);
|
||||
_setInProgress(true);
|
||||
await _fetchStopByName(stopName);
|
||||
_setInProgress(false);
|
||||
setInProgress(true);
|
||||
const stopsById = await searchStopByName(stopName);
|
||||
setFoundStops(Object.values(stopsById));
|
||||
setInProgress(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user