38 lines
862 B
TypeScript
38 lines
862 B
TypeScript
import { createReducer } from '@reduxjs/toolkit';
|
|
import { getLeads } from './thunk';
|
|
import { LeadItem } from '@interfaces';
|
|
|
|
export interface LeadsState {
|
|
items: LeadItem[];
|
|
loading: boolean;
|
|
error: string | null;
|
|
}
|
|
|
|
const initialState: LeadsState = {
|
|
items: [],
|
|
loading: false,
|
|
error: 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';
|
|
});
|
|
});
|
|
|
|
export default reducers;
|