49 lines
1.1 KiB
TypeScript
49 lines
1.1 KiB
TypeScript
"use client";
|
|||
|
|
|
||
|
|
import { useRouter } from "next/navigation";
|
||
|
|
import { useState } from "react";
|
||
|
|
|
||
|
|
import { api, errorText } from "@/lib/api";
|
||
|
|
|
||
|
|
/** Compares two charts (or opens their existing comparison). */
|
||
|
|
export function CompareButton({
|
||
|
|
chart1,
|
||
|
|
chart2,
|
||
|
|
label = "Összevetés velem",
|
||
|
|
className = "btn btn-sm btn-ghost",
|
||
|
|
}: {
|
||
|
|
chart1: string;
|
||
|
|
chart2: string;
|
||
|
|
label?: string;
|
||
|
|
className?: string;
|
||
|
|
}) {
|
||
|
|
const router = useRouter();
|
||
|
|
const [busy, setBusy] = useState(false);
|
||
|
|
const [error, setError] = useState<string | null>(null);
|
||
|
|
|
||
|
|
async function compare() {
|
||
|
|
setBusy(true);
|
||
|
|
setError(null);
|
||
|
|
try {
|
||
|
|
const s = await api<{ id: string }>("/synastries", { body: { chart1_id: chart1, chart2_id: chart2 } });
|
||
|
|
router.push(`/osszevetesek/${s.id}`);
|
||
|
|
} catch (e) {
|
||
|
|
setError(errorText(e));
|
||
|
|
setBusy(false);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return (
|
||
|
|
<>
|
||
|
|
<button type="button" className={className} onClick={compare} disabled={busy}>
|
||
|
|
{busy ? "Számolás…" : label}
|
||
|
|
</button>
|
||
|
|
{error && (
|
||
|
|
<p className="msg msg-error" role="alert">
|
||
|
|
{error}
|
||
|
|
</p>
|
||
|
|
)}
|
||
|
|
</>
|
||
|
|
);
|
||
|
|
}
|