import { PaymentMethodsResponse, PlaceOrderResponse } from '@interfaces'; import { createReducer } from '@reduxjs/toolkit'; import { getAllPaymentMethodsThunk, getOrderByIdThunk, getOrderHistoryThunk, placeOrderThunk, } from './thunk'; import { Order } from '@interfaces/order'; export interface PaymentMethodsState { paymentMethods: PaymentMethodsResponse; isLoading: boolean; error: string | null; placeOrderLoading: boolean; placeOrderSuccess: boolean; placeOrderError: string | null; orderHistory: Order[] | null; placeOrderResponse: PlaceOrderResponse | null; orderDetails: Order | null; orderDetailsLoading: boolean; orderDetailsError: string | null; } const initialState: PaymentMethodsState = { paymentMethods: [], isLoading: false, error: null, placeOrderLoading: false, placeOrderSuccess: false, placeOrderError: null, orderHistory: null, placeOrderResponse: null, orderDetails: null, orderDetailsLoading: false, orderDetailsError: null, }; const paymentMethodsReducer = createReducer(initialState, builder => { builder .addCase(getAllPaymentMethodsThunk.pending, state => { state.isLoading = true; state.error = null; }) .addCase(getAllPaymentMethodsThunk.fulfilled, (state, action) => { state.isLoading = false; state.paymentMethods = action.payload; }) .addCase(getAllPaymentMethodsThunk.rejected, (state, action) => { state.isLoading = false; state.error = action.payload as string; }) .addCase(placeOrderThunk.pending, state => { state.placeOrderLoading = true; state.placeOrderSuccess = false; state.placeOrderError = null; }) .addCase(placeOrderThunk.fulfilled, (state, action) => { state.placeOrderLoading = false; state.placeOrderSuccess = true; state.placeOrderResponse = action.payload; }) .addCase(placeOrderThunk.rejected, (state, action) => { state.placeOrderLoading = false; state.placeOrderSuccess = false; state.placeOrderError = action.payload as string; }) .addCase(getOrderHistoryThunk.pending, state => { state.isLoading = true; state.error = null; }) .addCase(getOrderHistoryThunk.fulfilled, (state, action) => { state.isLoading = false; state.orderHistory = action.payload; }) .addCase(getOrderHistoryThunk.rejected, (state, action) => { state.isLoading = false; state.error = action.payload as string; }) .addCase(getOrderByIdThunk.pending, state => { state.orderDetailsLoading = true; state.orderDetailsError = null; }) .addCase(getOrderByIdThunk.fulfilled, (state, action) => { state.orderDetailsLoading = false; state.orderDetails = action.payload; }) .addCase(getOrderByIdThunk.rejected, (state, action) => { state.orderDetailsLoading = false; state.orderDetailsError = action.payload as string; }); }); export default paymentMethodsReducer;