From 6f8618d5230baa8f4c543cc483bd6796d2a4160e Mon Sep 17 00:00:00 2001 From: Triston Date: Wed, 3 Apr 2024 20:01:52 -0500 Subject: [PATCH] feat(stationstores.ts): refactor station stores to use the get function fix #19 --- src/hooks/stationStores.ts | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/src/hooks/stationStores.ts b/src/hooks/stationStores.ts index a2fe568..4139704 100644 --- a/src/hooks/stationStores.ts +++ b/src/hooks/stationStores.ts @@ -2,27 +2,20 @@ import { createStore } from "kaioken" export const useStationsStore = createStore( null as Station[] | null, - (set) => ({ + (set, get) => ({ add: (station: Station): Station[] => { - let newState: Station[] | null = null - set((state) => { - newState = [...(state ?? []), station] - return newState - }) - //@ts-ignore + let newState: Station[] = [...(get() ?? []), station] + set(newState) return newState }, rmStation: (stationId): Station[] => { - let newState: Station[] | null = null - set((state) => { - newState = state?.filter((station) => station.id !== stationId) ?? [] - return newState - }) - //@ts-ignore + let newState: Station[] | null = + get()?.filter((station) => station.id !== stationId) ?? [] + set(newState) return newState }, override: (stationsList: Station[]) => { - set((_state) => stationsList) + set(stationsList) }, }) )