-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
432 additions
and
327 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { defineAction, z, ActionError } from "astro:actions"; | ||
import { DEFAULT_LOCALE_STRING } from '../consts.ts'; | ||
import { sendEmail } from '@server/email/server.ts'; | ||
import { app } from "@server/firebase/server.ts"; | ||
import { getFirestore } from "firebase-admin/firestore"; | ||
import type { FormData } from '@components/organisms/contactForm'; | ||
|
||
type ContactDetails = Omit<FormData, "recaptcha">; | ||
|
||
const contactFormSchema = z.object({ | ||
id: z.string(), | ||
name: z.string(), | ||
email: z.string().email(), | ||
message: z.string(), | ||
date: z.union([z.date(), z.string()]), | ||
}).omit({ id: true, date: true }); | ||
|
||
const database = getFirestore(app); | ||
|
||
export const server = { | ||
contact: defineAction({ | ||
accept: 'form', | ||
input: contactFormSchema, | ||
handler: async ({ name, email, message }: ContactDetails) => { | ||
try { | ||
const contactValidation = contactFormSchema.safeParse({ | ||
name, | ||
email, | ||
message | ||
}); | ||
if (!contactValidation.success) throw new Error(contactValidation.error?.errors.join(", ") || "Invalid data"); | ||
|
||
const { data } = contactValidation; | ||
const databaseRef = database.collection("contacts"); | ||
await databaseRef.add({ | ||
id: crypto.randomUUID(), | ||
name: data.name, | ||
email: data.email, | ||
message: data.message, | ||
date: new Date().toLocaleString(DEFAULT_LOCALE_STRING), | ||
}); | ||
const { data: emailData, error: emailError } = await sendEmail(data); | ||
if (emailError && !emailData) { | ||
throw new Error(`Something went wrong sending the email. Error: ${emailError.message} (${emailError.name})`); | ||
} | ||
|
||
return { | ||
ok: true | ||
} | ||
Check notice Code scanning / CodeQL Semicolon insertion Note
Avoid automated semicolon insertion (91% of all statements in
the enclosing function Error loading related location Loading |
||
} catch (error: unknown) { | ||
const actionError = error as ActionError; | ||
|
||
const message = actionError.message || "Something went wrong"; | ||
const code = actionError.status ?? 500; | ||
|
||
return new ActionError({ code, message }); | ||
} | ||
}, | ||
}), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.