|
1 | 1 | "use client"; |
2 | 2 |
|
| 3 | +import { useState, useTransition } from "react"; |
| 4 | + |
3 | 5 | import { useTranslations } from "next-intl"; |
4 | 6 |
|
| 7 | +import { verifyEmailAction } from "@/app/(frontend)/[locale]/(app)/auth/verify-email/actions"; |
| 8 | + |
5 | 9 | import { Button } from "@/components/ui/button"; |
6 | 10 | import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; |
7 | 11 |
|
8 | 12 | import { Link } from "@/i18n/navigation"; |
9 | 13 |
|
10 | | -export function VerifyEmail(props: React.ComponentProps<"div">) { |
| 14 | +type Status = "idle" | "success" | "error"; |
| 15 | + |
| 16 | +interface VerifyEmailProps extends React.ComponentProps<"div"> { |
| 17 | + token: string; |
| 18 | +} |
| 19 | + |
| 20 | +export function VerifyEmail({ token, ...props }: VerifyEmailProps) { |
11 | 21 | const t = useTranslations(); |
| 22 | + const [status, setStatus] = useState<Status>("idle"); |
| 23 | + const [isPending, startTransition] = useTransition(); |
| 24 | + |
| 25 | + const handleConfirm = () => { |
| 26 | + startTransition(async () => { |
| 27 | + const result = await verifyEmailAction(token); |
| 28 | + setStatus(result.success ? "success" : "error"); |
| 29 | + }); |
| 30 | + }; |
| 31 | + |
| 32 | + if (status === "success") { |
| 33 | + return ( |
| 34 | + <Card className="border-none shadow-none" {...props}> |
| 35 | + <CardHeader> |
| 36 | + <CardTitle className="text-3xl text-primary">{t("auth-verify-email-title")}</CardTitle> |
| 37 | + <CardDescription className="font-medium"> |
| 38 | + {t("auth-verify-email-description")} |
| 39 | + </CardDescription> |
| 40 | + </CardHeader> |
| 41 | + <CardContent> |
| 42 | + <Link href="/auth/sign-in"> |
| 43 | + <Button size="lg" className="w-full"> |
| 44 | + {t("auth-link-back-to-sign-in")} |
| 45 | + </Button> |
| 46 | + </Link> |
| 47 | + </CardContent> |
| 48 | + </Card> |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + if (status === "error") { |
| 53 | + return ( |
| 54 | + <Card className="border-none shadow-none" {...props}> |
| 55 | + <CardHeader> |
| 56 | + <CardTitle className="text-3xl text-primary"> |
| 57 | + {t("auth-verify-email-error-title")} |
| 58 | + </CardTitle> |
| 59 | + <CardDescription className="font-medium"> |
| 60 | + {t("auth-verify-email-error-description")} |
| 61 | + </CardDescription> |
| 62 | + </CardHeader> |
| 63 | + <CardContent> |
| 64 | + <Link href="/auth/sign-in"> |
| 65 | + <Button size="lg" className="w-full"> |
| 66 | + {t("auth-link-back-to-sign-in")} |
| 67 | + </Button> |
| 68 | + </Link> |
| 69 | + </CardContent> |
| 70 | + </Card> |
| 71 | + ); |
| 72 | + } |
12 | 73 |
|
13 | 74 | return ( |
14 | 75 | <Card className="border-none shadow-none" {...props}> |
15 | 76 | <CardHeader> |
16 | | - <CardTitle className="text-3xl text-primary">{t("auth-verify-email-title")}</CardTitle> |
| 77 | + <CardTitle className="text-3xl text-primary"> |
| 78 | + {t("auth-verify-email-confirm-title")} |
| 79 | + </CardTitle> |
17 | 80 | <CardDescription className="font-medium"> |
18 | | - {t("auth-verify-email-description")} |
| 81 | + {t("auth-verify-email-confirm-description")} |
19 | 82 | </CardDescription> |
20 | 83 | </CardHeader> |
21 | 84 | <CardContent> |
22 | | - <Link href="/auth/sign-in"> |
23 | | - <Button size="lg" className="w-full"> |
24 | | - {t("auth-link-back-to-sign-in")} |
25 | | - </Button> |
26 | | - </Link> |
| 85 | + <Button size="lg" className="w-full" onClick={handleConfirm} disabled={isPending}> |
| 86 | + {isPending |
| 87 | + ? t("auth-verify-email-confirm-button-pending") |
| 88 | + : t("auth-verify-email-confirm-button")} |
| 89 | + </Button> |
27 | 90 | </CardContent> |
28 | 91 | </Card> |
29 | 92 | ); |
|
0 commit comments