46 lines
1.2 KiB
TypeScript

import { createReducer } from '@reduxjs/toolkit';
import { getLeadDetails } from './thunk';
import { updateLeadStatus } from '../leads/thunk';
import { LeadDetailsItem } from '@interfaces';
export interface LeadDetailsState {
item: LeadDetailsItem | null;
loading: boolean;
error: string | null;
}
const initialState: LeadDetailsState = {
item: null,
loading: false,
error: null,
};
export const reducers = createReducer(initialState, builder => {
builder
.addCase(getLeadDetails.pending, acc => {
acc.loading = true;
acc.error = null;
})
.addCase(getLeadDetails.fulfilled, (acc, action) => {
acc.loading = false;
acc.item = action.payload;
acc.error = null;
})
.addCase(getLeadDetails.rejected, (acc, action) => {
acc.loading = false;
acc.error =
(action.payload as string) ??
action.error.message ??
'Failed to load lead details';
})
.addCase(updateLeadStatus.fulfilled, (acc, action) => {
if (acc.item && acc.item.id === action.payload.leadId) {
acc.item.status = action.payload.statusId;
acc.item.status_name = action.payload.statusName;
acc.item.color = action.payload.color;
}
});
});
export default reducers;