1
0
mirror of https://github.com/uetchy/namae.git synced 2025-07-03 06:40:04 +09:00
namae/src/store.tsx

48 lines
1.1 KiB
TypeScript
Raw Normal View History

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