🚨 Try to make TS linter happy

This commit is contained in:
2023-01-30 22:07:20 +01:00
parent 27f895ce0c
commit 2fd6783534
10 changed files with 242 additions and 152 deletions

View File

@@ -1,28 +1,37 @@
import { createContext, createSignal } from 'solid-js';
import { createContext, createSignal, JSX } from 'solid-js';
import { createStore } from 'solid-js/store';
import { Passages, Stops } from './types';
import { Line, Lines, Passage, Passages, Stop, Stops } from './types';
interface Store {
export interface BusinessDataStore {
getLine: (lineId: string) => Promise<Line>;
getLinePassages: (lineId: string) => Record<string, Passage[]>;
passages: () => Passages;
getLinePassages?: (lineId: string) => Passages;
addPassages?: (passages: Passages) => void;
clearPassages?: () => void;
refreshPassages: (stopId: number) => Promise<void>;
addPassages: (passages: Passages) => void;
clearPassages: () => void;
stops: () => Stops;
addStops?: (stops: Stops) => void;
getStop: (stopId: number) => Stop | undefined;
searchStopByName: (name: string) => Promise<Stops>;
};
export const BusinessDataContext = createContext<Store>();
export const BusinessDataContext = createContext<BusinessDataStore>();
export function BusinessDataProvider(props: { children: JSX.Element }) {
const [serverUrl, setServerUrl] = createSignal<string>("https://localhost:4443");
const [serverUrl] = createSignal<string>("https://localhost:4443");
const [store, setStore] = createStore({ lines: {}, passages: {}, stops: {} });
type Store = {
lines: Lines;
passages: Passages;
stops: Stops;
};
const getLine: Line = async (lineId: string) => {
const [store, setStore] = createStore<Store>({ lines: {}, passages: {}, stops: {} });
const getLine = async (lineId: string): Promise<Line> => {
let line = store.lines[lineId];
if (line === undefined) {
console.log(`${lineId} not found... fetch it from backend.`);
@@ -35,15 +44,15 @@ export function BusinessDataProvider(props: { children: JSX.Element }) {
return line;
}
const getLinePassages = (lineId: string) => {
const getLinePassages = (lineId: string): Record<string, Passage[]> => {
return store.passages[lineId];
};
const passages = () => {
const passages = (): Passages => {
return store.passages;
}
const refreshPassages = async (stopId: number) => {
const refreshPassages = async (stopId: number): Promise<void> => {
const httpOptions = { headers: { "Content-Type": "application/json" } };
console.log(`Fetching data for ${stopId}`);
const data = await fetch(`${serverUrl()}/stop/nextPassages/${stopId}`, httpOptions);
@@ -51,31 +60,30 @@ export function BusinessDataProvider(props: { children: JSX.Element }) {
addPassages(response.passages);
}
const addPassages = (passages) => {
setStore((s) => {
setStore('passages', passages);
});
const addPassages = (passages: Passages): void => {
setStore('passages', passages);
}
const clearPassages = () => {
setStore((s) => {
const clearPassages = (): void => {
setStore((s: Store): Store => {
for (const lineId of Object.keys(s.passages)) {
setStore('passages', lineId, undefined);
}
return s;
});
}
const getStop = (stopId: int) => {
const getStop = (stopId: number): Stop | undefined => {
return store.stops[stopId];
}
const searchStopByName = async (name: string) => {
const searchStopByName = async (name: string): Promise<Stops> => {
const data = await fetch(`${serverUrl()}/stop/?name=${name}`, {
headers: { 'Content-Type': 'application/json' }
});
const stops = await data.json();
const byIdStops = {};
const byIdStops: Stops = {};
for (const stop of stops) {
byIdStops[stop.id] = stop;
setStore('stops', stop.id, stop);
@@ -85,10 +93,22 @@ export function BusinessDataProvider(props: { children: JSX.Element }) {
return (
<BusinessDataContext.Provider value={{
getLine, getLinePassages, passages, refreshPassages, clearPassages,
getStop, searchStopByName
getLine, getLinePassages, passages, refreshPassages, addPassages, clearPassages, getStop, searchStopByName
}}>
{props.children}
</BusinessDataContext.Provider>
);
}
export interface BusinessDataStore {
getLine: (lineId: string) => Promise<Line>;
getLinePassages: (lineId: string) => Record<string, Passage[]>;
passages: () => Passages;
refreshPassages: (stopId: number) => Promise<void>;
addPassages: (passages: Passages) => void;
clearPassages: () => void;
getStop: (stopId: number) => Stop | undefined;
searchStopByName: (name: string) => Promise<Stops>;
};