Files
digital-pilates/store/index.ts
richarjiang 970a4b8568 feat(challenges): 新增 ChallengeProgressCard 组件并接入喝水挑战进度上报
- 抽离进度卡片为独立组件,支持主题色自定义与复用
- 挑战列表页顶部展示进行中的挑战进度
- 喝水记录自动上报至关联的水挑战
- 移除旧版 challengeSlice 与冗余进度样式
- 统一使用 value 字段上报进度,兼容多类型挑战
2025-09-29 15:14:59 +08:00

73 lines
2.7 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { configureStore, createListenerMiddleware } from '@reduxjs/toolkit';
import challengesReducer from './challengesSlice';
import checkinReducer, { addExercise, autoSyncCheckin, removeExercise, replaceExercises, setNote, toggleExerciseCompleted } from './checkinSlice';
import circumferenceReducer from './circumferenceSlice';
import exerciseLibraryReducer from './exerciseLibrarySlice';
import foodLibraryReducer from './foodLibrarySlice';
import foodRecognitionReducer from './foodRecognitionSlice';
import goalsReducer from './goalsSlice';
import healthReducer from './healthSlice';
import moodReducer from './moodSlice';
import nutritionReducer from './nutritionSlice';
import scheduleExerciseReducer from './scheduleExerciseSlice';
import tasksReducer from './tasksSlice';
import trainingPlanReducer from './trainingPlanSlice';
import userReducer from './userSlice';
import waterReducer from './waterSlice';
import workoutReducer from './workoutSlice';
// 创建监听器中间件来处理自动同步
const listenerMiddleware = createListenerMiddleware();
// 监听所有数据变动的 actions触发自动同步
const syncActions = [addExercise, removeExercise, replaceExercises, toggleExerciseCompleted, setNote];
syncActions.forEach(action => {
listenerMiddleware.startListening({
actionCreator: action,
effect: async (action, listenerApi) => {
const state = listenerApi.getState() as any;
const date = action.payload?.date;
if (!date) return;
// 延迟一下,避免在同一事件循环中重复触发
await new Promise(resolve => setTimeout(resolve, 100));
// 检查是否还有待同步的日期
const currentState = listenerApi.getState() as any;
const pendingSyncDates = currentState?.checkin?.pendingSyncDates || [];
if (pendingSyncDates.includes(date)) {
listenerApi.dispatch(autoSyncCheckin({ date }));
}
},
});
});
export const store = configureStore({
reducer: {
user: userReducer,
challenges: challengesReducer,
checkin: checkinReducer,
circumference: circumferenceReducer,
goals: goalsReducer,
health: healthReducer,
mood: moodReducer,
nutrition: nutritionReducer,
tasks: tasksReducer,
trainingPlan: trainingPlanReducer,
scheduleExercise: scheduleExerciseReducer,
exerciseLibrary: exerciseLibraryReducer,
foodLibrary: foodLibraryReducer,
foodRecognition: foodRecognitionReducer,
workout: workoutReducer,
water: waterReducer,
},
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware().prepend(listenerMiddleware.middleware),
});
export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;