Files
bodyshop/client/src/redux/application/application.reducer.js
2020-09-09 11:57:58 -07:00

87 lines
2.1 KiB
JavaScript

import ApplicationActionTypes from "./application.types";
const INITIAL_STATE = {
loading: false,
breadcrumbs: [],
recentItems: [],
selectedHeader: "home",
scheduleLoad: {
load: {},
calculating: false,
error: null,
},
};
const applicationReducer = (state = INITIAL_STATE, action) => {
switch (action.type) {
case ApplicationActionTypes.SET_SELECTED_HEADER:
return {
...state,
selectedHeader: action.payload,
};
case ApplicationActionTypes.ADD_RECENT_ITEM:
return {
...state,
recentItems: updateRecentItemsArray(state, action.payload),
};
case ApplicationActionTypes.SET_BREAD_CRUMBS:
return {
...state,
breadcrumbs: action.payload,
};
case ApplicationActionTypes.CALCULATE_SCHEDULE_LOAD:
return {
...state,
scheduleLoad: { ...state.scheduleLoad, calculating: true, error: null },
};
case ApplicationActionTypes.CALCULATE_SCHEDULE_LOAD_SUCCESS:
return {
...state,
scheduleLoad: {
...state.scheduleLoad,
load: action.payload,
calculating: false,
},
};
case ApplicationActionTypes.CALCULATE_SCHEDULE_LOAD_FAILURE:
return {
...state,
scheduleLoad: {
...state.scheduleLoad,
calculating: false,
error: action.payload,
},
};
case ApplicationActionTypes.START_LOADING:
return {
...state,
loading: true,
};
case ApplicationActionTypes.END_LOADING:
return {
...state,
loading: false,
};
default:
return state;
}
};
export default applicationReducer;
const updateRecentItemsArray = (state, newItem) => {
//Check to see if the new item is in the list.
const matchingIndex = state.recentItems.findIndex((i) => i.id === newItem.id);
if (matchingIndex >= 0) {
return [
newItem,
...state.recentItems.slice(0, matchingIndex),
...state.recentItems.slice(matchingIndex + 1, 9),
];
} else {
return [newItem, ...state.recentItems.slice(0, 9)];
}
};