refactor(store): 重构store模块,分离全局设置与用户信息,修改项目中的使用方法
parent
8f2980f07f
commit
053449121b
@ -0,0 +1,25 @@
|
||||
import { createSlice } from '@reduxjs/toolkit';
|
||||
import defaultSettings from '../settings.json';
|
||||
|
||||
export interface GlobalState {
|
||||
settings?: typeof defaultSettings;
|
||||
}
|
||||
|
||||
const initialState: GlobalState = {
|
||||
settings: defaultSettings
|
||||
};
|
||||
|
||||
const globalSlice = createSlice({
|
||||
name: 'global',
|
||||
initialState,
|
||||
reducers: {
|
||||
updateSettings(state, action) {
|
||||
state.settings = action.payload.settings;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
export const { updateSettings } = globalSlice.actions;
|
||||
|
||||
export default globalSlice.reducer;
|
||||
|
||||
@ -0,0 +1,36 @@
|
||||
import { createSlice } from '@reduxjs/toolkit';
|
||||
|
||||
export interface UserState {
|
||||
userInfo?: {
|
||||
name?: string;
|
||||
avatar?: string;
|
||||
job?: string;
|
||||
organization?: string;
|
||||
location?: string;
|
||||
email?: string;
|
||||
permissions: Record<string, string[]>;
|
||||
};
|
||||
userLoading?: boolean;
|
||||
}
|
||||
|
||||
const initialState: UserState = {
|
||||
userInfo: {
|
||||
permissions: {}
|
||||
},
|
||||
userLoading: false
|
||||
};
|
||||
|
||||
const userSlice = createSlice({
|
||||
name: 'user',
|
||||
initialState,
|
||||
reducers: {
|
||||
updateUserInfo(state, action) {
|
||||
state.userInfo = action.payload.userInfo || initialState.userInfo;
|
||||
state.userLoading = action.payload.userLoading;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
export const { updateUserInfo } = userSlice.actions;
|
||||
|
||||
export default userSlice.reducer;
|
||||
Loading…
Reference in New Issue