123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- import { Action } from '@ngrx/store';
- import { User } from 'src/app/shared/models/user/user.model';
- import { MsgState } from '../msg.state';
- import { UserActionTypes } from './user.action.types';
- import { UserState } from './user.state';
- import { UserActionUnion } from './users.actions';
-
- /** create reducer,
- *
- * a reducer need a initial state and an action to perform*/
-
- const initialUserState: UserState = {
- usersList: [],
- currentUser: null,
- msgState: MsgState.INIT_STATE,
- serverMsgError: 'No error message'
- };
-
- export function userReducer(
- state: UserState = initialUserState,
- action: Action
- ): UserState {
-
- switch (action.type) {
-
- // create a user form action to add a suer
- case UserActionTypes.CREATE_FORM_ADD_USER:
- return {
- ...state,
- msgState: MsgState.FORM_LOADING
- };
-
- case UserActionTypes.CREATE_FORM_ADD_USER_SUCCESS:
- return {
- ...state,
- msgState: MsgState.FORM_LOADED
- };
-
- case UserActionTypes.CREATE_FORM_ADD_USER_ERROR:
- return {
- ...state,
- msgState: MsgState.FORM_LOAD_ERR,
- serverMsgError: (<UserActionUnion>action).payload
- };
-
- // save a user (data of form) action
- case UserActionTypes.SAVE_USERFORM_ADD:
- return {
- ...state,
- msgState: MsgState.ADD
- };
-
- case UserActionTypes.SAVE_USERFORM_ADD_SUCCESS:
- let createdUser: User = (<UserActionUnion>action).payload;
- let usersListCopy1: User[] = [...state.usersList];
- usersListCopy1.push(createdUser);
-
- return {
- ...state,
- msgState: MsgState.ADDED,
- usersList: usersListCopy1
- };
-
- case UserActionTypes.SAVE_USERFORM_ADD_ERROR:
- return {
- ...state,
- msgState: MsgState.ERR,
- serverMsgError: (<UserActionUnion>action).payload
- };
-
- // get all users actions
- case UserActionTypes.GET_ALL_USERS:
- return { ...state, msgState: MsgState.LOADING };
-
- case UserActionTypes.GET_ALL_USERS_SUCCESS:
- return {
- ...state,
- msgState: MsgState.LOADED,
- usersList: (<UserActionUnion>action).payload,
- };
-
- case UserActionTypes.GET_ALL_USERS_ERROR:
- return {
- ...state,
- msgState: MsgState.ERR,
- serverMsgError: (<UserActionUnion>action).payload
- };
-
- // create a edit user form actions
- case UserActionTypes.CREATE_FORM_USER_EDIT:
- return {
- ...state,
- msgState: MsgState.FORM_LOADING,
- };
-
- case UserActionTypes.CREATE_FORM_USER_EDIT_SUCCESS:
- let modifiedUser: User = (<UserActionUnion>action).payload;
- return {
- ...state,
- msgState: MsgState.FORM_LOADED,
- currentUser: modifiedUser
- };
-
- case UserActionTypes.CREATE_FORM_USER_EDIT_ERROR:
- return {
- ...state,
- msgState: MsgState.ERR,
- serverMsgError: (<UserActionUnion>action).payload
- };
-
- // update user with user form data
-
- case UserActionTypes.UPDATE_USER_FORM:
- return {
- ...state,
- msgState: MsgState.UPDATE
- };
-
- case UserActionTypes.UPDATE_USER_FORM_SUCCESS:
- let updatedUser: User = (<UserActionUnion>action).payload;
- let usersListCopy2: User[] = [...state.usersList];
- usersListCopy2 = usersListCopy2.map((user) =>
- user.id == updatedUser.id ? updatedUser : user
- );
-
- return {
- ...state,
- usersList: usersListCopy2,
- msgState: MsgState.UPDATED
- };
-
- case UserActionTypes.UPDATE_USER_FORM_ERROR:
- return {
- ...state,
- msgState: MsgState.ERR,
- serverMsgError: (<UserActionUnion>action).payload
- };
-
- // delete a user actions
- case UserActionTypes.DELETE_A_USER:
- return {
- ...state,
- msgState: MsgState.LOADING
- };
-
- case UserActionTypes.DELETE_A_USER_SUCCESS:
- let deletedUser: User = (<UserActionUnion>action).payload;
- let deletedUserIndex = state.usersList.indexOf(deletedUser);
- let usersListCopy3: User[] = [...state.usersList];
- usersListCopy3.splice(deletedUserIndex, 1);
- return {
- ...state,
- msgState: MsgState.LOADED,
- usersList: usersListCopy3
- };
-
- case UserActionTypes.DELETE_A_USER_ERROR:
- return {
- ...state,
- msgState: MsgState.ERR,
- serverMsgError: (<UserActionUnion>action).payload
- };
-
- //load user's project actions
-
- case UserActionTypes.GET_USER_S_PROJECTS:
- return{
- ... state,
- msgState: MsgState.LOADING
- };
-
- case UserActionTypes.GET_USER_S_PROJECTS_SUCCESS:
- return{
- ...state,
- msgState: MsgState.LOADED,
- usersList: (<UserActionUnion> action).payload
- };
-
- case UserActionTypes.GET_USER_S_PROJECTS_ERROR:
- return{
- ...state,
- msgState: MsgState.ERR,
- serverMsgError: (<UserActionUnion> action).payload
- };
-
- case UserActionTypes.NAVIGATE_TO_USER_S_PROJECTS_COMPONENT_SUCCESS:
- return{
- ...state,
- msgState: MsgState.UPDATE
- };
-
- default:
- return { ...state };
- }
- }
|