232 lines
8.0 KiB
TypeScript
232 lines
8.0 KiB
TypeScript
"use client"
|
|
|
|
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 { trpc } from "@/lib/trpc/client"
|
|
import { ApiReward } from "@/server/routers/contentstack/reward/output"
|
|
|
|
import { ChevronRightSmallIcon, CloseLargeIcon } from "@/components/Icons"
|
|
import Image from "@/components/Image"
|
|
import Button from "@/components/TempDesignSystem/Button"
|
|
import Body from "@/components/TempDesignSystem/Text/Body"
|
|
import Caption from "@/components/TempDesignSystem/Text/Caption"
|
|
import CaptionLabel from "@/components/TempDesignSystem/Text/CaptionLabel"
|
|
import Title from "@/components/TempDesignSystem/Text/Title"
|
|
import { toast } from "@/components/TempDesignSystem/Toasts"
|
|
import useLang from "@/hooks/useLang"
|
|
|
|
import styles from "./surprises.module.css"
|
|
|
|
interface SurprisesProps {
|
|
surprises: ApiReward[]
|
|
}
|
|
|
|
export default function Surprises({ surprises }: SurprisesProps) {
|
|
const lang = useLang()
|
|
const [open, setOpen] = useState(true)
|
|
const [selectedSurprise, setSelectedSurprise] = useState(0)
|
|
const [showSurprises, setShowSurprises] = useState(false)
|
|
const update = trpc.contentstack.rewards.update.useMutation()
|
|
const intl = useIntl()
|
|
|
|
if (!surprises.length) return null
|
|
|
|
function showSurprise(n: number) {
|
|
setSelectedSurprise((surprise) => surprise + n)
|
|
}
|
|
|
|
function viewRewards(id?: string) {
|
|
if (!id) return
|
|
update.mutate({ id })
|
|
}
|
|
|
|
const surprise = surprises[selectedSurprise]
|
|
|
|
return (
|
|
<ModalOverlay
|
|
className={styles.overlay}
|
|
isOpen={open}
|
|
onOpenChange={setOpen}
|
|
>
|
|
<Modal className={styles.modal}>
|
|
<Dialog aria-label="Surprises" className={styles.dialog}>
|
|
{({ close }) => {
|
|
return (
|
|
<>
|
|
<div className={styles.top}>
|
|
{surprises.length > 1 && showSurprises && (
|
|
<CaptionLabel uppercase>
|
|
{intl.formatMessage(
|
|
{ id: "{amount} out of {total}" },
|
|
{
|
|
amount: selectedSurprise + 1,
|
|
total: surprises.length,
|
|
}
|
|
)}
|
|
</CaptionLabel>
|
|
)}
|
|
<button
|
|
onClick={() => {
|
|
viewRewards()
|
|
toast.success(
|
|
<>
|
|
{intl.formatMessage(
|
|
{ id: "Gift(s) added to your benefits" },
|
|
{ amount: surprises.length }
|
|
)}
|
|
<br />
|
|
<a href={benefits[lang]}>
|
|
{intl.formatMessage({ id: "Go to My Benefits" })}
|
|
</a>
|
|
</>
|
|
)
|
|
close()
|
|
}}
|
|
type="button"
|
|
className={styles.close}
|
|
>
|
|
<CloseLargeIcon />
|
|
</button>
|
|
</div>
|
|
{showSurprises ? (
|
|
<>
|
|
<div className={styles.content}>
|
|
<Surprise title={surprise.title}>
|
|
<Body textAlign="center">
|
|
This is just some dummy text describing the gift and
|
|
should be replaced.
|
|
</Body>
|
|
<div className={styles.badge}>
|
|
<Caption>Valid through DD M YYYY</Caption>
|
|
<Caption>Member ID 000000</Caption>
|
|
</div>
|
|
</Surprise>
|
|
</div>
|
|
{surprises.length > 1 && (
|
|
<>
|
|
<nav className={styles.nav}>
|
|
<Button
|
|
wrapping
|
|
variant="icon"
|
|
intent="tertiary"
|
|
disabled={selectedSurprise === 0}
|
|
onClick={() => showSurprise(-1)}
|
|
size="small"
|
|
>
|
|
<ChevronRightSmallIcon
|
|
className={styles.chevron}
|
|
width={20}
|
|
height={20}
|
|
/>
|
|
{intl.formatMessage({ id: "Previous" })}
|
|
</Button>
|
|
<Button
|
|
wrapping
|
|
variant="icon"
|
|
intent="tertiary"
|
|
disabled={selectedSurprise === surprises.length - 1}
|
|
onClick={() => showSurprise(1)}
|
|
size="small"
|
|
>
|
|
{intl.formatMessage({ id: "Next" })}
|
|
<ChevronRightSmallIcon width={20} height={20} />
|
|
</Button>
|
|
</nav>
|
|
</>
|
|
)}
|
|
</>
|
|
) : (
|
|
<div className={styles.content}>
|
|
{surprises.length > 1 ? (
|
|
<Surprise title={intl.formatMessage({ id: "Surprise!" })}>
|
|
<Body textAlign="center">
|
|
{intl.formatMessage<React.ReactNode>(
|
|
{
|
|
id: "You have <b>#</b> gifts waiting for you!",
|
|
},
|
|
{
|
|
amount: surprises.length,
|
|
b: (str) => <b>{str}</b>,
|
|
}
|
|
)}
|
|
<br />
|
|
{intl.formatMessage({
|
|
id: "Hurry up and use them before they expire!",
|
|
})}
|
|
</Body>
|
|
<Caption>
|
|
{intl.formatMessage({
|
|
id: "You'll find all your gifts in 'My benefits'",
|
|
})}
|
|
</Caption>
|
|
</Surprise>
|
|
) : (
|
|
<Surprise title={surprise.title}>
|
|
<Body textAlign="center">
|
|
{intl.formatMessage({
|
|
id: "We have a special gift waiting for you!",
|
|
})}
|
|
</Body>
|
|
<Caption>
|
|
{intl.formatMessage({
|
|
id: "You'll find all your gifts in 'My benefits'",
|
|
})}
|
|
</Caption>
|
|
</Surprise>
|
|
)}
|
|
|
|
<Button
|
|
intent="primary"
|
|
onPress={() => {
|
|
viewRewards(surprise.id)
|
|
setShowSurprises(true)
|
|
}}
|
|
size="medium"
|
|
theme="base"
|
|
fullWidth
|
|
>
|
|
{intl.formatMessage(
|
|
{
|
|
id: "Open gift(s)",
|
|
},
|
|
{ amount: surprises.length }
|
|
)}
|
|
</Button>
|
|
</div>
|
|
)}
|
|
</>
|
|
)
|
|
}}
|
|
</Dialog>
|
|
</Modal>
|
|
</ModalOverlay>
|
|
)
|
|
}
|
|
|
|
function Surprise({
|
|
title,
|
|
children,
|
|
}: {
|
|
title?: string
|
|
children?: React.ReactNode
|
|
}) {
|
|
return (
|
|
<>
|
|
<Image
|
|
src="/_static/img/loyalty-award.png"
|
|
width={113}
|
|
height={125}
|
|
alt="Gift"
|
|
/>
|
|
<Title textAlign="center" level="h4">
|
|
{title}
|
|
</Title>
|
|
|
|
{children}
|
|
</>
|
|
)
|
|
}
|