Migrate actions,reducers and action types to TS #15
This commit is contained in:
13
app/reducers/users/IUserAction.ts
Normal file
13
app/reducers/users/IUserAction.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import {UserActionType} from 'constants/userActionType'
|
||||
|
||||
/**
|
||||
* User action interface
|
||||
*
|
||||
* @export
|
||||
* @interface IUserAction
|
||||
*/
|
||||
export interface IUserAction {
|
||||
payload: any,
|
||||
type: UserActionType
|
||||
|
||||
}
|
||||
33
app/reducers/users/UserState.ts
Normal file
33
app/reducers/users/UserState.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { User,Profile } from "domain/users";
|
||||
|
||||
/**
|
||||
* User state
|
||||
*
|
||||
* @export
|
||||
* @class UserState
|
||||
*/
|
||||
export class UserState {
|
||||
/**
|
||||
* The list of users information
|
||||
*
|
||||
* @type {({[userId: string]: Profile} | null)}
|
||||
* @memberof UserState
|
||||
*/
|
||||
info: {[userId: string]: Profile} = {};
|
||||
|
||||
/**
|
||||
* If users profile are loaded
|
||||
*
|
||||
* @type {Boolean}
|
||||
* @memberof UserState
|
||||
*/
|
||||
loaded: Boolean = false;
|
||||
|
||||
/**
|
||||
* If edit profile is open {true} or not {false}
|
||||
*
|
||||
* @type {Boolean}
|
||||
* @memberof UserState
|
||||
*/
|
||||
openEditProfile: Boolean = false;
|
||||
}
|
||||
3
app/reducers/users/index.ts
Normal file
3
app/reducers/users/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import { userReducer } from "./userReducer";
|
||||
|
||||
export {userReducer};
|
||||
78
app/reducers/users/userReducer.ts
Normal file
78
app/reducers/users/userReducer.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
// - Import action types
|
||||
import {UserActionType} from 'constants/userActionType'
|
||||
|
||||
// - Import domain
|
||||
import { User,Profile } from "domain/users";
|
||||
|
||||
import { UserState } from "./UserState";
|
||||
import { IUserAction } from "./IUserAction";
|
||||
|
||||
/**
|
||||
* User reducer
|
||||
*/
|
||||
export var userReducer = (state: UserState = new UserState(), action: IUserAction) => {
|
||||
const { payload } = action
|
||||
switch (action.type) {
|
||||
case UserActionType.USER_INFO:
|
||||
return {
|
||||
...state,
|
||||
info: {
|
||||
...state.info,
|
||||
[payload.uid]: {
|
||||
...payload.info
|
||||
}
|
||||
}
|
||||
}
|
||||
case UserActionType.ADD_USER_INFO:
|
||||
return {
|
||||
...state,
|
||||
info: {
|
||||
...state.info,
|
||||
[payload.uid]: {
|
||||
...payload.info
|
||||
}
|
||||
},
|
||||
loaded: true
|
||||
}
|
||||
case UserActionType.ADD_PEOPLE_INFO:
|
||||
return {
|
||||
...state,
|
||||
info: {
|
||||
...state.info,
|
||||
...payload
|
||||
}
|
||||
}
|
||||
|
||||
case UserActionType.UPDATE_USER_INFO:
|
||||
return {
|
||||
...state,
|
||||
info: {
|
||||
...state.info,
|
||||
[payload.uid]: {
|
||||
...state.info![payload.uid],
|
||||
...payload.info
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
case UserActionType.CLEAR_ALL_DATA_USER:
|
||||
return new UserState();
|
||||
|
||||
case UserActionType.CLOSE_EDIT_PROFILE:
|
||||
return {
|
||||
...state,
|
||||
openEditProfile: false
|
||||
}
|
||||
|
||||
case UserActionType.OPEN_EDIT_PROFILE:
|
||||
return {
|
||||
...state,
|
||||
openEditProfile: true
|
||||
}
|
||||
|
||||
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user