|
1 | 1 | import * as Popover from '@radix-ui/react-popover'; |
2 | | -import * as React from 'react'; |
| 2 | +import { useId, useState } from 'react'; |
3 | 3 | import { toast } from 'sonner'; |
| 4 | +import { sendTestEmail } from '../actions/send-test-email'; |
4 | 5 | import { Button } from './button'; |
5 | 6 | import { Text } from './text'; |
6 | 7 |
|
7 | 8 | export const Send = ({ markup }: { markup: string }) => { |
8 | | - const [to, setTo] = React.useState(''); |
9 | | - const [subject, setSubject] = React.useState('Testing React Email'); |
10 | | - const [isSending, setIsSending] = React.useState(false); |
11 | | - const [isPopOverOpen, setIsPopOverOpen] = React.useState(false); |
| 9 | + const [to, setTo] = useState(''); |
| 10 | + const [subject, setSubject] = useState('Testing React Email'); |
| 11 | + const [isSending, setIsSending] = useState(false); |
| 12 | + const [isPopOverOpen, setIsPopOverOpen] = useState(false); |
12 | 13 |
|
13 | 14 | const onFormSubmit = async (e: React.FormEvent) => { |
14 | | - try { |
15 | | - e.preventDefault(); |
16 | | - setIsSending(true); |
| 15 | + e.preventDefault(); |
| 16 | + setIsSending(true); |
17 | 17 |
|
18 | | - const response = await fetch('https://react.email/api/send/test', { |
19 | | - method: 'POST', |
20 | | - headers: { 'Content-Type': 'application/json' }, |
21 | | - body: JSON.stringify({ |
22 | | - to, |
23 | | - subject, |
24 | | - html: markup, |
25 | | - }), |
26 | | - }); |
| 18 | + const response = await sendTestEmail(to, subject, markup); |
27 | 19 |
|
28 | | - if (response.ok) { |
29 | | - toast.success('Email sent! Check your inbox.'); |
30 | | - } else { |
31 | | - if (response.status === 429) { |
32 | | - const { error } = (await response.json()) as { error: string }; |
33 | | - toast.error(error); |
34 | | - } else { |
35 | | - toast.error('Something went wrong. Please try again.'); |
36 | | - } |
37 | | - } |
38 | | - } catch (_exception) { |
| 20 | + if (response.ok) { |
| 21 | + toast.success('Email sent! Check your inbox.'); |
| 22 | + } else if (response.status === 429) { |
| 23 | + toast.error('Too many requests. Try again in around 1 minute'); |
| 24 | + } else { |
39 | 25 | toast.error('Something went wrong. Please try again.'); |
40 | | - } finally { |
41 | | - setIsSending(false); |
42 | 26 | } |
| 27 | + |
| 28 | + setIsSending(false); |
43 | 29 | }; |
44 | 30 |
|
45 | | - const toId = React.useId(); |
46 | | - const subjectId = React.useId(); |
| 31 | + const toId = useId(); |
| 32 | + const subjectId = useId(); |
47 | 33 |
|
48 | 34 | return ( |
49 | 35 | <Popover.Root |
@@ -114,20 +100,26 @@ export const Send = ({ markup }: { markup: string }) => { |
114 | 100 | type="checkbox" |
115 | 101 | /> |
116 | 102 | <div className="mt-3 flex items-center justify-between"> |
117 | | - <Text className="inline-block" size="1"> |
118 | | - Powered by{' '} |
119 | | - <a |
120 | | - className="text-white/85 transition duration-300 ease-in-out hover:text-slate-12" |
121 | | - href="https://go.resend.com/react-email" |
122 | | - rel="noreferrer" |
123 | | - target="_blank" |
124 | | - > |
125 | | - Resend |
126 | | - </a> |
127 | | - </Text> |
| 103 | + <div className="flex flex-col inline-block"> |
| 104 | + <Text size="1"> |
| 105 | + Powered by{' '} |
| 106 | + <a |
| 107 | + className="text-white/85 transition duration-300 ease-in-out hover:text-slate-12" |
| 108 | + href="https://go.resend.com/react-email" |
| 109 | + rel="noreferrer" |
| 110 | + target="_blank" |
| 111 | + > |
| 112 | + Resend |
| 113 | + </a> |
| 114 | + </Text> |
| 115 | + </div> |
128 | 116 | <Button |
129 | 117 | className="disabled:border-transparent disabled:bg-slate-11" |
130 | | - disabled={subject.length === 0 || to.length === 0 || isSending} |
| 118 | + disabled={ |
| 119 | + subject.length === 0 || |
| 120 | + to.length === 0 || |
| 121 | + isSending |
| 122 | + } |
131 | 123 | type="submit" |
132 | 124 | > |
133 | 125 | Send |
|
0 commit comments