import { createSlice } from "@reduxjs/toolkit"; import { v4 as uuidv4 } from "uuid"; import { readConfigRequest, readConfigResponse } from "secure-electron-store"; const inventorySlice = createSlice({ name: "inventory", initialState: { items: typeof window.api.store.initial()["inventoryItems"] !== "undefined" ? window.api.store.initial()["inventoryItems"] : [], }, reducers: { addItem(state, action) { state.items = [...state.items, action.payload]; }, replaceItems(state, action) { state.items = action.payload; }, editItem(state, action) { if (action.payload.indices.length == action.payload.newItems.length) { action.payload.newItems.forEach((item, index) => { state.items[action.payload.indices[index]] = item; }); } }, deleteItem(state, action) { // action.payload == ID of item to delete state.items.forEach((item, idx) => { if (Object.assign({}, item).id == action.payload) { state.items.splice(idx, 1); } }); }, }, }); window.api.store.onReceive(readConfigResponse, function (args) { console.log(args, "GANG ARGS"); }); // Export actions export const { addItem, replaceItems, editItem, deleteItem } = inventorySlice.actions; // Export reducer export default inventorySlice.reducer;