Skip to content

Commit

Permalink
Open Share Modal after applying freewallets coupon
Browse files Browse the repository at this point in the history
  • Loading branch information
MananTank committed Oct 18, 2024
1 parent 0dbb174 commit 41ffb94
Show file tree
Hide file tree
Showing 3 changed files with 496 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { Suspense, useEffect, useRef, useState } from "react";
import { useForm } from "react-hook-form";
import { toast } from "sonner";
import { z } from "zod";
import { ShareFreeWalletsModal } from "./share-free-wallets-modal.client";

export type ActiveCouponResponse = {
id: string;
Expand All @@ -36,7 +37,10 @@ export type ActiveCouponResponse = {

function ApplyCouponCard(props: {
teamId: string | undefined;
onCouponApplied: (data: ActiveCouponResponse) => void;
onCouponApplied: (
data: ActiveCouponResponse,
isFreeWalletsCoupon: boolean,
) => void;
isPaymentSetup: boolean;
onAddPayment: () => void;
}) {
Expand Down Expand Up @@ -87,7 +91,9 @@ export function ApplyCouponCardUI(props: {
status: number;
data: null | ActiveCouponResponse;
}>;
onCouponApplied: ((data: ActiveCouponResponse) => void) | undefined;
onCouponApplied:
| ((data: ActiveCouponResponse, isFreeWalletsCoupon: boolean) => void)
| undefined;
prefillPromoCode?: string;
scrollIntoView?: boolean;
isPaymentSetup: boolean;
Expand Down Expand Up @@ -130,7 +136,12 @@ export function ApplyCouponCardUI(props: {
case 200: {
toast.success("Coupon applied successfully");
if (res.data) {
props.onCouponApplied?.(res.data);
props.onCouponApplied?.(
res.data,
// prod & dev
values.promoCode === "FREEWALLETS" ||
values.promoCode === "TESTFREEWALLETS",
);
}
break;
}
Expand Down Expand Up @@ -258,6 +269,7 @@ export function CouponSection(props: {
isPaymentSetup: boolean;
onAddPayment: () => void;
}) {
const [showShareModal, setShowShareModal] = useState(false);
const loggedInUser = useLoggedInUser();
const [optimisticCouponData, setOptimisticCouponData] = useState<
| {
Expand Down Expand Up @@ -319,35 +331,44 @@ export function CouponSection(props: {
? optimisticCouponData.data
: activeCoupon.data;

if (couponData) {
return (
<CouponDetailsCardUI
activeCoupon={couponData}
deleteCoupon={{
mutateAsync: deleteActiveCoupon.mutateAsync,
isPending: deleteActiveCoupon.isPending,
}}
/>
);
}

return (
<Suspense fallback={<LoadingCouponSection />}>
<ApplyCouponCard
teamId={props.teamId}
onCouponApplied={(coupon) => {
setOptimisticCouponData({
type: "added",
data: coupon,
});
activeCoupon.refetch().then(() => {
setOptimisticCouponData(undefined);
});
}}
isPaymentSetup={props.isPaymentSetup}
onAddPayment={props.onAddPayment}
<>
{couponData ? (
<CouponDetailsCardUI
activeCoupon={couponData}
deleteCoupon={{
mutateAsync: deleteActiveCoupon.mutateAsync,
isPending: deleteActiveCoupon.isPending,
}}
/>
) : (
<Suspense fallback={<LoadingCouponSection />}>
<ApplyCouponCard
teamId={props.teamId}
onCouponApplied={(coupon, isFreeWalletsCoupon) => {
setOptimisticCouponData({
type: "added",
data: coupon,
});

if (isFreeWalletsCoupon) {
setShowShareModal(true);
}
activeCoupon.refetch().then(() => {
setOptimisticCouponData(undefined);
});
}}
isPaymentSetup={props.isPaymentSetup}
onAddPayment={props.onAddPayment}
/>
</Suspense>
)}

<ShareFreeWalletsModal
isOpen={showShareModal}
onOpenChange={setShowShareModal}
/>
</Suspense>
</>
);
}

Expand Down
Loading

0 comments on commit 41ffb94

Please sign in to comment.