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

39 lines
901 B
TypeScript
Raw Normal View History

2020-08-31 08:41:53 +09:00
import { action, Action, createStore, createTypedHooks } from 'easy-peasy';
2020-03-26 20:22:06 +09:00
interface StatsModel {
2020-08-31 08:41:53 +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-31 08:41:53 +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-31 08:41:53 +09:00
state.totalCount += 1;
2020-03-26 20:22:06 +09:00
if (isAvailable) {
2020-08-31 08:41:53 +09:00
state.availableCount += 1;
2020-03-26 20:22:06 +09:00
}
}),
reset: action((state) => {
2020-08-31 08:41:53 +09:00
state.totalCount = 0;
state.availableCount = 0;
2020-03-26 20:22:06 +09:00
}),
2020-08-31 08:41:53 +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-31 08:41:53 +09:00
};
2020-03-26 20:22:06 +09:00
2020-08-31 08:41:53 +09:00
export const store = createStore(storeModel);
2020-03-27 00:49:23 +09:00
2020-08-31 08:41:53 +09:00
const typedHooks = createTypedHooks<StoreModel>();
export const useStoreActions = typedHooks.useStoreActions;
export const useStoreDispatch = typedHooks.useStoreDispatch;
export const useStoreState = typedHooks.useStoreState;