{"size": 6588, "ext": "js", "max_stars_repo_path": "dashboard/src/reducers/incomingRequest.js", "max_stars_repo_name": "augustinebest/app", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "dashboard/src/reducers/incomingRequest.js", "max_issues_repo_name": "augustinebest/app", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "dashboard/src/reducers/incomingRequest.js", "max_forks_repo_name": "augustinebest/app", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 30.2201834862, "max_line_length": 84, "alphanum_fraction": 0.4342744384}
import * as types from '../constants/incomingRequest';

const initialState = {
    incomingRequests: {
        requesting: false,
        success: false,
        error: null,
        incomingRequests: [],
        count: 0,
        skip: 0,
        limit: 10,
    },
    updateIncomingRequest: {
        requesting: false,
        success: false,
        error: null,
        incomingRequest: null,
    },
    deleteIncomingRequest: {
        requesting: false,
        success: false,
        error: null,
    },
    createIncomingRequest: {
        requesting: false,
        success: false,
        error: null,
        incomingRequest: null,
    },
    activeIncomingRequest: '',
};

export default function incomingRequest(state = initialState, action) {
    switch (action.type) {
        case types.FETCH_ALL_INCOMING_REQUEST_REQUEST:
            return {
                ...state,
                incomingRequests: {
                    requesting: true,
                    success: false,
                    error: null,
                },
            };

        case types.FETCH_ALL_INCOMING_REQUEST_SUCCESS:
            return {
                ...state,
                incomingRequests: {
                    requesting: false,
                    success: true,
                    error: null,
                    incomingRequests: action.payload.data,
                    skip: action.payload.skip,
                    limit: action.payload.limit,
                    count: action.payload.count,
                },
            };

        case types.FETCH_ALL_INCOMING_REQUEST_FAILURE:
            return {
                ...state,
                incomingRequests: {
                    ...state.incomingRequests,
                    requesting: false,
                    success: false,
                    error: action.payload,
                },
            };

        case types.CREATE_INCOMING_REQUEST_REQUEST:
            return {
                ...state,
                createIncomingRequest: {
                    requesting: true,
                    success: false,
                    error: null,
                },
            };

        case types.CREATE_INCOMING_REQUEST_SUCCESS: {
            let incomingRequests = [...state.incomingRequests.incomingRequests];
            if (action.payload.isDefault) {
                incomingRequests = incomingRequests.map(request => {
                    if (request.isDefault) {
                        request.isDefault = false;
                    }
                    return request;
                });
            }
            return {
                ...state,
                createIncomingRequest: {
                    requesting: false,
                    success: true,
                    error: null,
                    incomingRequest: action.payload,
                },
                incomingRequests: {
                    ...state.incomingRequests,
                    incomingRequests: [action.payload, ...incomingRequests],
                    count: state.incomingRequests.count + 1,
                },
            };
        }

        case types.CREATE_INCOMING_REQUEST_FAILURE:
            return {
                ...state,
                createIncomingRequest: {
                    requesting: false,
                    success: false,
                    error: action.payload,
                },
            };

        case types.EDIT_INCOMING_REQUEST_REQUEST:
            return {
                ...state,
                updateIncomingRequest: {
                    requesting: true,
                    success: false,
                    error: null,
                },
            };

        case types.EDIT_INCOMING_REQUEST_SUCCESS: {
            let incomingRequests = [...state.incomingRequests.incomingRequests];
            if (action.payload.isDefault) {
                incomingRequests = incomingRequests.map(request => {
                    if (request.isDefault) {
                        request.isDefault = false;
                    }
                    return request;
                });
            }

            incomingRequests = incomingRequests.map(request => {
                if (String(request._id) === String(action.payload._id)) {
                    request = action.payload;
                }
                return request;
            });

            return {
                ...state,
                updateIncomingRequest: {
                    requesting: false,
                    success: true,
                    error: null,
                    incomingRequest: action.payload,
                },
                incomingRequests: {
                    ...state.incomingRequests,
                    incomingRequests,
                },
            };
        }

        case types.EDIT_INCOMING_REQUEST_FAILURE:
            return {
                ...state,
                updateIncomingRequest: {
                    requesting: false,
                    success: false,
                    error: action.payload,
                },
            };

        case types.DELETE_INCOMING_REQUEST_REQUEST:
            return {
                ...state,
                deleteIncomingRequest: {
                    requesting: true,
                    success: false,
                    error: null,
                },
            };

        case types.DELETE_INCOMING_REQUEST_SUCCESS: {
            const incomingRequests = state.incomingRequests.incomingRequests.filter(
                request => String(request._id) !== String(action.payload._id)
            );
            return {
                ...state,
                deleteIncomingRequest: {
                    requesting: false,
                    success: true,
                    error: null,
                },
                incomingRequests: {
                    ...state.incomingRequests,
                    incomingRequests,
                    count: state.incomingRequests.count - 1,
                },
            };
        }

        case types.DELETE_INCOMING_REQUEST_FAILURE:
            return {
                ...state,
                deleteIncomingRequest: {
                    requesting: false,
                    success: false,
                    error: action.payload,
                },
            };

        case types.SET_ACTIVE_INCOMING_REQUEST:
            return {
                ...state,
                activeIncomingRequest: action.payload,
            };

        default:
            return state;
    }
}
