feat: first commit
This commit is contained in:
10
src/store/actions/app.ts
Normal file
10
src/store/actions/app.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import { RoleType, PageType } from '@/constants/app';
|
||||
import { LocationInfo } from '@/types/location';
|
||||
|
||||
import { CHANGE_ROLE_TYPE, CHANGE_HOME_PAGE, SET_LOCATION_INFO } from '../constants';
|
||||
|
||||
export const changeRoleType = (value: RoleType) => ({ type: CHANGE_ROLE_TYPE, value });
|
||||
|
||||
export const changeHomePage = (value: PageType) => ({ type: CHANGE_HOME_PAGE, value });
|
||||
|
||||
export const setLocationInfo = (value: LocationInfo) => ({ type: SET_LOCATION_INFO, value });
|
||||
3
src/store/actions/index.ts
Normal file
3
src/store/actions/index.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export * from './app';
|
||||
export * from './user';
|
||||
export * from './message';
|
||||
5
src/store/actions/message.ts
Normal file
5
src/store/actions/message.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import { UserMessage } from '@/types/message';
|
||||
|
||||
import { SET_USER_MESSAGE } from '../constants';
|
||||
|
||||
export const setMessageInfo = (value: Partial<UserMessage>) => ({ type: SET_USER_MESSAGE, value });
|
||||
7
src/store/actions/user.ts
Normal file
7
src/store/actions/user.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import { UserInfo } from '@/types/user';
|
||||
|
||||
import { SET_USER_INFO, SET_BIND_PHONE } from '../constants';
|
||||
|
||||
export const setUserInfo = (value: Partial<UserInfo>) => ({ type: SET_USER_INFO, value });
|
||||
|
||||
export const setBindPhone = (value: UserInfo['isBindPhone']) => ({ type: SET_BIND_PHONE, value });
|
||||
6
src/store/constants.ts
Normal file
6
src/store/constants.ts
Normal file
@ -0,0 +1,6 @@
|
||||
export const CHANGE_HOME_PAGE = 'CHANGE_HOME_PAGE';
|
||||
export const CHANGE_ROLE_TYPE = 'CHANGE_ROLE_TYPE';
|
||||
export const SET_LOCATION_INFO = 'SET_LOCATION_INFO';
|
||||
export const SET_USER_INFO = 'SET_USER_INFO';
|
||||
export const SET_BIND_PHONE = 'SET_BIND_PHONE';
|
||||
export const SET_USER_MESSAGE = 'SET_USER_MESSAGE';
|
||||
30
src/store/index.ts
Normal file
30
src/store/index.ts
Normal file
@ -0,0 +1,30 @@
|
||||
import { createStore, applyMiddleware, compose, Middleware, Store } from 'redux';
|
||||
import { thunk } from 'redux-thunk';
|
||||
|
||||
import { IState } from '@/types/store';
|
||||
import { isDev } from '@/utils/common';
|
||||
|
||||
import reducers from './reducers';
|
||||
|
||||
const composeEnhancers =
|
||||
typeof window === 'object' && (window as BL.Anything).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
|
||||
? (window as BL.Anything).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
|
||||
// Specify extension’s options like name, actionsBlacklist, actionsCreators, serialize...
|
||||
})
|
||||
: compose;
|
||||
|
||||
const middlewares: Middleware[] = [thunk];
|
||||
|
||||
if (isDev()) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
middlewares.push(require('redux-logger').createLogger());
|
||||
}
|
||||
|
||||
const enhancer = composeEnhancers(applyMiddleware(...middlewares));
|
||||
|
||||
function configStore() {
|
||||
const store = createStore(reducers, enhancer);
|
||||
return store as Store<IState>;
|
||||
}
|
||||
|
||||
export default configStore();
|
||||
44
src/store/reducers/app.ts
Normal file
44
src/store/reducers/app.ts
Normal file
@ -0,0 +1,44 @@
|
||||
import Taro from '@tarojs/taro';
|
||||
|
||||
import { Action } from 'redux';
|
||||
|
||||
import { RoleType, PageType } from '@/constants/app';
|
||||
import { CacheKey } from '@/constants/cache-key';
|
||||
import { LocationInfo } from '@/types/location';
|
||||
import { AppState } from '@/types/store';
|
||||
|
||||
import { CHANGE_ROLE_TYPE, CHANGE_HOME_PAGE, SET_LOCATION_INFO } from '../constants';
|
||||
|
||||
const DEFAULT_LOCATION: LocationInfo = {
|
||||
provinceCode: '440000',
|
||||
provinceDesc: '广东省',
|
||||
cityCode: '440100',
|
||||
cityDesc: '广州市',
|
||||
latitude: 23.125178,
|
||||
longitude: 113.280637,
|
||||
};
|
||||
|
||||
const defaultAppMode = Taro.getStorageSync<RoleType>(CacheKey.APP_MODE_NEW) || RoleType.Anchor;
|
||||
const INIT_STATE: AppState = {
|
||||
roleType: defaultAppMode,
|
||||
homePageType: defaultAppMode === RoleType.Anchor ? PageType.JOB : PageType.Anchor,
|
||||
location: Taro.getStorageSync<LocationInfo>(CacheKey.CACHE_LOCATION_INFO) || DEFAULT_LOCATION,
|
||||
};
|
||||
|
||||
const appState = (state: AppState = INIT_STATE, action: Action): AppState => {
|
||||
const { type, value } = action as BL.Anything;
|
||||
switch (type) {
|
||||
case CHANGE_ROLE_TYPE:
|
||||
Taro.setStorageSync(CacheKey.APP_MODE_NEW, value);
|
||||
return { ...state, roleType: value };
|
||||
case CHANGE_HOME_PAGE:
|
||||
return { ...state, homePageType: value };
|
||||
case SET_LOCATION_INFO:
|
||||
Taro.setStorageSync(CacheKey.CACHE_LOCATION_INFO, value);
|
||||
return { ...state, location: value };
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
};
|
||||
|
||||
export default appState;
|
||||
11
src/store/reducers/index.ts
Normal file
11
src/store/reducers/index.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { combineReducers } from 'redux';
|
||||
|
||||
import appState from './app';
|
||||
import message from './message';
|
||||
import userInfo from './user';
|
||||
|
||||
export default combineReducers({
|
||||
appState,
|
||||
userInfo,
|
||||
message,
|
||||
});
|
||||
22
src/store/reducers/message.ts
Normal file
22
src/store/reducers/message.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { Action } from 'redux';
|
||||
|
||||
import { SET_USER_MESSAGE } from '@/store/constants';
|
||||
import { UserMessage } from '@/types/message';
|
||||
|
||||
const INIT_STATE: Partial<UserMessage> = {
|
||||
count: 0,
|
||||
};
|
||||
|
||||
const message = (state: Partial<UserMessage> = INIT_STATE, action: Action): Partial<UserMessage> => {
|
||||
const { type, value } = action as BL.Anything;
|
||||
switch (type) {
|
||||
case SET_USER_MESSAGE: {
|
||||
const newValue: Partial<UserMessage> = { ...state, ...value };
|
||||
return newValue;
|
||||
}
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
};
|
||||
|
||||
export default message;
|
||||
25
src/store/reducers/user.ts
Normal file
25
src/store/reducers/user.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import { Action } from 'redux';
|
||||
|
||||
import { UserInfo } from '@/types/user';
|
||||
|
||||
import { SET_USER_INFO, SET_BIND_PHONE } from '../constants';
|
||||
|
||||
const INIT_STATE: Partial<UserInfo> = {};
|
||||
|
||||
const userInfo = (state: Partial<UserInfo> = INIT_STATE, action: Action): Partial<UserInfo> => {
|
||||
const { type, value } = action as BL.Anything;
|
||||
switch (type) {
|
||||
case SET_USER_INFO: {
|
||||
const newUserInfo: Partial<UserInfo> = { ...state, ...value };
|
||||
return newUserInfo;
|
||||
}
|
||||
case SET_BIND_PHONE: {
|
||||
const newUserInfo: Partial<UserInfo> = { ...state, isBindPhone: value };
|
||||
return newUserInfo;
|
||||
}
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
};
|
||||
|
||||
export default userInfo;
|
||||
7
src/store/selector/app.ts
Normal file
7
src/store/selector/app.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import { IState } from '@/types/store';
|
||||
|
||||
export const selectRoleType = (state: IState) => state.appState.roleType;
|
||||
|
||||
export const selectHomePageType = (state: IState) => state.appState.homePageType;
|
||||
|
||||
export const selectLocation = (state: IState) => state.appState.location;
|
||||
3
src/store/selector/index.ts
Normal file
3
src/store/selector/index.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export * from './app';
|
||||
export * from './user';
|
||||
export * from './message';
|
||||
3
src/store/selector/message.ts
Normal file
3
src/store/selector/message.ts
Normal file
@ -0,0 +1,3 @@
|
||||
import { IState } from '@/types/store';
|
||||
|
||||
export const selectMessage = (state: IState) => state.message;
|
||||
3
src/store/selector/user.ts
Normal file
3
src/store/selector/user.ts
Normal file
@ -0,0 +1,3 @@
|
||||
import { IState } from '@/types/store';
|
||||
|
||||
export const selectUserInfo = (state: IState) => state.userInfo;
|
||||
Reference in New Issue
Block a user