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: (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 = (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: (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: (action).payload, }; case UserActionTypes.GET_ALL_USERS_ERROR: return { ...state, msgState: MsgState.ERR, serverMsgError: (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 = (action).payload; return { ...state, msgState: MsgState.FORM_LOADED, currentUser: modifiedUser }; case UserActionTypes.CREATE_FORM_USER_EDIT_ERROR: return { ...state, msgState: MsgState.ERR, serverMsgError: (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 = (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: (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 = (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: (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: ( action).payload }; case UserActionTypes.GET_USER_S_PROJECTS_ERROR: return{ ...state, msgState: MsgState.ERR, serverMsgError: ( action).payload }; case UserActionTypes.NAVIGATE_TO_USER_S_PROJECTS_COMPONENT_SUCCESS: return{ ...state, msgState: MsgState.UPDATE }; default: return { ...state }; } }