86 lines
2.8 KiB
TypeScript
86 lines
2.8 KiB
TypeScript
"use client";
|
|||
|
|
|
||
|
|
import Link from "next/link";
|
||
|
|
import { useCallback, useEffect, useState } from "react";
|
||
|
|
|
||
|
|
import { api, errorText } from "@/lib/api";
|
||
|
|
import { formatDate } from "@/lib/astro";
|
||
|
|
import type { PersonInvite } from "@/lib/types";
|
||
|
|
|
||
|
|
/** The user's "how compatible are we?" invitations: waiting ones (revocable) and accepted ones. */
|
||
|
|
export function PendingInvites() {
|
||
|
|
const [invites, setInvites] = useState<PersonInvite[] | null>(null);
|
||
|
|
const [error, setError] = useState<string | null>(null);
|
||
|
|
|
||
|
|
const load = useCallback(async () => {
|
||
|
|
try {
|
||
|
|
setInvites((await api<{ invites: PersonInvite[] }>("/invites")).invites);
|
||
|
|
} catch (e) {
|
||
|
|
setError(errorText(e));
|
||
|
|
}
|
||
|
|
}, []);
|
||
|
|
|
||
|
|
useEffect(() => {
|
||
|
|
load();
|
||
|
|
}, [load]);
|
||
|
|
|
||
|
|
async function revoke(i: PersonInvite) {
|
||
|
|
if (!window.confirm(`Visszavonod a meghívót${i.label ? ` (${i.label})` : ""}? A link nem fog működni.`)) return;
|
||
|
|
try {
|
||
|
|
await api(`/invites/${i.id}`, { method: "DELETE" });
|
||
|
|
await load();
|
||
|
|
} catch (e) {
|
||
|
|
setError(errorText(e));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Waiting ones, and the accepted ones of the last 30 days.
|
||
|
|
const recent = Date.now() - 30 * 86_400_000;
|
||
|
|
const shown = (invites ?? []).filter(
|
||
|
|
(i) => i.status === "pending" || (i.status === "accepted" && i.accepted_at && Date.parse(i.accepted_at) > recent),
|
||
|
|
);
|
||
|
|
if (!error && shown.length === 0) return null;
|
||
|
|
|
||
|
|
return (
|
||
|
|
<section className="card" aria-labelledby="invites-title" style={{ gap: 12 }}>
|
||
|
|
<h2 id="invites-title">Meghívóid</h2>
|
||
|
|
{error && (
|
||
|
|
<p className="msg msg-error" role="alert">
|
||
|
|
{error}
|
||
|
|
</p>
|
||
|
|
)}
|
||
|
|
<ul className="invite-list">
|
||
|
|
{shown.map((i) => (
|
||
|
|
<li key={i.id}>
|
||
|
|
<span className="invite-who">
|
||
|
|
<strong>{i.label ?? i.email ?? "Meghívó"}</strong>
|
||
|
|
{i.label && i.email && <span className="hint">{i.email}</span>}
|
||
|
|
</span>
|
||
|
|
{i.status === "pending" ? (
|
||
|
|
<>
|
||
|
|
<span className="hint">
|
||
|
|
Elküldve {formatDate(i.created_at)}, érvényes {formatDate(i.expires_at).replace(/\.$/, "")}-ig
|
||
|
|
</span>
|
||
|
|
<button type="button" className="btn btn-sm btn-ghost" onClick={() => revoke(i)}>
|
||
|
|
Visszavonás
|
||
|
|
</button>
|
||
|
|
</>
|
||
|
|
) : (
|
||
|
|
<>
|
||
|
|
<span className="hint">
|
||
|
|
Elfogadta{i.accepted_by ? `: ${i.accepted_by}` : ""} · {formatDate(i.accepted_at)}
|
||
|
|
</span>
|
||
|
|
{i.synastry_id && (
|
||
|
|
<Link href={`/osszevetesek/${i.synastry_id}`} className="btn btn-sm">
|
||
|
|
Összevetés
|
||
|
|
</Link>
|
||
|
|
)}
|
||
|
|
</>
|
||
|
|
)}
|
||
|
|
</li>
|
||
|
|
))}
|
||
|
|
</ul>
|
||
|
|
</section>
|
||
|
|
);
|
||
|
|
}
|