60 lines
1.8 KiB
TypeScript
60 lines
1.8 KiB
TypeScript
"use client";
|
|||
|
|
|
||
|
|
import { useRouter } from "next/navigation";
|
||
|
|
import { useState } from "react";
|
||
|
|
|
||
|
|
import { api, errorText } from "@/lib/api";
|
||
|
|
|
||
|
|
/** Changes only a person's name (any time; the chart and analyses stay). */
|
||
|
|
export function RenamePerson({ id, name }: { id: string; name: string }) {
|
||
|
|
const router = useRouter();
|
||
|
|
const [value, setValue] = useState(name);
|
||
|
|
const [busy, setBusy] = useState(false);
|
||
|
|
const [error, setError] = useState<string | null>(null);
|
||
|
|
|
||
|
|
async function save(e: React.FormEvent) {
|
||
|
|
e.preventDefault();
|
||
|
|
if (!value.trim()) return setError("Add meg a nevét.");
|
||
|
|
setBusy(true);
|
||
|
|
setError(null);
|
||
|
|
try {
|
||
|
|
await api(`/charts/${id}`, { method: "PATCH", body: { label: value.trim() } });
|
||
|
|
router.push(`/kepletek/${id}`);
|
||
|
|
router.refresh();
|
||
|
|
} catch (err) {
|
||
|
|
setError(errorText(err));
|
||
|
|
setBusy(false);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return (
|
||
|
|
<form onSubmit={save} className="field" style={{ gap: 16 }} noValidate>
|
||
|
|
<div className="field" style={{ maxWidth: 420 }}>
|
||
|
|
<label htmlFor="rename">Név</label>
|
||
|
|
<input
|
||
|
|
id="rename"
|
||
|
|
className="inp"
|
||
|
|
value={value}
|
||
|
|
maxLength={100}
|
||
|
|
autoComplete="off"
|
||
|
|
onChange={(e) => setValue(e.target.value)}
|
||
|
|
required
|
||
|
|
/>
|
||
|
|
</div>
|
||
|
|
{error && (
|
||
|
|
<p className="msg msg-error" role="alert">
|
||
|
|
{error}
|
||
|
|
</p>
|
||
|
|
)}
|
||
|
|
<div className="form-actions">
|
||
|
|
<button type="submit" className="btn btn-primary" disabled={busy || value.trim() === name}>
|
||
|
|
{busy ? "Mentés…" : "Név mentése"}
|
||
|
|
</button>
|
||
|
|
<button type="button" className="btn btn-ghost" onClick={() => router.push(`/kepletek/${id}`)} disabled={busy}>
|
||
|
|
Mégse
|
||
|
|
</button>
|
||
|
|
</div>
|
||
|
|
</form>
|
||
|
|
);
|
||
|
|
}
|