|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { ComponentProps, useState } from 'react'; |
| 4 | +import { Check } from '../../../modules/icons/Check'; |
| 5 | +import { Copy } from '../../../modules/icons/Copy'; |
| 6 | +import { ExternalLink } from '../../../modules/icons/ExternalLink'; |
| 7 | +import styles from './page.module.css'; |
| 8 | +import { mailTemplates } from '../../../data/mail-templates'; |
| 9 | + |
| 10 | +const TEMPLATES = Object.keys(mailTemplates); |
| 11 | + |
| 12 | +function buildParams(template: string, contactName: string, talkTitle: string): URLSearchParams { |
| 13 | + const params = new URLSearchParams({ template }); |
| 14 | + if (contactName) params.set('contactName', contactName); |
| 15 | + if (talkTitle) params.set('talkTitle', talkTitle); |
| 16 | + return params; |
| 17 | +} |
| 18 | + |
| 19 | +function toURI(map: Record<string, string>): string { |
| 20 | + return Object.entries(map) |
| 21 | + .map(([key, value]) => `${key}=${encodeURIComponent(value)}`) |
| 22 | + .join('&'); |
| 23 | +} |
| 24 | + |
| 25 | +function CopyableZone({ label, value, valueClassName }: { label: string; value: string; valueClassName?: string }) { |
| 26 | + const [copied, setCopied] = useState(false); |
| 27 | + const handleCopy = async () => { |
| 28 | + await navigator.clipboard.writeText(value); |
| 29 | + setCopied(true); |
| 30 | + setTimeout(() => setCopied(false), 2000); |
| 31 | + }; |
| 32 | + return ( |
| 33 | + <div className={styles.previewField}> |
| 34 | + <span className={styles.previewLabel}>{label}</span> |
| 35 | + <div className={styles.previewZone}> |
| 36 | + <pre className={valueClassName}>{value}</pre> |
| 37 | + <button type="button" className={styles.copyButton} onClick={handleCopy} aria-label={`Copier ${label}`}> |
| 38 | + {copied ? <Check size={14} /> : <Copy size={14} />} |
| 39 | + {copied ? ' Copié' : ' Copier'} |
| 40 | + </button> |
| 41 | + </div> |
| 42 | + </div> |
| 43 | + ); |
| 44 | +} |
| 45 | + |
| 46 | +export default function AdminMail() { |
| 47 | + const [template, setTemplate] = useState(TEMPLATES[0]); |
| 48 | + const [contactName, setContactName] = useState(''); |
| 49 | + const [talkTitle, setTalkTitle] = useState(''); |
| 50 | + const [email, setEmail] = useState(''); |
| 51 | + const [loading, setLoading] = useState(false); |
| 52 | + const [previewLoading, setPreviewLoading] = useState(false); |
| 53 | + const [subject, setSubject] = useState(''); |
| 54 | + const [body, setBody] = useState(''); |
| 55 | + const [showPreview, setShowPreview] = useState(false); |
| 56 | + |
| 57 | + const handlePreview = async () => { |
| 58 | + setPreviewLoading(true); |
| 59 | + const params = buildParams(template, contactName, talkTitle); |
| 60 | + const response = await fetch(`/api/mail?${params}`); |
| 61 | + const data = await response.json(); |
| 62 | + if (response.ok) { |
| 63 | + setSubject(data.subject); |
| 64 | + setBody(data.body); |
| 65 | + setShowPreview(true); |
| 66 | + } |
| 67 | + setPreviewLoading(false); |
| 68 | + }; |
| 69 | + |
| 70 | + const handleSubmit: ComponentProps<'form'>['onSubmit'] = async (e) => { |
| 71 | + e.preventDefault(); |
| 72 | + setLoading(true); |
| 73 | + const params = buildParams(template, contactName, talkTitle); |
| 74 | + const response = await fetch(`/api/mail?${params}`); |
| 75 | + const data = await response.json(); |
| 76 | + if (response.ok) { |
| 77 | + const mailToParams = { |
| 78 | + cc: 'contact@lyonjs.org', |
| 79 | + subject: data.subject, |
| 80 | + body: data.body, |
| 81 | + }; |
| 82 | + window.location.href = `mailto:${encodeURIComponent(email)}?${toURI(mailToParams)}`; |
| 83 | + } |
| 84 | + setLoading(false); |
| 85 | + }; |
| 86 | + |
| 87 | + return ( |
| 88 | + <main className={styles.container}> |
| 89 | + <h1>Générateur de mail</h1> |
| 90 | + <form onSubmit={handleSubmit} className={styles.form}> |
| 91 | + <div className={styles.field}> |
| 92 | + <label htmlFor="template">Template</label> |
| 93 | + <select id="template" value={template} onChange={(e) => setTemplate(e.target.value)}> |
| 94 | + {TEMPLATES.map((t) => ( |
| 95 | + <option key={t} value={t}> |
| 96 | + {t} |
| 97 | + </option> |
| 98 | + ))} |
| 99 | + </select> |
| 100 | + </div> |
| 101 | + |
| 102 | + <div className={styles.field}> |
| 103 | + <label htmlFor="email">Email du destinataire</label> |
| 104 | + <input id="email" type="email" value={email} onChange={(e) => setEmail(e.target.value)} required /> |
| 105 | + </div> |
| 106 | + |
| 107 | + <div className={styles.field}> |
| 108 | + <label htmlFor="contactName">Nom du contact (optionnel)</label> |
| 109 | + <input id="contactName" type="text" value={contactName} onChange={(e) => setContactName(e.target.value)} /> |
| 110 | + </div> |
| 111 | + |
| 112 | + <div className={styles.field}> |
| 113 | + <label htmlFor="talkTitle">Titre du talk (optionnel)</label> |
| 114 | + <input id="talkTitle" type="text" value={talkTitle} onChange={(e) => setTalkTitle(e.target.value)} /> |
| 115 | + </div> |
| 116 | + |
| 117 | + <div className={styles.formActions}> |
| 118 | + <button type="button" className={styles.buttonSecondary} onClick={handlePreview} disabled={previewLoading}> |
| 119 | + {previewLoading ? 'Chargement...' : 'Prévisualiser le mail'} |
| 120 | + </button> |
| 121 | + <button type="submit" className={styles.buttonIcon} disabled={loading} aria-label="Ouvrir le mail"> |
| 122 | + {loading ? ( |
| 123 | + '…' |
| 124 | + ) : ( |
| 125 | + <> |
| 126 | + Ouvrir le mail <ExternalLink size={16} /> |
| 127 | + </> |
| 128 | + )} |
| 129 | + </button> |
| 130 | + </div> |
| 131 | + </form> |
| 132 | + |
| 133 | + {showPreview && ( |
| 134 | + <section className={styles.previewSection} aria-label="Aperçu du mail"> |
| 135 | + <CopyableZone label="Objet" value={subject} valueClassName={styles.previewSubjectText} /> |
| 136 | + <CopyableZone label="Contenu" value={body} valueClassName={styles.previewBodyText} /> |
| 137 | + </section> |
| 138 | + )} |
| 139 | + </main> |
| 140 | + ); |
| 141 | +} |
0 commit comments