-
-
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
14 changed files
with
152 additions
and
69 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,60 +1,62 @@ | ||
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 | ||
} | ||
} 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 }); | ||
} | ||
}, | ||
}), | ||
}; | ||
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, | ||
}; | ||
} 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 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
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,39 @@ | ||
--- | ||
import { generateBreadcrumbs } from './utils/generateBreadcrumbs'; | ||
const { pathname:currentPath }= Astro.url; | ||
const breadcrumbs = generateBreadcrumbs({ currentPath }); | ||
--- | ||
|
||
<div class="flex justify-center align-center"> | ||
{breadcrumbs?.map(({ link, label }) => { | ||
const isLast = breadcrumbs.at(-1).link === link | ||
|
||
return ( | ||
<> | ||
{!isLast ? ( | ||
<a href={link}>{label}</a> | ||
) : ( | ||
<span>{label}</span> | ||
)} | ||
{!isLast && " / "} | ||
</> | ||
); | ||
})} | ||
</div> | ||
|
||
<script type="application/ld+json" set:html={JSON.stringify({ | ||
"@context": "https://schema.org/", | ||
"@type": "BreadcrumbList", | ||
"itemListElement": breadcrumbs.map(({ link, label }, index) => { | ||
const isLast = breadcrumbs.at(-1).link === link | ||
|
||
return { | ||
"@type": "ListItem", | ||
"position": index + 1, | ||
"name": label, | ||
"item": isLast ? undefined : link | ||
}; | ||
}) | ||
})}/> |
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,5 @@ | ||
@layer breadcrumbs { | ||
.breadcrumbs{ | ||
gap:2rem; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/ui/components/molecules/breadcrumbs/utils/generateBreadcrumbs/generateBreadcrumbs.ts
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,19 @@ | ||
import { deSlugify } from '@shared/utils/deSlugify'; | ||
|
||
interface GenerateBreadcrumbsProps{ | ||
currentPath: string, | ||
} | ||
|
||
export const generateBreadcrumbs = ({ currentPath }: GenerateBreadcrumbsProps) => { | ||
const pathSegments = currentPath.split('/').filter(segment => segment.trim() !== ''); | ||
const breadcrumbs = pathSegments.map((_, index) => { | ||
const link = `/${pathSegments.slice(0, index + 1).join('/')}`; | ||
const label = deSlugify(pathSegments.at(index) ?? ''); | ||
|
||
return { label, link }; | ||
}); | ||
|
||
currentPath !== '/' && breadcrumbs.unshift({ label: "Home", link: "/" }); | ||
|
||
return breadcrumbs; | ||
} |
1 change: 1 addition & 0 deletions
1
src/ui/components/molecules/breadcrumbs/utils/generateBreadcrumbs/index.ts
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 @@ | ||
export * from './generateBreadcrumbs' |
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,5 @@ | ||
export function deSlugify(slug: string): string { | ||
return slug | ||
.replace(/-/g, ' ') | ||
.replace(/\b\w/g, match => match.toUpperCase()); | ||
} |
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 @@ | ||
export * from "./deSlugify"; |