implement blog subscription feature with modal and email handling
This commit is contained in:
@@ -1,17 +1,17 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { useRef, useEffect } from 'react';
|
import { useRef, useEffect, useState } from 'react';
|
||||||
import Link from 'next/link';
|
import { BlogSubFormModal } from '@/components/ui/BlogSubFormModal';
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
locale: string;
|
|
||||||
title: string;
|
title: string;
|
||||||
text: string;
|
text: string;
|
||||||
cta: string;
|
cta: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export function BlogCTASection({ locale, title, text, cta }: Props) {
|
export function BlogCTASection({ title, text, cta }: Props) {
|
||||||
const sectionRef = useRef<HTMLDivElement>(null);
|
const sectionRef = useRef<HTMLDivElement>(null);
|
||||||
|
const [modalOpen, setModalOpen] = useState(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const section = sectionRef.current;
|
const section = sectionRef.current;
|
||||||
@@ -36,9 +36,10 @@ export function BlogCTASection({ locale, title, text, cta }: Props) {
|
|||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div ref={sectionRef} className="w-full flex justify-center items-center pt-24 md:pt-36">
|
<>
|
||||||
|
<div ref={sectionRef} className="pt-24 md:pt-36 px-8 md:px-14 lg:px-20 max-w-275 mx-auto">
|
||||||
<div
|
<div
|
||||||
className="w-full max-w-2xl reveal flex flex-col items-start gap-8 p-10 md:p-14 rounded-2xl border border-brand-brown/8 bg-white/40 transition-all duration-300 cursor-default"
|
className="w-full reveal flex flex-col items-start gap-8 p-10 md:p-14 rounded-2xl border border-brand-brown/8 bg-white/40 transition-all duration-300 cursor-default"
|
||||||
style={{ transitionDelay: `120ms` }}
|
style={{ transitionDelay: `120ms` }}
|
||||||
>
|
>
|
||||||
<div>
|
<div>
|
||||||
@@ -51,7 +52,10 @@ export function BlogCTASection({ locale, title, text, cta }: Props) {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="w-full flex flex-row justify-end">
|
<div className="w-full flex flex-row justify-end">
|
||||||
<Link href={`/${locale}/freelance`} className="group flex items-center gap-2 font-sans text-xs uppercase tracking-[0.18em] text-brand-brown border-b border-brand-brown/30 pb-0.5 hover:border-brand-brown transition-colors duration-200">
|
<button
|
||||||
|
onClick={() => setModalOpen(true)}
|
||||||
|
className="group flex items-center gap-2 font-sans text-xs uppercase tracking-[0.18em] text-brand-brown border-b border-brand-brown/30 pb-0.5 hover:border-brand-brown transition-colors duration-200 cursor-pointer"
|
||||||
|
>
|
||||||
{cta}
|
{cta}
|
||||||
<svg
|
<svg
|
||||||
className="w-3.5 h-3.5 group-hover:translate-x-1.5 transition-transform duration-200"
|
className="w-3.5 h-3.5 group-hover:translate-x-1.5 transition-transform duration-200"
|
||||||
@@ -62,9 +66,12 @@ export function BlogCTASection({ locale, title, text, cta }: Props) {
|
|||||||
>
|
>
|
||||||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.5} d="M17 8l4 4m0 0l-4 4m4-4H3" />
|
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.5} d="M17 8l4 4m0 0l-4 4m4-4H3" />
|
||||||
</svg>
|
</svg>
|
||||||
</Link>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<BlogSubFormModal isOpen={modalOpen} onClose={() => setModalOpen(false)} />
|
||||||
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import { BlogHero } from "@/app/[locale]/blog/_components/BlogHero";
|
|||||||
import { BlogCTASection } from "@/app/[locale]/blog/_components/BlogCTASection";
|
import { BlogCTASection } from "@/app/[locale]/blog/_components/BlogCTASection";
|
||||||
import { ArticleList } from "@/app/[locale]/blog/[slug]/_components/ArticleList";
|
import { ArticleList } from "@/app/[locale]/blog/[slug]/_components/ArticleList";
|
||||||
import { getTranslations } from "next-intl/server";
|
import { getTranslations } from "next-intl/server";
|
||||||
import { BlogSubFormController } from '@/components/ui/BlogSubFormController';
|
|
||||||
|
|
||||||
type props = {
|
type props = {
|
||||||
params: Promise<{ locale: string }>;
|
params: Promise<{ locale: string }>;
|
||||||
@@ -16,12 +15,9 @@ export default async function BlogPage({ params }: props) {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<main className="bg-brand-beige">
|
<main className="bg-brand-beige">
|
||||||
<BlogSubFormController />
|
|
||||||
|
|
||||||
<BlogHero />
|
<BlogHero />
|
||||||
|
|
||||||
<BlogCTASection
|
<BlogCTASection
|
||||||
locale={locale}
|
|
||||||
title={t("title")}
|
title={t("title")}
|
||||||
text={t("text")}
|
text={t("text")}
|
||||||
cta={t("cta")}
|
cta={t("cta")}
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
'use server';
|
||||||
|
|
||||||
|
import { addContact } from '@/lib/mail';
|
||||||
|
|
||||||
|
export type SubscribeState = { success: true } | { success: false; error: string } | null;
|
||||||
|
|
||||||
|
export async function subscribeNewsletter(
|
||||||
|
_: SubscribeState,
|
||||||
|
formData: FormData,
|
||||||
|
): Promise<SubscribeState> {
|
||||||
|
const email = (formData.get('email') as string)?.trim();
|
||||||
|
|
||||||
|
if (!email) return { success: false, error: "L'adresse email est requise." };
|
||||||
|
|
||||||
|
const emailRe = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
||||||
|
if (!emailRe.test(email)) {
|
||||||
|
return { success: false, error: 'Adresse email invalide.' };
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await addContact(email);
|
||||||
|
return { success: true };
|
||||||
|
} catch (err) {
|
||||||
|
return { success: false, error: 'Une erreur est survenue. Réessaie plus tard.' };
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,65 +2,47 @@
|
|||||||
|
|
||||||
import { useEffect, useRef, useState, useActionState } from 'react';
|
import { useEffect, useRef, useState, useActionState } from 'react';
|
||||||
import { createPortal } from 'react-dom';
|
import { createPortal } from 'react-dom';
|
||||||
import { sendContactMail, type ContactFormState } from '@/app/actions/sendContactMail';
|
import { subscribeNewsletter, type SubscribeState } from '@/app/actions/subscribeNewsletter';
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
isOpen: boolean;
|
isOpen: boolean;
|
||||||
onClose: () => void;
|
onClose: () => void;
|
||||||
};
|
};
|
||||||
|
|
||||||
type FieldProps = {
|
function EmailField({ inputRef }: { inputRef: React.RefObject<HTMLInputElement | null> }) {
|
||||||
label: string;
|
|
||||||
name: string;
|
|
||||||
type?: string;
|
|
||||||
required?: boolean;
|
|
||||||
textarea?: boolean;
|
|
||||||
inputRef?: React.RefObject<HTMLInputElement | null>;
|
|
||||||
};
|
|
||||||
|
|
||||||
function Field({ label, name, type = 'text', required, textarea, inputRef }: FieldProps) {
|
|
||||||
const base =
|
const base =
|
||||||
'w-full font-sans text-sm text-brand-brown bg-white/60 border border-brand-brown/12 rounded-lg px-4 py-3 placeholder:text-brand-brown/25 focus:outline-none focus:border-brand-brown/35 focus:bg-white/80 transition-colors duration-150';
|
'w-full font-sans text-sm text-brand-brown bg-white/60 border border-brand-brown/12 rounded-lg px-4 py-3 placeholder:text-brand-brown/25 focus:outline-none focus:border-brand-brown/35 focus:bg-white/80 transition-colors duration-150';
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-col gap-1.5">
|
<div className="flex flex-col gap-1.5">
|
||||||
<label className="font-sans text-[11px] uppercase tracking-[0.18em] text-brand-brown/45">
|
<label className="font-sans text-[11px] uppercase tracking-[0.18em] text-brand-brown/45">
|
||||||
{label}
|
Email
|
||||||
</label>
|
</label>
|
||||||
{textarea ? (
|
|
||||||
<textarea
|
|
||||||
name={name}
|
|
||||||
required={required}
|
|
||||||
rows={5}
|
|
||||||
className={`${base} resize-none`}
|
|
||||||
/>
|
|
||||||
) : (
|
|
||||||
<input
|
<input
|
||||||
ref={inputRef as React.RefObject<HTMLInputElement>}
|
ref={inputRef}
|
||||||
name={name}
|
name="email"
|
||||||
type={type}
|
type="email"
|
||||||
required={required}
|
required
|
||||||
className={base}
|
className={base}
|
||||||
/>
|
/>
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function BlogSubFormModal({ isOpen, onClose }: Props) {
|
export function BlogSubFormModal({ isOpen, onClose }: Props) {
|
||||||
const [mounted, setMounted] = useState(false);
|
const [mounted, setMounted] = useState(false);
|
||||||
const [state, action, pending] = useActionState<ContactFormState, FormData>(
|
const [state, action, pending] = useActionState<SubscribeState, FormData>(
|
||||||
sendContactMail,
|
subscribeNewsletter,
|
||||||
null,
|
null,
|
||||||
);
|
);
|
||||||
const firstInputRef = useRef<HTMLInputElement>(null);
|
const emailInputRef = useRef<HTMLInputElement>(null);
|
||||||
const formRef = useRef<HTMLFormElement>(null);
|
const formRef = useRef<HTMLFormElement>(null);
|
||||||
|
|
||||||
useEffect(() => setMounted(true), []);
|
useEffect(() => setMounted(true), []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (isOpen) {
|
if (isOpen) {
|
||||||
setTimeout(() => firstInputRef.current?.focus(), 50);
|
setTimeout(() => emailInputRef.current?.focus(), 50);
|
||||||
document.body.style.overflow = 'hidden';
|
document.body.style.overflow = 'hidden';
|
||||||
} else {
|
} else {
|
||||||
document.body.style.overflow = '';
|
document.body.style.overflow = '';
|
||||||
@@ -83,23 +65,20 @@ export function BlogSubFormModal({ isOpen, onClose }: Props) {
|
|||||||
return createPortal(
|
return createPortal(
|
||||||
<div className="fixed inset-0 z-50 flex items-center justify-center p-4" role="dialog" aria-modal="true">
|
<div className="fixed inset-0 z-50 flex items-center justify-center p-4" role="dialog" aria-modal="true">
|
||||||
|
|
||||||
{/* Backdrop */}
|
|
||||||
<div
|
<div
|
||||||
className="absolute inset-0 bg-brand-brown/40 backdrop-blur-sm"
|
className="absolute inset-0 bg-brand-brown/40 backdrop-blur-sm"
|
||||||
onClick={onClose}
|
onClick={onClose}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{/* Panel */}
|
|
||||||
<div className="relative w-full max-w-lg bg-brand-beige rounded-2xl shadow-2xl flex flex-col gap-7 p-8 md:p-10">
|
<div className="relative w-full max-w-lg bg-brand-beige rounded-2xl shadow-2xl flex flex-col gap-7 p-8 md:p-10">
|
||||||
|
|
||||||
{/* Header */}
|
|
||||||
<div className="flex items-start justify-between gap-4">
|
<div className="flex items-start justify-between gap-4">
|
||||||
<div>
|
<div>
|
||||||
<h2 className="font-serif text-brand-brown text-[clamp(1.3rem,2vw,1.65rem)] leading-tight">
|
<h2 className="font-serif text-brand-brown text-[clamp(1.3rem,2vw,1.65rem)] leading-tight">
|
||||||
Abonnez-vous au blog
|
Ne ratez aucun article
|
||||||
</h2>
|
</h2>
|
||||||
<p className="font-sans text-sm text-brand-brown/45 mt-1.5">
|
<p className="font-sans text-sm text-brand-brown/45 mt-1.5">
|
||||||
Recevez une alerte par mail quand un nouvel article est publie. C'est gratuit.
|
Un mail à chaque nouvel article. Pas de spam, désabonnement en un clic.
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<button
|
<button
|
||||||
@@ -113,11 +92,12 @@ export function BlogSubFormModal({ isOpen, onClose }: Props) {
|
|||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Success */}
|
|
||||||
{state?.success ? (
|
{state?.success ? (
|
||||||
<div className="flex flex-col items-center gap-3 py-10 text-center">
|
<div className="flex flex-col items-center gap-3 py-10 text-center">
|
||||||
<span className="font-serif text-brand-brown text-xl">C'est note !</span>
|
<span className="font-serif text-brand-brown text-xl">C'est noté !</span>
|
||||||
<p className="font-sans text-sm text-brand-brown/45">Vous devriez recevoir un mail de confirmation d'ici peu.</p>
|
<p className="font-sans text-sm text-brand-brown/45">
|
||||||
|
Vous recevrez un email à chaque nouvel article.
|
||||||
|
</p>
|
||||||
<button
|
<button
|
||||||
onClick={onClose}
|
onClick={onClose}
|
||||||
className="mt-4 font-sans text-xs uppercase tracking-[0.18em] text-brand-brown/40 hover:text-brand-brown border-b border-brand-brown/20 hover:border-brand-brown pb-0.5 transition-colors duration-150 cursor-pointer"
|
className="mt-4 font-sans text-xs uppercase tracking-[0.18em] text-brand-brown/40 hover:text-brand-brown border-b border-brand-brown/20 hover:border-brand-brown pb-0.5 transition-colors duration-150 cursor-pointer"
|
||||||
@@ -127,9 +107,7 @@ export function BlogSubFormModal({ isOpen, onClose }: Props) {
|
|||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
<form ref={formRef} action={action} className="flex flex-col gap-5">
|
<form ref={formRef} action={action} className="flex flex-col gap-5">
|
||||||
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
<EmailField inputRef={emailInputRef} />
|
||||||
<Field label="Email" name="email" type="email" required />
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{state?.error && (
|
{state?.error && (
|
||||||
<p className="font-sans text-xs text-red-700/80">{state.error}</p>
|
<p className="font-sans text-xs text-red-700/80">{state.error}</p>
|
||||||
@@ -141,7 +119,7 @@ export function BlogSubFormModal({ isOpen, onClose }: Props) {
|
|||||||
disabled={pending}
|
disabled={pending}
|
||||||
className="inline-flex items-center gap-2.5 font-sans text-[13px] tracking-wide text-brand-beige bg-brand-brown rounded-lg px-7 py-3 hover:bg-brand-brown/85 disabled:opacity-50 transition-colors duration-200 cursor-pointer disabled:cursor-default"
|
className="inline-flex items-center gap-2.5 font-sans text-[13px] tracking-wide text-brand-beige bg-brand-brown rounded-lg px-7 py-3 hover:bg-brand-brown/85 disabled:opacity-50 transition-colors duration-200 cursor-pointer disabled:cursor-default"
|
||||||
>
|
>
|
||||||
{pending ? 'Envoi…' : 'Envoyer'}
|
{pending ? 'Inscription…' : "S'abonner"}
|
||||||
{!pending && (
|
{!pending && (
|
||||||
<svg className="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24" aria-hidden="true">
|
<svg className="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24" aria-hidden="true">
|
||||||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.5} d="M17 8l4 4m0 0l-4 4m4-4H3" />
|
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.5} d="M17 8l4 4m0 0l-4 4m4-4H3" />
|
||||||
|
|||||||
+16
@@ -25,3 +25,19 @@ export async function sendMail({ subject, html, from }: SendMailOptions) {
|
|||||||
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function addContact(email: string) {
|
||||||
|
const audienceId = process.env.RESEND_AUDIENCE_ID;
|
||||||
|
|
||||||
|
if (!audienceId) throw new Error('RESEND_AUDIENCE_ID is not set');
|
||||||
|
|
||||||
|
const { data, error } = await resend.contacts.create({
|
||||||
|
email,
|
||||||
|
audienceId,
|
||||||
|
unsubscribed: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (error) throw error;
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|||||||
+3
-3
@@ -72,9 +72,9 @@
|
|||||||
"subtitle": "Thoughts on architecture, web development, and the practices that make products last."
|
"subtitle": "Thoughts on architecture, web development, and the practices that make products last."
|
||||||
},
|
},
|
||||||
"blogSubscription": {
|
"blogSubscription": {
|
||||||
"title": "Working on something ambitious?",
|
"title": "Never miss an article",
|
||||||
"text": "I'm available for freelance projects — web apps, architecture, and product development.",
|
"text": "Get an email whenever a new article is published. No spam — unsubscribe anytime.",
|
||||||
"cta": "See my services"
|
"cta": "Subscribe"
|
||||||
},
|
},
|
||||||
"backToBlog": "Back to blog",
|
"backToBlog": "Back to blog",
|
||||||
"scrollPrompt": "Scroll",
|
"scrollPrompt": "Scroll",
|
||||||
|
|||||||
+3
-3
@@ -72,9 +72,9 @@
|
|||||||
"subtitle": "Pensées sur l'architecture, le développement web et les pratiques qui font durer les produits."
|
"subtitle": "Pensées sur l'architecture, le développement web et les pratiques qui font durer les produits."
|
||||||
},
|
},
|
||||||
"blogSubscription": {
|
"blogSubscription": {
|
||||||
"title": "Vous travaillez sur un projet ambitieux ?",
|
"title": "Ne ratez aucun article",
|
||||||
"text": "Je suis disponible pour des missions freelance — applications web, architecture et développement produit.",
|
"text": "Un email à chaque nouvel article. Pas de spam, désabonnement en un clic.",
|
||||||
"cta": "Voir mes services"
|
"cta": "S'abonner"
|
||||||
},
|
},
|
||||||
"backToBlog": "Retour au blog",
|
"backToBlog": "Retour au blog",
|
||||||
"scrollPrompt": "Défiler",
|
"scrollPrompt": "Défiler",
|
||||||
|
|||||||
Reference in New Issue
Block a user