80 lines
2.3 KiB
TypeScript
80 lines
2.3 KiB
TypeScript
import { createSlice, type PayloadAction } from '@reduxjs/toolkit';
|
|
|
|
import type { AsyncStatus, DepositDraft, DepositProduct } from '../../../types/app';
|
|
import { seedDepositProducts } from './depositThunk';
|
|
|
|
interface DepositState {
|
|
status: AsyncStatus;
|
|
products: DepositProduct[];
|
|
draft: DepositDraft;
|
|
receiptId: string | null;
|
|
selectedDepositId: string | null;
|
|
}
|
|
|
|
const initialState: DepositState = {
|
|
status: 'idle',
|
|
products: [],
|
|
draft: {
|
|
amount: '',
|
|
monthlyInstallment: '',
|
|
tenureMonths: 12,
|
|
interestPayoutType: 'cumulative',
|
|
},
|
|
receiptId: null,
|
|
selectedDepositId: null,
|
|
};
|
|
|
|
const depositSlice = createSlice({
|
|
name: 'deposit',
|
|
initialState,
|
|
reducers: {
|
|
setDepositProduct(state, action: PayloadAction<string>) {
|
|
state.draft.productId = action.payload;
|
|
},
|
|
setDepositAmount(state, action: PayloadAction<string>) {
|
|
state.draft.amount = action.payload;
|
|
},
|
|
setMonthlyInstallment(state, action: PayloadAction<string>) {
|
|
state.draft.monthlyInstallment = action.payload;
|
|
},
|
|
setDepositTenure(state, action: PayloadAction<number>) {
|
|
state.draft.tenureMonths = action.payload;
|
|
},
|
|
setInterestPayoutType(state, action: PayloadAction<'cumulative' | 'non-cumulative'>) {
|
|
state.draft.interestPayoutType = action.payload;
|
|
},
|
|
setDepositReceipt(state, action: PayloadAction<string>) {
|
|
state.receiptId = action.payload;
|
|
},
|
|
setSelectedDeposit(state, action: PayloadAction<string | null>) {
|
|
state.selectedDepositId = action.payload;
|
|
},
|
|
resetDepositFlow(state) {
|
|
state.draft = {
|
|
amount: '',
|
|
monthlyInstallment: '',
|
|
tenureMonths: 12,
|
|
interestPayoutType: 'cumulative',
|
|
};
|
|
state.receiptId = null;
|
|
state.selectedDepositId = null;
|
|
},
|
|
},
|
|
extraReducers: (builder) => {
|
|
builder
|
|
.addCase(seedDepositProducts.pending, (state) => {
|
|
state.status = 'loading';
|
|
})
|
|
.addCase(seedDepositProducts.fulfilled, (state, action) => {
|
|
state.status = 'succeeded';
|
|
state.products = action.payload;
|
|
})
|
|
.addCase(seedDepositProducts.rejected, (state) => {
|
|
state.status = 'failed';
|
|
});
|
|
},
|
|
});
|
|
|
|
export const depositActions = depositSlice.actions;
|
|
export const depositReducer = depositSlice.reducer;
|