58 lines
1.6 KiB
TypeScript
58 lines
1.6 KiB
TypeScript
import { createSlice, createSelector } from "@reduxjs/toolkit";
|
|
import type { PayloadAction } from "@reduxjs/toolkit";
|
|
|
|
export enum EUserDeviceType {
|
|
ios,
|
|
android,
|
|
}
|
|
|
|
interface IUserConfig {
|
|
deviceType: EUserDeviceType;
|
|
isShowTryApp: boolean;
|
|
isForceShortPath: boolean;
|
|
}
|
|
|
|
const initialState: IUserConfig = {
|
|
deviceType: EUserDeviceType.ios,
|
|
isShowTryApp: false,
|
|
isForceShortPath: false,
|
|
};
|
|
|
|
const userConfigSlice = createSlice({
|
|
name: "userConfig",
|
|
initialState,
|
|
reducers: {
|
|
update(state, action: PayloadAction<Partial<IUserConfig>>) {
|
|
return { ...initialState, ...state, ...action.payload };
|
|
},
|
|
addDeviceType(state, action: PayloadAction<EUserDeviceType>) {
|
|
state.deviceType = action.payload;
|
|
return state;
|
|
},
|
|
addIsShowTryApp(state, action: PayloadAction<boolean>) {
|
|
state.isShowTryApp = action.payload;
|
|
return state;
|
|
},
|
|
addIsForceShortPath(state, action: PayloadAction<boolean>) {
|
|
state.isForceShortPath = action.payload;
|
|
return state;
|
|
},
|
|
},
|
|
extraReducers: (builder) => builder.addCase("reset", () => initialState),
|
|
});
|
|
|
|
export const { actions } = userConfigSlice;
|
|
export const selectUserDeviceType = createSelector(
|
|
(state: { userConfig: IUserConfig }) => state.userConfig.deviceType,
|
|
(userConfig) => userConfig
|
|
);
|
|
export const selectIsShowTryApp = createSelector(
|
|
(state: { userConfig: IUserConfig }) => state.userConfig.isShowTryApp,
|
|
(userConfig) => userConfig
|
|
);
|
|
export const selectIsForceShortPath = createSelector(
|
|
(state: { userConfig: IUserConfig }) => state.userConfig.isForceShortPath,
|
|
(userConfig) => userConfig
|
|
);
|
|
export default userConfigSlice.reducer;
|