2020-08-20 00:57:33 +09:00
|
|
|
import { action, Action, createStore, createTypedHooks } from 'easy-peasy'
|
|
|
|
import { History } from 'history'
|
2020-03-26 20:22:06 +09:00
|
|
|
|
|
|
|
interface StatsModel {
|
2020-08-20 00:57:33 +09:00
|
|
|
availableCount: number
|
|
|
|
totalCount: number
|
|
|
|
add: Action<StatsModel, boolean>
|
|
|
|
reset: Action<StatsModel, void>
|
2020-03-26 20:22:06 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
interface StoreModel {
|
2020-08-20 00:57:33 +09:00
|
|
|
stats: StatsModel
|
2020-03-26 20:22:06 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
const statsModel: StatsModel = {
|
|
|
|
availableCount: 0,
|
|
|
|
totalCount: 0,
|
|
|
|
add: action((state, isAvailable) => {
|
2020-08-20 00:57:33 +09:00
|
|
|
state.totalCount += 1
|
2020-03-26 20:22:06 +09:00
|
|
|
if (isAvailable) {
|
2020-08-20 00:57:33 +09:00
|
|
|
state.availableCount += 1
|
2020-03-26 20:22:06 +09:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
reset: action((state) => {
|
2020-08-20 00:57:33 +09:00
|
|
|
state.totalCount = 0
|
|
|
|
state.availableCount = 0
|
2020-03-26 20:22:06 +09:00
|
|
|
}),
|
2020-08-20 00:57:33 +09:00
|
|
|
}
|
2020-03-26 20:22:06 +09:00
|
|
|
|
2020-03-27 00:49:23 +09:00
|
|
|
const storeModel: StoreModel = {
|
2020-03-26 20:22:06 +09:00
|
|
|
stats: statsModel,
|
2020-08-20 00:57:33 +09:00
|
|
|
}
|
2020-03-26 20:22:06 +09:00
|
|
|
|
2020-08-20 00:57:33 +09:00
|
|
|
export const store = createStore(storeModel)
|
2020-03-27 00:49:23 +09:00
|
|
|
|
|
|
|
export function wrapHistoryWithStoreHandler(history: History) {
|
|
|
|
history.listen(() => {
|
|
|
|
// reset stats counter
|
2020-08-20 00:57:33 +09:00
|
|
|
store.getActions().stats.reset()
|
|
|
|
})
|
|
|
|
return history
|
2020-03-27 00:49:23 +09:00
|
|
|
}
|
|
|
|
|
2020-08-20 00:57:33 +09:00
|
|
|
const typedHooks = createTypedHooks<StoreModel>()
|
|
|
|
export const useStoreActions = typedHooks.useStoreActions
|
|
|
|
export const useStoreDispatch = typedHooks.useStoreDispatch
|
|
|
|
export const useStoreState = typedHooks.useStoreState
|