import { createReducer } from '@reduxjs/toolkit'; import { DashboardLeadCountItem } from '@interfaces'; import { getDashboardLeadCount, resetDashboardState } from './thunk'; export interface DashboardState { leadCountData: DashboardLeadCountItem[]; loading: boolean; error: string | null; } const initialState: DashboardState = { leadCountData: [], loading: false, error: null, }; export const dashboardReducer = createReducer(initialState, builder => { builder .addCase(getDashboardLeadCount.pending, acc => { acc.loading = true; acc.error = null; }) .addCase(getDashboardLeadCount.fulfilled, (acc, action) => { acc.loading = false; acc.leadCountData = action.payload; acc.error = null; }) .addCase(getDashboardLeadCount.rejected, (acc, action) => { acc.loading = false; acc.error = (action.payload as string) ?? action.error.message ?? 'Failed to load dashboard lead count'; }) .addCase(resetDashboardState, acc => { acc.leadCountData = []; acc.loading = false; acc.error = null; }); }); export default dashboardReducer;