Skip to content

Commit

Permalink
feat: migrate to Astro actions
Browse files Browse the repository at this point in the history
  • Loading branch information
fbuireu committed May 14, 2024
1 parent 38828be commit 5845918
Show file tree
Hide file tree
Showing 9 changed files with 432 additions and 327 deletions.
Binary file modified .yarn/install-state.gz
Binary file not shown.
3 changes: 3 additions & 0 deletions astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import cloudflare from "@astrojs/cloudflare";
import million from "million/compiler";

export default defineConfig({
experimental: {
actions: true,
},
site: "https://biancafiore.me",
vite: {
ssr: {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"@fontsource/baskervville": "^5.0.20",
"@hookform/resolvers": "^3.3.4",
"astro": "^4.8.3",
"firebase": "^10.11.1",
"firebase": "^10.12.0",
"firebase-admin": "^12.1.0",
"gsap": "^3.12.5",
"markdown-it": "^14.1.0",
Expand All @@ -64,7 +64,7 @@
"react-hook-form": "^7.51.4",
"react-router-dom": "^6.23.1",
"resend": "^3.2.0",
"swiper": "^11.1.2",
"swiper": "^11.1.3",
"three": "^0.164.1",
"zod": "^3.23.8"
},
Expand Down
60 changes: 60 additions & 0 deletions src/actions/index.ts
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
have an explicit semicolon).
} 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 });
}
},
}),
};
1 change: 1 addition & 0 deletions src/env.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// <reference path="../.astro/actions.d.ts" />
/// <reference path="../.astro/types.d.ts" />
interface ImportMetaEnv {
readonly PUBLIC_GOOGLE_ANALYTICS_ID: string;
Expand Down
66 changes: 0 additions & 66 deletions src/pages/api/contact-form.ts

This file was deleted.

11 changes: 6 additions & 5 deletions src/ui/components/organisms/contactForm/ContactForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { CONTACT_FORM_REQUEST_PARAMETERS } from "src/consts.ts";
import { encode } from "@components/organisms/contactForm/utils/encode";
import { flyPlane } from "@components/organisms/contactForm/utils/flyPlane";
import Spinner from "@components/atoms/spinner/Spinner.tsx";
import { actions } from "astro:actions";

const schema = z.object({
name: z.string().trim().min(1, "Please insert your name"),
Expand Down Expand Up @@ -69,12 +70,12 @@ export const ContactForm = () => {

try {
setFormStatus(FormStatus.LOADING);
const requestParams: RequestInit = {
...CONTACT_FORM_REQUEST_PARAMETERS,
body: encode({ ...data }),
};
const contactData = new FormData();
contactData.append("name", data.name);
contactData.append("email", data.email);
contactData.append("message", data.message);

const response = await fetch(`/api/contact-form`, requestParams);
const response = await actions.contact(contactData);

if (response.ok) {
flyPlane(submitRef.current);
Expand Down
1 change: 1 addition & 0 deletions tsconfig.paths.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@server/*": ["./src/server/*"],
"@ui/*": ["./src/ui/*"],
"@components/*": ["./src/ui/components/*"],
"@utils/*": ["./src/utils/*"],
Expand Down
Loading

0 comments on commit 5845918

Please sign in to comment.