import { createReducer } from '@reduxjs/toolkit'; import { getLeads, getLeadCountList } from './thunk'; import { LeadItem, LeadCountItem } from '@interfaces'; export interface LeadsState { items: LeadItem[]; loading: boolean; error: string | null; counts: LeadCountItem[]; countsLoading: boolean; countsError: string | null; } const initialState: LeadsState = { items: [], loading: false, error: null, counts: [], countsLoading: false, countsError: null, }; export const reducers = createReducer(initialState, builder => { builder .addCase(getLeads.pending, acc => { acc.loading = true; acc.error = null; }) .addCase(getLeads.fulfilled, (acc, action) => { acc.loading = false; acc.items = action.payload; acc.error = null; }) .addCase(getLeads.rejected, (acc, action) => { acc.loading = false; acc.error = (action.payload as string) ?? action.error.message ?? 'Failed to load leads'; }) .addCase(getLeadCountList.pending, acc => { acc.countsLoading = true; acc.countsError = null; }) .addCase(getLeadCountList.fulfilled, (acc, action) => { acc.countsLoading = false; acc.counts = action.payload; acc.countsError = null; }) .addCase(getLeadCountList.rejected, (acc, action) => { acc.countsLoading = false; acc.countsError = (action.payload as string) ?? action.error.message ?? 'Failed to load lead counts'; }); }); export default reducers;