-
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.
feat(contact): implemented contact page and form with server actions
- Loading branch information
1 parent
1307000
commit b0bc91c
Showing
20 changed files
with
439 additions
and
101 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
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 |
---|---|---|
|
@@ -51,4 +51,4 @@ | |
"slice-machine-ui": "1.23.0", | ||
"tailwindcss": "3.4.1" | ||
} | ||
} | ||
} |
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,143 @@ | ||
'use server'; | ||
|
||
import { | ||
ContactFormFields, | ||
ContactFormState, | ||
} from '@/components/forms/ContactForm'; | ||
|
||
const apiUrl = process.env.EMAIL_API_URL; | ||
const apiKey = process.env.EMAIL_API_KEY; | ||
|
||
type SenderOrRecipient = { | ||
name: string; | ||
email: string; | ||
}; | ||
|
||
type Attachment = ({ url: string } | { content: string }) & { | ||
name: string; | ||
}; | ||
|
||
type MessageBody = { | ||
sender: SenderOrRecipient; | ||
to: SenderOrRecipient[]; | ||
bcc?: SenderOrRecipient[]; | ||
cc?: SenderOrRecipient[]; | ||
replyTo?: SenderOrRecipient; | ||
subject: string; | ||
htmlContent: string; | ||
textContent: string; | ||
attachment?: Attachment[]; | ||
templateId?: number; | ||
params?: { [key: string]: string }; | ||
tags?: string[]; | ||
scheduledAt?: string; | ||
batchId?: string; | ||
}; | ||
|
||
type MessageResponseSuccess = { | ||
messageId: string; | ||
}; | ||
type MessageResponseScheduled = { | ||
messageId: string; | ||
batchId: string; | ||
}; | ||
type MessageResponseErrorCode = | ||
| 'invalid_parameter' | ||
| 'missing_parameter' | ||
| 'out_of_range' | ||
| 'campaign_processing' | ||
| 'campaign_sent' | ||
| 'document_not_found' | ||
| 'reseller_permission_denied' | ||
| 'not_enough_credits' | ||
| 'permission_denied' | ||
| 'duplicate_parameter' | ||
| 'duplicate_request' | ||
| 'method_not_allowed' | ||
| 'unauthorized' | ||
| 'account_under_validation' | ||
| 'not_acceptable' | ||
| 'bad_request'; | ||
type MessageResponseError = { | ||
message: string; | ||
code: MessageResponseErrorCode; | ||
}; | ||
type MessageResponse = | ||
| MessageResponseSuccess | ||
| MessageResponseScheduled | ||
| MessageResponseError; | ||
|
||
export async function sendMessage( | ||
_prevState: ContactFormState, | ||
formData: FormData, | ||
): Promise<ContactFormState> { | ||
const { | ||
firstName, | ||
lastName, | ||
email, | ||
company, | ||
website, | ||
description, | ||
}: ContactFormFields = { | ||
firstName: formData.get('firstName') as string, | ||
lastName: formData.get('lastName') as string, | ||
email: formData.get('email') as string, | ||
company: formData.get('company') as string, | ||
website: formData.get('website') as string, | ||
description: formData.get('description') as string, | ||
}; | ||
|
||
const messageBody: MessageBody = { | ||
sender: { | ||
name: 'charlesharwood.dev', | ||
email: 'no-reply@charlesharwood.dev', | ||
}, | ||
to: [ | ||
{ | ||
email: 'info@charlesharwood.dev', | ||
name: 'Charles Harwood', | ||
}, | ||
], | ||
replyTo: { | ||
email, | ||
name: `${firstName} ${lastName}`, | ||
}, | ||
subject: 'New enquiry from charlesharwood.dev', | ||
htmlContent: `<!DOCTYPE html><html><head></head><body><h1>You've received a new enquiry from https://charlesharwood.dev:</h1><p>First Name: ${firstName}</p><p>Last Name: ${lastName}</p><p>Email: ${email}</p><p>Company: ${company}</p><p>Website: ${website}</p><p>Message: ${description}</p></body></html>`, | ||
textContent: `First Name: ${firstName}\nLast Name: ${lastName}\nEmail: ${email}\nCompany: ${company}\nWebsite: ${website}\nMessage: ${description}`, | ||
}; | ||
|
||
const fetchOptions: RequestInit = { | ||
method: 'POST', | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
'api-key': apiKey, | ||
}, | ||
body: JSON.stringify(messageBody), | ||
}; | ||
|
||
try { | ||
const response = await fetch(apiUrl, fetchOptions); | ||
const data: MessageResponse = await response.json(); | ||
|
||
if ('code' in data) { | ||
throw new Error(data.message, { | ||
cause: data.code, | ||
}); | ||
} | ||
|
||
return { | ||
message: | ||
'Message sent successfully! I will be in touch as soon as I can.', | ||
success: true, | ||
}; | ||
} catch (error) { | ||
console.error(error); | ||
|
||
return { | ||
message: error instanceof Error ? error.message : 'Failed to send email', | ||
success: false, | ||
}; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { asText } from '@prismicio/client'; | ||
import { Metadata } from 'next'; | ||
import { notFound } from 'next/navigation'; | ||
|
||
import { ContactForm } from '@/components/forms/ContactForm'; | ||
import { Container } from '@/components/layout/Container'; | ||
import { Heading } from '@/components/typography/Heading'; | ||
import { createClient } from '@/prismic-config'; | ||
import { METADATA_BASE } from '@/utils/constants'; | ||
|
||
export default async function ContactPage() { | ||
const client = createClient(); | ||
const page = await client.getByUID('page', 'contact'); | ||
|
||
return ( | ||
<main> | ||
<Container as="section" yPadding="sm"> | ||
<Heading as="h1" className="mb-10"> | ||
{asText(page.data.title)} | ||
</Heading> | ||
<p> | ||
Please fill out the form below to send me a message about your | ||
project! | ||
</p> | ||
</Container> | ||
<Container as="section" yPadding="sm"> | ||
<ContactForm /> | ||
</Container> | ||
</main> | ||
); | ||
} | ||
|
||
export async function generateMetadata(): Promise<Metadata> { | ||
const client = createClient(); | ||
const page = await client.getByUID('page', 'contact').catch(notFound); | ||
const settings = await client.getSingle('settings'); | ||
|
||
return { | ||
metadataBase: METADATA_BASE, | ||
title: `${asText(page.data.title)} | ${asText(settings.data.siteTitle)}`, | ||
description: page.data.meta_description, | ||
openGraph: { | ||
title: page.data.meta_title, | ||
images: [ | ||
{ | ||
url: page.data.meta_image.url, | ||
}, | ||
], | ||
}, | ||
}; | ||
} |
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
Oops, something went wrong.