81 lines
2.7 KiB
TypeScript
81 lines
2.7 KiB
TypeScript
"use client"
|
|
|
|
import { useLocale, useTranslations } from "next-intl";
|
|
import type { Consent } from "@/types";
|
|
import { Item, ItemActions, ItemContent, ItemDescription, ItemTitle } from "@/components/ui/item";
|
|
import { Button } from "@/components/ui/button";
|
|
import { Spinner } from "@/components/ui/spinner";
|
|
import { localeMap } from "@/types";
|
|
import { enUS } from "date-fns/locale";
|
|
import { format } from "date-fns";
|
|
import { revokeConsent } from "@/actions/consents";
|
|
import { useState } from "react";
|
|
import { toast } from "sonner";
|
|
import { Check, X } from "lucide-react";
|
|
|
|
interface Props {
|
|
consents: Consent[] | null;
|
|
setConsents: (consents: Consent[]) => void;
|
|
}
|
|
|
|
export default function Applications({ consents, setConsents }: Props) {
|
|
const t = useTranslations("MePage.items.applications");
|
|
const [list, setList] = useState(consents);
|
|
const [revokingId, setRevokingId] = useState<string | null>(null);
|
|
|
|
const locale = useLocale();
|
|
const dateLocale = localeMap[locale as keyof typeof localeMap] ?? enUS;
|
|
|
|
const handleRevoke = async (clientId: string) => {
|
|
if (revokingId) return;
|
|
setRevokingId(clientId);
|
|
|
|
try {
|
|
const result = await revokeConsent(clientId);
|
|
if (result.success) {
|
|
const updated = list?.filter((c) => c.client_id !== clientId) ?? [];
|
|
setList(updated);
|
|
setConsents(updated);
|
|
toast(t("successfullyRevoked"), { icon: <Check size={16} /> });
|
|
} else {
|
|
toast(t("revocationError"), { icon: <X size={16} /> });
|
|
}
|
|
} catch {
|
|
toast(t("revocationError"), { icon: <X size={16} /> });
|
|
} finally {
|
|
setRevokingId(null);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="w-full lg:max-w-sm flex flex-col gap-4">
|
|
<div className="space-y-2">
|
|
<h1 className="text-xl">{t("title")}</h1>
|
|
<p className="text-sm text-muted-foreground">{t("subtitle")}</p>
|
|
</div>
|
|
<div className="space-y-4">
|
|
{list?.map((consent) => (
|
|
<Item key={consent.client_id}>
|
|
<ItemContent>
|
|
<ItemTitle>{consent.client_name}</ItemTitle>
|
|
<ItemDescription>
|
|
{t("authorized")}{" "}
|
|
{format(consent.authorized_at, "PPP", { locale: dateLocale })}
|
|
</ItemDescription>
|
|
</ItemContent>
|
|
<ItemActions>
|
|
<Button
|
|
disabled={!consent.isRevokable || revokingId === consent.client_id}
|
|
onClick={() => handleRevoke(consent.client_id)}
|
|
>
|
|
{revokingId === consent.client_id && <Spinner />}
|
|
{t("revoke")}
|
|
</Button>
|
|
</ItemActions>
|
|
</Item>
|
|
))}
|
|
{list == null && <h2 className="text-lg">{t("noConsents")}</h2>}
|
|
</div>
|
|
</div>
|
|
);
|
|
} |