import { useState } from "react";
import { api } from "../api.js";
import { ErrorNote } from "../pages/widgets.jsx";
import { passwordProblem } from "./admin-core.js";
// A secret that is shown once: a one-time password or an invitation link.
export function OneTimeSecret({ title, value, hint, onDone }) {
const [copied, setCopied] = useState(false);
async function copy() {
try {
await navigator.clipboard.writeText(value);
setCopied(true);
} catch {
setCopied(false);
}
}
return (
);
}
export function PasswordForm({ onDone, submitLabel = "Passwort ändern" }) {
const [current, setCurrent] = useState("");
const [next, setNext] = useState("");
const [repeat, setRepeat] = useState("");
const [error, setError] = useState("");
const [pending, setPending] = useState(false);
async function submit(event) {
event.preventDefault();
const problem = passwordProblem(next, repeat, current);
if (problem) {
setError(problem);
return;
}
setPending(true);
setError("");
try {
await api("/api/v1/auth/password", { method: "POST", body: { current_password: current, new_password: next } });
setCurrent("");
setNext("");
setRepeat("");
await onDone?.();
} catch (err) {
setError(err.message);
} finally {
setPending(false);
}
}
return (
);
}