import { createSlice, createAsyncThunk } from "@reduxjs/toolkit"; import sebnInventory from "../../apis/sebnInventory"; import _ from "lodash"; import i18n from "../../i18n"; export const getBranches = createAsyncThunk("main/getBranches", async () => { const response = await sebnInventory.get("branch/"); return response.data.results; }); export const getAreas = createAsyncThunk("main/getAreas", async (thunkAPI) => { const response = await sebnInventory.get(`area/?branch=${thunkAPI.id}`); return response.data.results; }); export const getBranch = createAsyncThunk("main/getBranch", async () => { let branch = localStorage.getItem("branch"); if (branch) { const response = await sebnInventory.get(`branch/${branch}/`); return response.data; } return null; }); export const getInventoryTaking = createAsyncThunk( "main/getInventoryTaking", async () => { const response = await sebnInventory.get(`inventory_taking/`); if (response.data.results[0]) { return response.data.results[0]; } } ); export const checkItem = createAsyncThunk( "main/checkItem", async (state, thunkAPI) => { if ( _.findIndex(thunkAPI.getState().search.history, function (o) { return ( o.inventory_id === thunkAPI.getState().search.selected_item.inventory_id ); }) === -1 ) { return null; } else { return { name: "Error", message: i18n.t("messages.ItemWasAlreadyChecked"), }; } } ); export const mainSlice = createSlice({ name: "main", initialState: { branches: [], branch: { id: localStorage.getItem("branch") }, areas: [], area: "", inventory_taking: null, label: "", alerts: [], }, reducers: { selectBranch: (state, action) => { localStorage.setItem("branch", action.payload.id); state.branch = action.payload; }, selectArea: (state, action) => { state.area = action.payload; }, clearArea: (state, action) => { state.area = ""; }, setLabel: (state, action) => { state.label = action.payload; }, clearLabel: (state) => { state.label = ""; }, incrementLabel: (state, action) => { state.label = parseInt(state.label) + 1; }, addAlert: (state, action) => { state.alerts.push(action.payload); }, removeAlert: (state, action) => { state.alerts = _.remove(state.alerts, function (i) { return i === action.payload; }); }, }, extraReducers: { [getBranches.fulfilled]: (state, action) => { state.branches = action.payload; }, [getAreas.fulfilled]: (state, action) => { state.areas = action.payload; }, [getBranch.fulfilled]: (state, action) => { state.branch = action.payload; }, [checkItem.fulfilled]: (state, action) => { if (action.payload) { state.alerts.push(action.payload); } }, [getInventoryTaking.fulfilled]: (state, action) => { if (action.payload) { state.inventory_taking = action.payload; } else { state.alerts.push({ name: "Error", message: i18n.t("messages.NoInventoryRunning"), }); } }, }, }); // Action creators are generated for each case reducer function export const { selectBranch, selectArea, setLabel, incrementLabel, clearArea, clearLabel, addAlert, removeAlert, } = mainSlice.actions; export default mainSlice.reducer;