etsy-finance-tracker/client/src/store/slices/customerSlice.ts
dlawler489 1a3bd33be8 Migrate frontend from localStorage to MongoDB API
- Remove localStorage from all 4 Redux slices (products, orders, expenses, customers)
- Layout fetches all data from API on mount; adds logout button with active nav highlighting
- Wire API calls in Products, Orders, Expenses pages for all CRUD operations
- DataImport uses POST /orders/bulk for CSV upserts and API for PDF slip orders
- MissingProductsModal creates products via API
- Relax Order model: optional customerId, embedded customer, fees, printingCost on items, default paymentStatus=paid
- Relax Expense model: free-string category, add taxDeductible/vendor/reference fields
- Add printingCost to Product model
- Add POST /orders/bulk endpoint for upsert-by-orderNumber
- Raise rate limit to 1000 req/15min for bulk imports

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-22 08:48:05 +10:00

47 lines
1.3 KiB
TypeScript

import { createSlice, PayloadAction } from '@reduxjs/toolkit';
export interface Customer {
_id: string;
name: string;
email: string;
totalOrders: number;
totalSpent: number;
}
interface CustomerState {
customers: Customer[];
loading: boolean;
error: string | null;
}
const initialState: CustomerState = {
customers: [],
loading: false,
error: null,
};
const customerSlice = createSlice({
name: 'customers',
initialState,
reducers: {
setCustomers: (state, action: PayloadAction<Customer[]>) => {
state.customers = action.payload;
},
addCustomer: (state, action: PayloadAction<Customer>) => {
state.customers.push(action.payload);
},
updateCustomer: (state, action: PayloadAction<Customer>) => {
const index = state.customers.findIndex(c => c._id === action.payload._id);
if (index !== -1) state.customers[index] = action.payload;
},
setLoading: (state, action: PayloadAction<boolean>) => {
state.loading = action.payload;
},
setError: (state, action: PayloadAction<string | null>) => {
state.error = action.payload;
},
},
});
export const { setCustomers, addCustomer, updateCustomer, setLoading, setError } = customerSlice.actions;
export default customerSlice.reducer;