68 lines
2.1 KiB
TypeScript
68 lines
2.1 KiB
TypeScript
import { IPaywallProduct } from "@/api/resources/Paywall";
|
|
import { SubscriptionReceipt } from "@/api/resources/UserSubscriptionReceipts";
|
|
import { TCanMakePaymentResult } from "@/hooks/payment/useCanUseStripeButton";
|
|
import { createSlice, createSelector } from "@reduxjs/toolkit";
|
|
import type { PayloadAction } from "@reduxjs/toolkit";
|
|
import { PaymentRequest } from "@stripe/stripe-js";
|
|
|
|
interface IStripeButton {
|
|
paymentRequest: PaymentRequest | null;
|
|
availableMethods: TCanMakePaymentResult;
|
|
}
|
|
|
|
interface IPayment {
|
|
selectedPrice: number | null;
|
|
isDiscount: boolean;
|
|
subscriptionReceipt: SubscriptionReceipt | null;
|
|
activeProduct: IPaywallProduct | null;
|
|
stripeButton: IStripeButton;
|
|
}
|
|
|
|
const initialState: IPayment = {
|
|
selectedPrice: null,
|
|
isDiscount: false,
|
|
subscriptionReceipt: null,
|
|
activeProduct: null,
|
|
stripeButton: {
|
|
paymentRequest: null,
|
|
availableMethods: null,
|
|
}
|
|
};
|
|
|
|
const paymentSlice = createSlice({
|
|
name: "payment",
|
|
initialState,
|
|
reducers: {
|
|
update(state, action: PayloadAction<Partial<IPayment>>) {
|
|
return { ...state, ...action.payload };
|
|
},
|
|
updateStripeButton(state, action: PayloadAction<IStripeButton>) {
|
|
return { ...state, stripeButton: action.payload };
|
|
},
|
|
},
|
|
extraReducers: (builder) => builder.addCase("reset", () => initialState),
|
|
});
|
|
|
|
export const { actions } = paymentSlice;
|
|
export const selectSelectedPrice = createSelector(
|
|
(state: { payment: IPayment }) => state.payment.selectedPrice,
|
|
(payment) => payment
|
|
);
|
|
export const selectActiveProduct = createSelector(
|
|
(state: { payment: IPayment }) => state.payment.activeProduct,
|
|
(payment) => payment
|
|
);
|
|
export const selectIsDiscount = createSelector(
|
|
(state: { payment: IPayment }) => state.payment.isDiscount,
|
|
(payment) => payment
|
|
);
|
|
export const selectSubscriptionReceipt = createSelector(
|
|
(state: { payment: IPayment }) => state.payment.subscriptionReceipt,
|
|
(payment) => payment
|
|
);
|
|
export const selectStripeButton = createSelector(
|
|
(state: { payment: IPayment }) => state.payment.stripeButton,
|
|
(payment) => payment
|
|
);
|
|
export default paymentSlice.reducer;
|