fix
This commit is contained in:
@@ -2,13 +2,16 @@
|
||||
|
||||
import { useLocale, useTranslations } from "next-intl";
|
||||
import type { Consent } from "@/types";
|
||||
import { Item, ItemActions, ItemContent, ItemDescription, ItemTitle } from "@/components/ui/item"
|
||||
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;
|
||||
@@ -18,16 +21,29 @@ interface Props {
|
||||
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) => {
|
||||
const result = await revokeConsent(clientId);
|
||||
if (result.success) {
|
||||
const updated = list?.filter(c => c.client_id !== clientId) ?? [];
|
||||
setList(updated);
|
||||
setConsents(updated);
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -40,18 +56,23 @@ export default function Applications({ consents, setConsents }: Props) {
|
||||
<Item key={consent.client_id}>
|
||||
<ItemContent>
|
||||
<ItemTitle>{consent.client_name}</ItemTitle>
|
||||
<ItemDescription>{t("authorized")} {format(consent.authorized_at, "PPP", { locale: dateLocale })}</ItemDescription>
|
||||
<ItemDescription>
|
||||
{t("authorized")}{" "}
|
||||
{format(consent.authorized_at, "PPP", { locale: dateLocale })}
|
||||
</ItemDescription>
|
||||
</ItemContent>
|
||||
<ItemActions>
|
||||
<Button
|
||||
disabled={!consent.isRevokable}
|
||||
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>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user