19 refactor store methods #20
@ -2,27 +2,20 @@ import { createStore } from "kaioken"
|
|||||||
|
|
||||||
export const useStationsStore = createStore(
|
export const useStationsStore = createStore(
|
||||||
null as Station[] | null,
|
null as Station[] | null,
|
||||||
(set) => ({
|
(set, get) => ({
|
||||||
add: (station: Station): Station[] => {
|
add: (station: Station): Station[] => {
|
||||||
let newState: Station[] | null = null
|
let newState: Station[] = [...(get() ?? []), station]
|
||||||
set((state) => {
|
set(newState)
|
||||||
newState = [...(state ?? []), station]
|
|
||||||
return newState
|
|
||||||
})
|
|
||||||
//@ts-ignore
|
|
||||||
return newState
|
return newState
|
||||||
},
|
},
|
||||||
rmStation: (stationId): Station[] => {
|
rmStation: (stationId): Station[] => {
|
||||||
let newState: Station[] | null = null
|
let newState: Station[] | null =
|
||||||
set((state) => {
|
get()?.filter((station) => station.id !== stationId) ?? []
|
||||||
newState = state?.filter((station) => station.id !== stationId) ?? []
|
set(newState)
|
||||||
return newState
|
|
||||||
})
|
|
||||||
//@ts-ignore
|
|
||||||
return newState
|
return newState
|
||||||
},
|
},
|
||||||
override: (stationsList: Station[]) => {
|
override: (stationsList: Station[]) => {
|
||||||
set((_state) => stationsList)
|
set(stationsList)
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user