150 lines
4.6 KiB
TypeScript
150 lines
4.6 KiB
TypeScript
"use client";
|
|||
|
|
|
||
|
|
import { useState } from "react";
|
||
|
|
|
||
|
|
import { api, errorText } from "@/lib/api";
|
||
|
|
|
||
|
|
const MAX_MESSAGE = 300;
|
||
|
|
|
||
|
|
interface Created {
|
||
|
|
url: string;
|
||
|
|
emailed: boolean;
|
||
|
|
email: string | null;
|
||
|
|
label: string | null;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Invites someone to compare charts instead of typing in their birth data:
|
||
|
|
* they enter their own data, and both see the comparison (not each other's
|
||
|
|
* birth data). No person slot is used.
|
||
|
|
*/
|
||
|
|
export function InvitePerson() {
|
||
|
|
const [label, setLabel] = useState("");
|
||
|
|
const [email, setEmail] = useState("");
|
||
|
|
const [message, setMessage] = useState("");
|
||
|
|
const [busy, setBusy] = useState(false);
|
||
|
|
const [error, setError] = useState<string | null>(null);
|
||
|
|
const [created, setCreated] = useState<Created | null>(null);
|
||
|
|
const [copied, setCopied] = useState(false);
|
||
|
|
|
||
|
|
async function submit(e: React.FormEvent) {
|
||
|
|
e.preventDefault();
|
||
|
|
setBusy(true);
|
||
|
|
setError(null);
|
||
|
|
try {
|
||
|
|
setCreated(
|
||
|
|
await api<Created>("/invites", {
|
||
|
|
body: { label: label.trim() || null, email: email.trim() || null, message: message.trim() || null },
|
||
|
|
}),
|
||
|
|
);
|
||
|
|
} catch (err) {
|
||
|
|
setError(errorText(err));
|
||
|
|
} finally {
|
||
|
|
setBusy(false);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async function copy() {
|
||
|
|
if (!created) return;
|
||
|
|
try {
|
||
|
|
await navigator.clipboard.writeText(created.url);
|
||
|
|
setCopied(true);
|
||
|
|
setTimeout(() => setCopied(false), 2000);
|
||
|
|
} catch {
|
||
|
|
setError("Nem sikerült másolni; jelöld ki a linket, és másold kézzel.");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (created) {
|
||
|
|
return (
|
||
|
|
<div className="field invite-done" style={{ gap: 12 }}>
|
||
|
|
<p className="msg msg-ok" role="status">
|
||
|
|
{created.emailed
|
||
|
|
? `Elküldtük a meghívót ide: ${created.email}. A linket te is továbbküldheted.`
|
||
|
|
: created.email
|
||
|
|
? `Ez a cím nem kér levelet tőlünk, ezért nem írtunk neki. Küldd el neki te a linket.`
|
||
|
|
: "Kész a meghívó. Küldd el a linket annak, akit meghívsz."}
|
||
|
|
</p>
|
||
|
|
<div className="share-link">
|
||
|
|
<input
|
||
|
|
className="inp"
|
||
|
|
readOnly
|
||
|
|
value={created.url}
|
||
|
|
aria-label="Meghívó link"
|
||
|
|
onFocus={(e) => e.currentTarget.select()}
|
||
|
|
/>
|
||
|
|
<button type="button" className="btn btn-sm" onClick={copy}>
|
||
|
|
{copied ? "Kimásolva" : "Másolás"}
|
||
|
|
</button>
|
||
|
|
</div>
|
||
|
|
<div className="form-actions">
|
||
|
|
<button type="button" className="btn btn-sm btn-ghost" onClick={() => setCreated(null)}>
|
||
|
|
Újabb meghívó
|
||
|
|
</button>
|
||
|
|
</div>
|
||
|
|
<p className="hint">
|
||
|
|
A meghívó 14 napig érvényes. Amikor elfogadja, levelet kapsz, és az összevetés megjelenik az Összevetéseid
|
||
|
|
között.
|
||
|
|
</p>
|
||
|
|
</div>
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
return (
|
||
|
|
<form className="field" style={{ gap: 14 }} onSubmit={submit}>
|
||
|
|
<div className="grid-2">
|
||
|
|
<div className="field">
|
||
|
|
<label htmlFor="invite-label">Kit hívsz meg?</label>
|
||
|
|
<input
|
||
|
|
id="invite-label"
|
||
|
|
className="inp"
|
||
|
|
value={label}
|
||
|
|
maxLength={80}
|
||
|
|
onChange={(e) => setLabel(e.target.value)}
|
||
|
|
placeholder="pl. Anna"
|
||
|
|
/>
|
||
|
|
<span className="hint">Csak te látod, a meghívóid között.</span>
|
||
|
|
</div>
|
||
|
|
<div className="field">
|
||
|
|
<label htmlFor="invite-email">E-mail-címe (nem kötelező)</label>
|
||
|
|
<input
|
||
|
|
id="invite-email"
|
||
|
|
className="inp"
|
||
|
|
type="email"
|
||
|
|
value={email}
|
||
|
|
onChange={(e) => setEmail(e.target.value)}
|
||
|
|
placeholder="anna@pelda.hu"
|
||
|
|
autoComplete="off"
|
||
|
|
/>
|
||
|
|
<span className="hint">Ha megadod, levélben is elküldjük neki.</span>
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
<div className="field">
|
||
|
|
<label htmlFor="invite-message">Üzenet (nem kötelező)</label>
|
||
|
|
<textarea
|
||
|
|
id="invite-message"
|
||
|
|
className="inp"
|
||
|
|
rows={3}
|
||
|
|
maxLength={MAX_MESSAGE}
|
||
|
|
value={message}
|
||
|
|
onChange={(e) => setMessage(e.target.value)}
|
||
|
|
placeholder="Szia! Nézzük meg, mennyire illünk össze."
|
||
|
|
/>
|
||
|
|
<span className="hint">
|
||
|
|
{message.length}/{MAX_MESSAGE} karakter, link nélkül.
|
||
|
|
</span>
|
||
|
|
</div>
|
||
|
|
<div className="form-actions">
|
||
|
|
<button type="submit" className="btn btn-primary" disabled={busy}>
|
||
|
|
{busy ? "Készül…" : "Meghívó készítése"}
|
||
|
|
</button>
|
||
|
|
</div>
|
||
|
|
{error && (
|
||
|
|
<p className="msg msg-error" role="alert">
|
||
|
|
{error}
|
||
|
|
</p>
|
||
|
|
)}
|
||
|
|
</form>
|
||
|
|
);
|
||
|
|
}
|