72 lines
2.3 KiB
TypeScript
72 lines
2.3 KiB
TypeScript
"use client";
|
|
|
|
import { useState, useTransition } from "react";
|
|
|
|
import type { EventResponseStatus } from "@/modules/calendar/service";
|
|
|
|
import type { CalendarActionResult } from "./actions";
|
|
|
|
export function EventRsvpButtons({
|
|
action,
|
|
eventId,
|
|
initialResponse,
|
|
}: Readonly<{
|
|
action: (state: CalendarActionResult, formData: FormData) => Promise<CalendarActionResult>;
|
|
eventId: string;
|
|
initialResponse: EventResponseStatus | null;
|
|
}>) {
|
|
const [response, setResponse] = useState<EventResponseStatus | null>(initialResponse);
|
|
const [error, setError] = useState("");
|
|
const [pending, startTransition] = useTransition();
|
|
|
|
const respond = (status: EventResponseStatus) => {
|
|
const formData = new FormData();
|
|
formData.set("eventId", eventId);
|
|
formData.set("status", status);
|
|
startTransition(async () => {
|
|
const result = await action({ ok: true, message: "" }, formData);
|
|
if (result.ok) {
|
|
setResponse(status);
|
|
setError("");
|
|
} else {
|
|
setError(result.message);
|
|
}
|
|
});
|
|
};
|
|
|
|
const base =
|
|
"min-h-11 rounded-md border px-4 text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-primary-500 disabled:cursor-not-allowed disabled:opacity-50";
|
|
const active = "border-primary-600 bg-primary-600 text-white";
|
|
const inactive = "border-border bg-background hover:bg-accent";
|
|
|
|
return (
|
|
<div className="flex flex-col gap-1">
|
|
<div className="flex flex-wrap gap-2" role="group" aria-label="Rückmeldung zu diesem Termin">
|
|
<button
|
|
type="button"
|
|
className={`${base} ${response === "attending" ? active : inactive}`}
|
|
aria-pressed={response === "attending"}
|
|
disabled={pending}
|
|
onClick={() => respond("attending")}
|
|
>
|
|
Bin da
|
|
</button>
|
|
<button
|
|
type="button"
|
|
className={`${base} ${response === "not_attending" ? active : inactive}`}
|
|
aria-pressed={response === "not_attending"}
|
|
disabled={pending}
|
|
onClick={() => respond("not_attending")}
|
|
>
|
|
Bin nicht da
|
|
</button>
|
|
</div>
|
|
{error ? (
|
|
<p role="alert" className="text-xs text-destructive">
|
|
{error}
|
|
</p>
|
|
) : null}
|
|
</div>
|
|
);
|
|
}
|