pos-dashboard/src/core/redux/reducers/inventoryReducer.js
2025-08-03 20:55:11 +07:00

190 lines
4.2 KiB
JavaScript

import { INVENTORY_ACTIONS } from '../actions/inventoryActions';
const initialState = {
// Inventory list
inventories: [],
totalInventories: 0,
currentPage: 1,
totalPages: 1,
pageSize: 10,
hasPrevious: false,
hasNext: false,
// Current inventory (for edit/view)
currentInventory: null,
// Search results
searchResults: [],
searchQuery: '',
// Loading states
loading: false,
inventoryLoading: false,
searchLoading: false,
// Error states
error: null,
inventoryError: null,
searchError: null,
// Operation states
creating: false,
updating: false,
deleting: false,
};
const inventoryReducer = (state = initialState, action) => {
switch (action.type) {
// Fetch Inventories
case INVENTORY_ACTIONS.FETCH_INVENTORIES_REQUEST:
return {
...state,
loading: true,
error: null,
};
case INVENTORY_ACTIONS.FETCH_INVENTORIES_SUCCESS: {
const { inventory, total_count, page, total_pages, limit } =
action.payload.data;
return {
...state,
loading: false,
inventories: inventory,
totalInventories: total_count || inventory.length,
currentPage: page || 1,
totalPages: total_pages || 1,
pageSize: limit || 10,
hasPrevious: false,
hasNext: false,
error: null,
};
}
case INVENTORY_ACTIONS.FETCH_INVENTORIES_FAILURE:
return {
...state,
loading: false,
error: action.payload,
};
// Fetch Single Inventory
case INVENTORY_ACTIONS.FETCH_INVENTORY_REQUEST:
return {
...state,
inventoryLoading: true,
inventoryError: null,
};
case INVENTORY_ACTIONS.FETCH_INVENTORY_SUCCESS:
return {
...state,
inventoryLoading: false,
currentInventory: action.payload.data,
inventoryError: null,
};
case INVENTORY_ACTIONS.FETCH_INVENTORY_FAILURE:
return {
...state,
inventoryLoading: false,
inventoryError: action.payload,
};
// Create Inventory
case INVENTORY_ACTIONS.CREATE_INVENTORY_REQUEST:
return {
...state,
creating: true,
error: null,
};
case INVENTORY_ACTIONS.CREATE_INVENTORY_SUCCESS:
return {
...state,
creating: false,
inventories: [action.payload.data, ...state.inventories],
totalInventories: state.totalInventories + 1,
error: null,
};
case INVENTORY_ACTIONS.CREATE_INVENTORY_FAILURE:
return {
...state,
creating: false,
error: action.payload,
};
// Update Inventory
case INVENTORY_ACTIONS.UPDATE_INVENTORY_REQUEST:
return {
...state,
updating: true,
error: null,
};
case INVENTORY_ACTIONS.UPDATE_INVENTORY_SUCCESS:
return {
...state,
updating: false,
inventories: state.inventories.map(inv =>
inv.id === action.payload.data.id ? action.payload.data : inv
),
currentInventory: action.payload.data,
error: null,
};
case INVENTORY_ACTIONS.UPDATE_INVENTORY_FAILURE:
return {
...state,
updating: false,
error: action.payload,
};
// Delete Inventory
case INVENTORY_ACTIONS.DELETE_INVENTORY_REQUEST:
return {
...state,
deleting: true,
error: null,
};
case INVENTORY_ACTIONS.DELETE_INVENTORY_SUCCESS:
return {
...state,
deleting: false,
inventories: state.inventories.filter(inv => inv.id !== action.payload),
totalInventories: state.totalInventories - 1,
error: null,
};
case INVENTORY_ACTIONS.DELETE_INVENTORY_FAILURE:
return {
...state,
deleting: false,
error: action.payload,
};
// Clear states
case INVENTORY_ACTIONS.CLEAR_INVENTORY_ERROR:
return {
...state,
error: null,
inventoryError: null,
searchError: null,
};
case INVENTORY_ACTIONS.CLEAR_CURRENT_INVENTORY:
return {
...state,
currentInventory: null,
inventoryError: null,
};
default:
return state;
}
};
export default inventoryReducer;