123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- import { ProjectActionUnion } from './projects.actions';
- import { Action } from '@ngrx/store';
- import { MsgState } from '../msg.state';
- import { ProjectState } from './project.state';
- import { ProjectActionTypes } from './project.action.types';
- import { Project } from 'src/app/shared/models/project/project.model';
-
- /** a reducer need first initial state and action to perform */
-
- const projectInitState: ProjectState = {
- currentProject: null,
- projectsList: [],
- msgState: MsgState.INIT_STATE,
- serverErrorMsg: 'no error massage'
- };
-
- export function projectReducer(
- state: ProjectState = projectInitState,
- projectAction: Action
- ): ProjectState {
- switch (projectAction.type) {
- // get all projects action reducer
- case ProjectActionTypes.GET_ALL_PROJECTS:
- return {
- ...state,
- msgState: MsgState.LOADING,
- };
-
- case ProjectActionTypes.GET_ALL_PROJECTS_SUCCESS:
- return {
- ...state,
- msgState: MsgState.LOADED,
- projectsList: (<ProjectActionUnion>projectAction).payload,
- };
-
- case ProjectActionTypes.GET_ALL_PROJECTS_ERROR:
- return {
- ...state,
- msgState: MsgState.ERR,
- serverErrorMsg: (<ProjectActionUnion>projectAction).payload,
- };
-
- // create project form to add action reducer
-
- case ProjectActionTypes.CREATE_PROJECT_FORM_ADD:
- return {
- ...state,
- msgState: MsgState.FORM_LOADING,
- };
-
- case ProjectActionTypes.CREATE_PROJECT_FORM_ADD_SUCCESS:
- return {
- ...state,
- msgState: MsgState.FORM_LOADED,
- };
-
- case ProjectActionTypes.CREATE_PROJECT_FORM_ADD_ERROR:
- return {
- ...state,
- msgState: MsgState.ERR,
- serverErrorMsg: (<ProjectActionUnion>projectAction).payload,
- };
-
- //saving project form data
-
- case ProjectActionTypes.SAVE_PROJECT_FORM_DATA:
- return {
- ...state,
- msgState: MsgState.LOADING,
- };
-
- case ProjectActionTypes.CREATE_PROJECT_FORM_ADD_SUCCESS:
- let createdProject: Project = (<ProjectActionUnion>projectAction).payload;
- let projectsListCopy1: Project[] = [...state.projectsList];
- projectsListCopy1.push(createdProject);
-
- return {
- ...state,
- msgState: MsgState.ADDED,
- projectsList: projectsListCopy1,
- };
-
- // create form to update project
- case ProjectActionTypes.CREATE_PROJECT_FORM_UPDATE:
- return {
- ...state,
- msgState: MsgState.FORM_LOADING,
- };
-
- case ProjectActionTypes.CREATE_PROJECT_FORM_UPDATE_SUCCESS:
- let projectToUpdate: Project = (<ProjectActionUnion>projectAction)
- .payload;
- return {
- ...state,
- msgState: MsgState.FORM_LOADED,
- currentProject: projectToUpdate,
- };
-
- case ProjectActionTypes.CREATE_PROJECT_FORM_UPDATE_ERROR:
- return {
- ...state,
- msgState: MsgState.FORM_LOAD_ERR,
- serverErrorMsg: (<ProjectActionUnion>projectAction).payload,
- };
-
- //update project with form data
-
- case ProjectActionTypes.UPDATE_PROJECT_WITH_FORM_DATA:
- return {
- ...state,
- msgState: MsgState.DEFAULT_MSG,
- };
-
- case ProjectActionTypes.UPDATE_PROJECT_WITH_FORM_DATA_SUCCESS:
- let updatedProject: Project = (<ProjectActionUnion>projectAction).payload;
- let projectsListCopy2 = [...state.projectsList];
- projectsListCopy2 = projectsListCopy2.map((project) =>
- project.id == updatedProject.id ? updatedProject : project
- );
-
- return{
- ...state,
- msgState: MsgState.UPDATED,
- projectsList : projectsListCopy2
- }
-
- // delete project
- case ProjectActionTypes.DELETE_PROJECT:
- return {
- ...state,
- msgState: MsgState.DELETE
- }
-
- case ProjectActionTypes.DELETE_PROJECT_SUCCESS:
- let deletedProject = (<ProjectActionUnion> projectAction).payload;
- let deletedProjectIndex = state.projectsList.indexOf(deletedProject);
- let projectsListCopy3 : Project[] = [...state.projectsList];
- projectsListCopy3 = projectsListCopy3.splice(deletedProjectIndex);
-
- return{
- ...state,
- msgState: MsgState.DELETED,
- projectsList: projectsListCopy3
- };
-
- case ProjectActionTypes.DELETE_PROJECT_ERROR:
- return{
- ...state,
- msgState: MsgState.ERR,
- serverErrorMsg: (<ProjectActionUnion>projectAction).payload
- };
-
- default:
- return { ...state };
- }
- }
|