import * as storage from '../../lib/storage' ; import type { ReduxState } from '../index' ; type Dispatch < A extends Action > = ( action : A | Promise < A > | ThunkAction < A > ) => void ; type GetState = ( ) => ReduxState ; type ThunkAction < A extends Action > = ( dispatch : Dispatch < A > , getState : GetState ) => void ; type Action = ToggleFavoriteAction | LoadFavoritesAction ; const LOAD_FAVORITE_BUILDINGS = 'buildings/LOAD_FAVORITE_BUILDINGS' ; const TOGGLE_FAVORITE_BUILDING = 'buildings/TOGGLE_FAVORITE_BUILDING' ; type LoadFavoritesAction = { type : 'buildings/LOAD_FAVORITE_BUILDINGS' ; payload : Array < string > ; } ; export async function loadFavoriteBuildings ( ) { const favoriteBuildings = await storage . getFavoriteBuildings ( ) ; return { type : LOAD_FAVORITE_BUILDINGS , payload : favoriteBuildings } ; } type ToggleFavoriteAction = { type : 'buildings/TOGGLE_FAVORITE_BUILDING' ; payload : Array < string > ; } ; export function toggleFavoriteBuilding ( buildingName ) { return ( dispatch , getState ) => { const state = getState ( ) ; const currentFavorites = state . buildings ? state . buildings . favorites : [ ] ; const newFavorites = currentFavorites . includes ( buildingName ) ? currentFavorites . filter ( ( name ) => name !== buildingName ) : [ ... currentFavorites , buildingName ] ; newFavorites . sort ( ( a , b ) => a . localeCompare ( b ) ) ; storage . setFavoriteBuildings ( newFavorites ) ; dispatch ( { type : TOGGLE_FAVORITE_BUILDING , payload : newFavorites } ) ; } ; } export type State = { favorites : Array < string > ; } ; const initialState = { favorites : [ ] , } ; export function buildings ( state = initialState , action ) { switch ( action . type ) { case LOAD_FAVORITE_BUILDINGS : case TOGGLE_FAVORITE_BUILDING : { return { ... state , favorites : action . payload } ; } default : { return state ; } } }