Skip to content

Commit 95367ec

Browse files
committed
feat: Use server actions instead of API
1 parent 06ceb8b commit 95367ec

File tree

2 files changed

+20
-50
lines changed

2 files changed

+20
-50
lines changed

app/api/send/route.ts

-27
This file was deleted.

app/page.tsx

+20-23
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
1-
'use client';
1+
import { VercelInviteUserEmail } from '../emails/vercel-invite-user';
2+
import { Resend } from 'resend';
23

34
export default function Page() {
4-
const onSubmit = async (e) => {
5-
e.preventDefault();
6-
const email = e.target.email.value;
5+
async function send(formData: FormData) {
6+
'use server';
77

8-
try {
9-
const response = await fetch('/api/send', {
10-
method: 'POST',
11-
headers: { 'Content-Type': 'application/json' },
12-
body: JSON.stringify({ email }),
13-
});
8+
const resend = new Resend(process.env.RESEND_API_KEY);
9+
const email = formData.get('email') as string;
1410

15-
if (!response.ok) {
16-
alert('Failed to send email. Try again later.');
17-
return;
18-
}
11+
const { data, error } = await resend.emails.send({
12+
from: 'Vercel <[email protected]>',
13+
to: [email],
14+
subject: 'Join team on Vercel',
15+
react: VercelInviteUserEmail({}),
16+
});
1917

20-
const json = await response.json();
21-
alert(`Email sent: ${json.data.id}`);
18+
if (error) {
19+
console.log(error);
2220
}
23-
catch(error) {
24-
console.error(error);
25-
}
26-
};
21+
22+
console.log(data);
23+
}
2724

2825
return (
2926
<div className="bg-zinc-950 py-16 sm:py-24 h-[100vh]">
@@ -35,12 +32,12 @@ export default function Page() {
3532
<p className="mx-auto mt-2 max-w-xl text-center text-lg leading-8 text-gray-300">
3633
Type your email address to get invited to a team.
3734
</p>
38-
<form className="mx-auto mt-10 flex max-w-md gap-x-4" onSubmit={onSubmit}>
39-
<label htmlFor="email-address" className="sr-only">
35+
<form className="mx-auto mt-10 flex max-w-md gap-x-4" action={send}>
36+
<label htmlFor="email" className="sr-only">
4037
Email address
4138
</label>
4239
<input
43-
id="email-address"
40+
id="email"
4441
name="email"
4542
type="email"
4643
required

0 commit comments

Comments
 (0)