diff options
| author | Ariel Costas Guerrero <94913521+arielcostas@users.noreply.github.com> | 2024-09-09 18:48:45 +0200 |
|---|---|---|
| committer | Ariel Costas Guerrero <94913521+arielcostas@users.noreply.github.com> | 2024-09-09 18:48:57 +0200 |
| commit | 9925249bf489ae960189f6daabe59263d1620c89 (patch) | |
| tree | da04f135ee1e724491c7cc1c5d128f285a83713e /src/data/StopDataProvider.ts | |
| parent | 663a2a3ff10bf55498a6c8731b1a32e9f98d7343 (diff) | |
Favourite stops, local stop list
Diffstat (limited to 'src/data/StopDataProvider.ts')
| -rw-r--r-- | src/data/StopDataProvider.ts | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/src/data/StopDataProvider.ts b/src/data/StopDataProvider.ts new file mode 100644 index 0000000..620a659 --- /dev/null +++ b/src/data/StopDataProvider.ts @@ -0,0 +1,65 @@ +import stops from './stops.json'; + +export interface CachedStopList { + timestamp: number; + data: Stop[]; +} + +export interface Stop { + stopId: number + name: string; + latitude?: number; + longitude?: number; + lines: string[]; + favourite?: boolean; +} + +export class StopDataProvider { + async getStops(): Promise<Stop[]> { + const rawFavouriteStops = localStorage.getItem('favouriteStops'); + let favouriteStops: number[] = []; + if (rawFavouriteStops) { + favouriteStops = JSON.parse(rawFavouriteStops) as number[]; + } + + return stops.map((stop: Stop) => { + return { + ...stop, + favourite: favouriteStops.includes(stop.stopId) + }; + }); + } + + addFavourite(stopId: number) { + const rawFavouriteStops = localStorage.getItem('favouriteStops'); + let favouriteStops: number[] = []; + if (rawFavouriteStops) { + favouriteStops = JSON.parse(rawFavouriteStops) as number[]; + } + + if (!favouriteStops.includes(stopId)) { + favouriteStops.push(stopId); + localStorage.setItem('favouriteStops', JSON.stringify(favouriteStops)); + } + } + + removeFavourite(stopId: number) { + const rawFavouriteStops = localStorage.getItem('favouriteStops'); + let favouriteStops: number[] = []; + if (rawFavouriteStops) { + favouriteStops = JSON.parse(rawFavouriteStops) as number[]; + } + + const newFavouriteStops = favouriteStops.filter(id => id !== stopId); + localStorage.setItem('favouriteStops', JSON.stringify(newFavouriteStops)); + } + + isFavourite(stopId: number): boolean { + const rawFavouriteStops = localStorage.getItem('favouriteStops'); + if (rawFavouriteStops) { + const favouriteStops = JSON.parse(rawFavouriteStops) as number[]; + return favouriteStops.includes(stopId); + } + return false; + } +}
\ No newline at end of file |
