From 339c6957fe92c4ec1809159f09c55201d3794c18 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Fri, 6 May 2022 18:21:16 +0200 Subject: chore: add a Contact page --- src/pages/contact.tsx | 181 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 181 insertions(+) create mode 100644 src/pages/contact.tsx (limited to 'src/pages/contact.tsx') diff --git a/src/pages/contact.tsx b/src/pages/contact.tsx new file mode 100644 index 0000000..d88a8a1 --- /dev/null +++ b/src/pages/contact.tsx @@ -0,0 +1,181 @@ +import Notice, { NoticeKind } from '@components/atoms/layout/notice'; +import { BreadcrumbItem } from '@components/molecules/nav/breadcrumb'; +import ContactForm, { + ContactFormProps, +} from '@components/organisms/forms/contact-form'; +import SocialMedia from '@components/organisms/widgets/social-media'; +import PageLayout from '@components/templates/page/page-layout'; +import { meta } from '@content/pages/contact.mdx'; +import styles from '@styles/pages/contact.module.scss'; +import { sendMail } from '@services/graphql/contact'; +import { loadTranslation } from '@utils/helpers/i18n'; +import useSettings from '@utils/hooks/use-settings'; +import { GetStaticProps, NextPage } from 'next'; +import Head from 'next/head'; +import { useRouter } from 'next/router'; +import Script from 'next/script'; +import { useState } from 'react'; +import { useIntl } from 'react-intl'; +import { ContactPage as ContactPageSchema, Graph, WebPage } from 'schema-dts'; + +const ContactPage: NextPage = () => { + const { dates, intro, seo, title } = meta; + const intl = useIntl(); + const homeLabel = intl.formatMessage({ + defaultMessage: 'Home', + description: 'Breadcrumb: home label', + id: 'j5k9Fe', + }); + const breadcrumb: BreadcrumbItem[] = [ + { id: 'home', name: homeLabel, url: '/' }, + { id: 'contact', name: title, url: '/contact' }, + ]; + + const socialMediaTitle = intl.formatMessage({ + defaultMessage: 'Find me elsewhere', + description: 'ContactPage: social media widget title', + id: 'Qh2CwH', + }); + + const { website } = useSettings(); + const { asPath } = useRouter(); + const pageUrl = `${website.url}${asPath}`; + const pagePublicationDate = new Date(dates.publication); + const pageUpdateDate = new Date(dates.update); + + const webpageSchema: WebPage = { + '@id': `${pageUrl}`, + '@type': 'WebPage', + breadcrumb: { '@id': `${website.url}/#breadcrumb` }, + name: seo.title, + description: seo.description, + reviewedBy: { '@id': `${website.url}/#branding` }, + url: `${pageUrl}`, + isPartOf: { + '@id': `${website.url}`, + }, + }; + + const contactSchema: ContactPageSchema = { + '@id': `${website.url}/#contact`, + '@type': 'ContactPage', + name: title, + description: intro, + author: { '@id': `${website.url}/#branding` }, + creator: { '@id': `${website.url}/#branding` }, + dateCreated: pagePublicationDate.toISOString(), + dateModified: pageUpdateDate.toISOString(), + datePublished: pagePublicationDate.toISOString(), + editor: { '@id': `${website.url}/#branding` }, + inLanguage: website.locales.default, + license: 'https://creativecommons.org/licenses/by-sa/4.0/deed.fr', + mainEntityOfPage: { '@id': `${pageUrl}` }, + }; + + const schemaJsonLd: Graph = { + '@context': 'https://schema.org', + '@graph': [webpageSchema, contactSchema], + }; + + const widgets = [ + , + ]; + + const [status, setStatus] = useState('info'); + const [statusMessage, setStatusMessage] = useState(''); + + const submitMail: ContactFormProps['sendMail'] = async (data, reset) => { + const { email, message, name, subject } = data; + const messageHTML = message.replace(/\r?\n/g, '
'); + const body = `Message received from ${name} <${email}> on ${website.url}.

${messageHTML}`; + const replyTo = `${name} <${email}>`; + const mailData = { + body, + clientMutationId: 'contact', + replyTo, + subject, + }; + const { message: mutationMessage, sent } = await sendMail(mailData); + + if (sent) { + setStatus('success'); + setStatusMessage( + intl.formatMessage({ + defaultMessage: + 'Thanks. Your message was successfully sent. I will answer it as soon as possible.', + description: 'Contact: success message', + id: '3Pipok', + }) + ); + reset(); + } else { + const errorPrefix = intl.formatMessage({ + defaultMessage: 'An error occurred:', + description: 'Contact: error message', + id: 'TpyFZ6', + }); + const error = `${errorPrefix} ${mutationMessage}`; + setStatus('error'); + setStatusMessage(error); + } + }; + + return ( + <> + + {`${seo.title} - ${website.name}`} + + + + + + +