"use client" import { AnimatePresence, motion } from "framer-motion" import { usePathname } from "next/navigation" import React, { useState } from "react" import { Dialog, Modal, ModalOverlay } from "react-aria-components" import { useIntl } from "react-intl" import { benefits } from "@/constants/routes/myPages" import { dt } from "@/lib/dt" import { trpc } from "@/lib/trpc/client" import { ChevronRightSmallIcon, CloseLargeIcon } from "@/components/Icons" import Image from "@/components/Image" import Button from "@/components/TempDesignSystem/Button" import Link from "@/components/TempDesignSystem/Link" import Body from "@/components/TempDesignSystem/Text/Body" import Caption from "@/components/TempDesignSystem/Text/Caption" import Title from "@/components/TempDesignSystem/Text/Title" import { toast } from "@/components/TempDesignSystem/Toasts" import useLang from "@/hooks/useLang" import styles from "./surprises.module.css" import type { Surprise, SurprisesProps, } from "@/types/components/blocks/surprises" const variants = { enter: (direction: number) => { return { x: direction > 0 ? 1000 : -1000, opacity: 0, } }, center: { x: 0, opacity: 1, }, exit: (direction: number) => { return { x: direction < 0 ? 1000 : -1000, opacity: 0, } }, } export default function SurprisesNotification({ surprises, membershipNumber, }: SurprisesProps) { const lang = useLang() const pathname = usePathname() const [open, setOpen] = useState(true) const [[selectedSurprise, direction], setSelectedSurprise] = useState([0, 0]) const [showSurprises, setShowSurprises] = useState(false) const unwrap = trpc.contentstack.rewards.unwrap.useMutation({ onSuccess: () => { if (pathname.indexOf(benefits[lang]) !== 0) { toast.success( <> {intl.formatMessage( { id: "Gift(s) added to your benefits" }, { amount: surprises.length } )}
{intl.formatMessage({ id: "Go to My Benefits" })} ) } }, onError: (error) => { console.error("Error", error) }, }) const intl = useIntl() if (!surprises.length) { return null } const surprise = surprises[selectedSurprise] function showSurprise(newDirection: number) { setSelectedSurprise(([currentIndex]) => [ currentIndex + newDirection, newDirection, ]) } async function viewRewards() { const updates = surprises .map((surprise) => { const coupons = surprise.coupons ?.map((coupon) => { if (coupon?.couponCode) { return { rewardId: surprise.id, couponCode: coupon.couponCode, } } }) .filter( (coupon): coupon is { rewardId: string; couponCode: string } => !!coupon ) return coupons }) .flat() .filter( (coupon): coupon is { rewardId: string; couponCode: string } => !!coupon ) unwrap.mutate(updates) } const earliestExpirationDate = surprise.coupons?.reduce( (earliestDate, coupon) => { const expiresAt = dt(coupon.expiresAt) return earliestDate.isBefore(expiresAt) ? earliestDate : expiresAt }, dt() ) return ( {({ close }) => { return ( <>
{surprises.length > 1 && showSurprises && ( {intl.formatMessage( { id: "{amount} out of {total}" }, { amount: selectedSurprise + 1, total: surprises.length, } )} )}
{showSurprises ? ( <> {surprise.description}
{intl.formatMessage( { id: "Expires at the earliest" }, { date: dt(earliestExpirationDate) .locale(lang) .format("D MMM YYYY"), } )} {intl.formatMessage({ id: "Membership ID", })}{" "} {membershipNumber}
{surprises.length > 1 && ( <> )} ) : ( {surprises.length > 1 ? ( <> {intl.formatMessage( { id: "You have # gifts waiting for you!", }, { amount: surprises.length, b: (str) => {str}, } )}
{intl.formatMessage({ id: "Hurry up and use them before they expire!", })} ) : ( intl.formatMessage({ id: "We have a special gift waiting for you!", }) )} {intl.formatMessage({ id: "You'll find all your gifts in 'My benefits'", })}
)} ) }}
) } function Surprise({ title, children, }: { title?: string children?: React.ReactNode }) { return (
Gift
{title}
{children}
) }