39 lines
976 B
TypeScript
39 lines
976 B
TypeScript
import { createReducer } from '@reduxjs/toolkit';
|
|
import { addLead, resetAddLeadState } from './thunk';
|
|
|
|
export interface AddLeadState {
|
|
loading: boolean;
|
|
error: string | null;
|
|
successMessage: string | null;
|
|
}
|
|
|
|
const initialState: AddLeadState = {
|
|
loading: false,
|
|
error: null,
|
|
successMessage: null,
|
|
};
|
|
|
|
export const addLeadReducer = createReducer(initialState, builder => {
|
|
builder
|
|
.addCase(resetAddLeadState, () => initialState)
|
|
.addCase(addLead.pending, acc => {
|
|
acc.loading = true;
|
|
acc.error = null;
|
|
acc.successMessage = null;
|
|
})
|
|
.addCase(addLead.fulfilled, (acc, action) => {
|
|
acc.loading = false;
|
|
acc.successMessage = action.payload.message;
|
|
acc.error = null;
|
|
})
|
|
.addCase(addLead.rejected, (acc, action) => {
|
|
acc.loading = false;
|
|
acc.error =
|
|
(action.payload as string) ??
|
|
action.error.message ??
|
|
'Failed to add lead';
|
|
});
|
|
});
|
|
|
|
export default addLeadReducer;
|