63 lines
1.9 KiB
TypeScript
63 lines
1.9 KiB
TypeScript
"use client";
|
|||
|
|
|
||
|
|
import { useRouter } from "next/navigation";
|
||
|
|
import { useState } from "react";
|
||
|
|
|
||
|
|
import { api, errorText } from "@/lib/api";
|
||
|
|
|
||
|
|
export function ProfileForm({ displayName, locale }: { displayName: string; locale: string }) {
|
||
|
|
const router = useRouter();
|
||
|
|
const [name, setName] = useState(displayName);
|
||
|
|
const [lang, setLang] = useState(locale);
|
||
|
|
const [status, setStatus] = useState<{ ok: boolean; text: string } | null>(null);
|
||
|
|
const [busy, setBusy] = useState(false);
|
||
|
|
|
||
|
|
async function save(e: React.FormEvent) {
|
||
|
|
e.preventDefault();
|
||
|
|
setBusy(true);
|
||
|
|
setStatus(null);
|
||
|
|
try {
|
||
|
|
await api("/me", { method: "PATCH", body: { display_name: name.trim(), locale: lang } });
|
||
|
|
setStatus({ ok: true, text: "Elmentve." });
|
||
|
|
router.refresh();
|
||
|
|
} catch (err) {
|
||
|
|
setStatus({ ok: false, text: errorText(err) });
|
||
|
|
} finally {
|
||
|
|
setBusy(false);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return (
|
||
|
|
<form onSubmit={save} className="field" style={{ gap: 16 }}>
|
||
|
|
<div className="field">
|
||
|
|
<label htmlFor="display-name">Megjelenített név</label>
|
||
|
|
<input
|
||
|
|
id="display-name"
|
||
|
|
className="inp"
|
||
|
|
value={name}
|
||
|
|
maxLength={80}
|
||
|
|
autoComplete="name"
|
||
|
|
onChange={(e) => setName(e.target.value)}
|
||
|
|
/>
|
||
|
|
</div>
|
||
|
|
<div className="field">
|
||
|
|
<label htmlFor="locale">Az elemzések nyelve</label>
|
||
|
|
<select id="locale" className="inp" value={lang} onChange={(e) => setLang(e.target.value)}>
|
||
|
|
<option value="hu">Magyar</option>
|
||
|
|
<option value="en">English</option>
|
||
|
|
</select>
|
||
|
|
</div>
|
||
|
|
<div className="form-actions">
|
||
|
|
<button type="submit" className="btn btn-primary" disabled={busy}>
|
||
|
|
Mentés
|
||
|
|
</button>
|
||
|
|
{status && (
|
||
|
|
<span className={status.ok ? "msg msg-ok" : "msg msg-error"} role="status">
|
||
|
|
{status.text}
|
||
|
|
</span>
|
||
|
|
)}
|
||
|
|
</div>
|
||
|
|
</form>
|
||
|
|
);
|
||
|
|
}
|