mirror of
https://github.com/kennethnym/aris.git
synced 2026-03-20 09:01:19 +00:00
feat(waitlist): handle duplicate emails and send confirmation
Co-authored-by: Ona <no-reply@ona.com>
This commit is contained in:
@@ -6,6 +6,7 @@ import { Streamdown } from "streamdown"
|
|||||||
|
|
||||||
import { ChatBox } from "~/chat/chat-box"
|
import { ChatBox } from "~/chat/chat-box"
|
||||||
import {
|
import {
|
||||||
|
duplicateEmailMessage,
|
||||||
INITLAL_MESSAGES,
|
INITLAL_MESSAGES,
|
||||||
waitListJoinedMessage,
|
waitListJoinedMessage,
|
||||||
type Message,
|
type Message,
|
||||||
@@ -45,6 +46,11 @@ export function meta({}: Route.MetaArgs) {
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const FormError = {
|
||||||
|
Duplicate: "duplicate",
|
||||||
|
Resend: "resend",
|
||||||
|
} as const
|
||||||
|
|
||||||
export async function action({ request }: Route.ActionArgs) {
|
export async function action({ request }: Route.ActionArgs) {
|
||||||
const formData = await request.formData()
|
const formData = await request.formData()
|
||||||
const email = formData.get("email")
|
const email = formData.get("email")
|
||||||
@@ -55,6 +61,13 @@ export async function action({ request }: Route.ActionArgs) {
|
|||||||
|
|
||||||
const resend = new Resend(process.env.RESEND_API_KEY)
|
const resend = new Resend(process.env.RESEND_API_KEY)
|
||||||
|
|
||||||
|
const dup = await resend.contacts.get({
|
||||||
|
email,
|
||||||
|
})
|
||||||
|
if (dup.data) {
|
||||||
|
return { error: FormError.Duplicate }
|
||||||
|
}
|
||||||
|
|
||||||
const res = await resend.contacts.create({
|
const res = await resend.contacts.create({
|
||||||
email,
|
email,
|
||||||
segments: [
|
segments: [
|
||||||
@@ -67,7 +80,20 @@ export async function action({ request }: Route.ActionArgs) {
|
|||||||
|
|
||||||
if (res.error) {
|
if (res.error) {
|
||||||
console.log("Error adding contact to Resend:", res.error)
|
console.log("Error adding contact to Resend:", res.error)
|
||||||
return { error: res.error.message }
|
return { error: FormError.Resend, message: res.error.message }
|
||||||
|
}
|
||||||
|
|
||||||
|
const emailRes = await resend.emails.send({
|
||||||
|
from: "Aelis <no-reply@ael.is>",
|
||||||
|
to: email,
|
||||||
|
template: {
|
||||||
|
id: "waitlist-confirmation",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
if (emailRes.error) {
|
||||||
|
// swallow the error since the user is already added to the waitlist, but log it for debugging
|
||||||
|
console.log("Error sending confirmation email:", emailRes.error)
|
||||||
}
|
}
|
||||||
|
|
||||||
return { email }
|
return { email }
|
||||||
@@ -85,8 +111,12 @@ export default function Home() {
|
|||||||
if (fetcher.data?.email && !isAnimatingSend) {
|
if (fetcher.data?.email && !isAnimatingSend) {
|
||||||
setMessages((messages) => [...messages, waitListJoinedMessage(fetcher.data.email)])
|
setMessages((messages) => [...messages, waitListJoinedMessage(fetcher.data.email)])
|
||||||
} else if (fetcher.data?.error) {
|
} else if (fetcher.data?.error) {
|
||||||
|
if (fetcher.data.error === FormError.Duplicate && !isAnimatingSend) {
|
||||||
|
setMessages((messages) => [...messages, duplicateEmailMessage()])
|
||||||
|
} else {
|
||||||
console.error(fetcher.data.error)
|
console.error(fetcher.data.error)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}, [fetcher.data?.email, fetcher.data?.error, isAnimatingSend])
|
}, [fetcher.data?.email, fetcher.data?.error, isAnimatingSend])
|
||||||
|
|
||||||
const insertEmailMessage = (email: string) => {
|
const insertEmailMessage = (email: string) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user