{"size": 2403, "ext": "js", "max_stars_repo_path": "frontend/src/reducers/sealReducer.js", "max_stars_repo_name": "UsernameForGerman/PrettlNKKomax", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "frontend/src/reducers/sealReducer.js", "max_issues_repo_name": "UsernameForGerman/PrettlNKKomax", "max_issues_repo_issues_event_min_datetime": "2021-03-19T14:08:01.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-12T00:41:09.000Z", "max_forks_repo_path": "frontend/src/reducers/sealReducer.js", "max_forks_repo_name": "UsernameForGerman/PrettlNKKomax", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 9, "max_forks_count": null, "avg_line_length": 24.5204081633, "max_line_length": 90, "alphanum_fraction": 0.5201831045}
import komax_seal_api from "../DAL/komax_seal/komax_seal_api";
import handle401 from "./handle401";
import {logoutThunk} from "./authReducer";

const initialState = {
    isFetching : false,
    sealsList : []
}

const TOGGLE_FETCHING = "SEAL/TOGGLE_FETCHING";
const SET_LIST = "SEAL/TOGGLE_SET_LIST";

const sealReducer = (state = initialState, action) => {
    let stateCopy = {...state};
    switch (action.type) {
        case TOGGLE_FETCHING : {
            stateCopy.isFetching = !stateCopy.isFetching;
            break;
        }

        case SET_LIST : {
            stateCopy.sealsList = action.list;
            break;
        }
    }
    return stateCopy;
}

const toggleFetchAC = () => {
    return {
        type : TOGGLE_FETCHING
    }
}

const setListAC = (list) => {
    return {
        type : SET_LIST,
        list : list
    }
}

const getSealsListThunk = () => {
    return (dispatch) => {
        dispatch(toggleFetchAC());
        komax_seal_api.getSealsList()
            .then((data) => {
                dispatch(setListAC(data));
                dispatch(toggleFetchAC());
            })
            .catch(err => {
                handle401(err, dispatch);
            });
    }
}

const updateSealThunk = (seal) => {
    return (dispatch) => {
        dispatch(toggleFetchAC());
        komax_seal_api.updateSeal(seal)
            .then((data) => {
                dispatch(getSealsListThunk());
                dispatch(toggleFetchAC());
            })
            .catch(err => {
                dispatch(logoutThunk())
            })
    }
}

const createSealThunk = (seal) => {
    return (dispatch) => {
        dispatch(toggleFetchAC());
        komax_seal_api.createSeal(seal)
            .then((data) => {
                dispatch(getSealsListThunk());
                dispatch(toggleFetchAC());
            })
            .catch(err => {
                handle401(err, dispatch);
            });
    }
}

const deleteSealThunk = (seal) => {
    return (dispatch) => {
        dispatch(toggleFetchAC());
        komax_seal_api.deleteSeal(seal)
            .then((data) => {
                dispatch(getSealsListThunk());
                dispatch(toggleFetchAC());
            })
            .catch(err => {
                handle401(err, dispatch);
            });
    }
}

export {getSealsListThunk, updateSealThunk, sealReducer, createSealThunk, deleteSealThunk}