71 lines
2.1 KiB
TypeScript
71 lines
2.1 KiB
TypeScript
import { createReducer } from '@reduxjs/toolkit';
|
|
import { DashboardLeadCountItem, DashboardProjectActivityItem } from '@interfaces';
|
|
import { getDashboardLeadCount, getDashboardProjectActivity, resetDashboardState } from './thunk';
|
|
|
|
export interface DashboardState {
|
|
leadCountData: DashboardLeadCountItem[];
|
|
projectActivityData: DashboardProjectActivityItem[];
|
|
loading: boolean;
|
|
activityLoading: boolean;
|
|
error: string | null;
|
|
activityError: string | null;
|
|
}
|
|
|
|
const initialState: DashboardState = {
|
|
leadCountData: [],
|
|
projectActivityData: [],
|
|
loading: false,
|
|
activityLoading: false,
|
|
error: null,
|
|
activityError: null,
|
|
};
|
|
|
|
export const dashboardReducer = createReducer(initialState, builder => {
|
|
builder
|
|
// Lead Count
|
|
.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';
|
|
})
|
|
// Project Activity
|
|
.addCase(getDashboardProjectActivity.pending, acc => {
|
|
acc.activityLoading = true;
|
|
acc.activityError = null;
|
|
})
|
|
.addCase(getDashboardProjectActivity.fulfilled, (acc, action) => {
|
|
acc.activityLoading = false;
|
|
acc.projectActivityData = action.payload;
|
|
acc.activityError = null;
|
|
})
|
|
.addCase(getDashboardProjectActivity.rejected, (acc, action) => {
|
|
acc.activityLoading = false;
|
|
acc.activityError =
|
|
(action.payload as string) ??
|
|
action.error.message ??
|
|
'Failed to load project activity log';
|
|
})
|
|
// Reset
|
|
.addCase(resetDashboardState, acc => {
|
|
acc.leadCountData = [];
|
|
acc.projectActivityData = [];
|
|
acc.loading = false;
|
|
acc.activityLoading = false;
|
|
acc.error = null;
|
|
acc.activityError = null;
|
|
});
|
|
});
|
|
|
|
export default dashboardReducer;
|