import { createAsyncThunk , createSlice } from "@reduxjs/toolkit" ; import { QueryStatus } from "@reduxjs/toolkit/query" ; import { AssetEnablement , PickleModelJson , } from "picklefinance-core/lib/model/PickleModelJson" ; import { RootState } from "." ; const apiHost = process . env . apiHost ; export const fetchCore = createAsyncThunk < PickleModelJson > ( "core/fetch" , async ( ) => { const response = await fetch ( `${ apiHost }/protocol/pfcore` ) ; return await response . json ( ) ; } ) ; interface CoreState { data : PickleModelJson | undefined ; loading : keyof typeof QueryStatus ; } const initialState = { data : undefined , loading : "uninitialized" } ; const coreSlice = createSlice ( { name : "core" , initialState , reducers : { } , extraReducers : ( builder ) => { builder . addCase ( fetchCore . pending , ( state ) => { state . loading = "pending" ; } ) ; builder . addCase ( fetchCore . fulfilled , ( state , action ) => { state . data = action . payload ; state . loading = "fulfilled" ; } ) ; } , } ) ; /**
 * Selectors
 */ const selectCore = ( state ) => state . core . data ; const selectEnabledJars = ( state ) => { if ( state . core . data === undefined ) return [ ] ; return state . core . data . assets . jars . filter ( ( jar ) => jar . enablement === AssetEnablement . ENABLED ) ; } ; const selectLoadingState = ( state ) => state . core . loading ; const selectTimestamp = ( state ) => state . core . data ?. timestamp ; export const CoreSelectors = { selectCore , selectEnabledJars , selectLoadingState , selectTimestamp , } ; export default coreSlice . reducer ;