()
| 21 | }); |
| 22 | |
| 23 | function RouteComponent() { |
| 24 | const search = useSearch({ from: '/unsubscribe' }); |
| 25 | const { email, category, token } = search; |
| 26 | const trpc = useTRPC(); |
| 27 | const [isUnsubscribing, setIsUnsubscribing] = useState(false); |
| 28 | const [isSuccess, setIsSuccess] = useState(false); |
| 29 | const [error, setError] = useState<string | null>(null); |
| 30 | |
| 31 | const unsubscribeMutation = useMutation( |
| 32 | trpc.email.unsubscribe.mutationOptions({ |
| 33 | onSuccess: () => { |
| 34 | setIsSuccess(true); |
| 35 | setIsUnsubscribing(false); |
| 36 | }, |
| 37 | onError: (err) => { |
| 38 | setError(err.message || 'Failed to unsubscribe'); |
| 39 | setIsUnsubscribing(false); |
| 40 | }, |
| 41 | }), |
| 42 | ); |
| 43 | |
| 44 | const handleUnsubscribe = () => { |
| 45 | setIsUnsubscribing(true); |
| 46 | setError(null); |
| 47 | unsubscribeMutation.mutate({ email, category, token }); |
| 48 | }; |
| 49 | |
| 50 | const categoryName = |
| 51 | emailCategories[category as keyof typeof emailCategories]?.label ?? category; |
| 52 | |
| 53 | if (isSuccess) { |
| 54 | return ( |
| 55 | <PublicPageCard |
| 56 | title="Unsubscribed" |
| 57 | description={`You've been unsubscribed from ${categoryName} emails. You won't receive any more ${categoryName.toLowerCase()} emails from |
| 58 | us.`} |
| 59 | /> |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | return ( |
| 64 | <PublicPageCard |
| 65 | title="Unsubscribe" |
| 66 | description={ |
| 67 | <> |
| 68 | Unsubscribe from {categoryName} emails? You'll stop receiving{' '} |
| 69 | {categoryName.toLowerCase()} emails sent to |
| 70 | <span className="">{email}</span> |
| 71 | </> |
| 72 | } |
| 73 | > |
| 74 | <div className="col gap-3"> |
| 75 | {error && ( |
| 76 | <div className="bg-destructive/10 text-destructive px-4 py-3 rounded-md text-sm"> |
| 77 | {error} |
| 78 | </div> |
| 79 | )} |
| 80 | <Button onClick={handleUnsubscribe} disabled={isUnsubscribing}> |
nothing calls this directly
no outgoing calls
no test coverage detected