All files / src/app/compliance/_components RevokeForm.tsx

78.26% Statements 18/23
64.7% Branches 11/17
60% Functions 3/5
81.81% Lines 18/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 1033x                                   51x 51x 51x 51x 51x   51x 1x 1x 1x 1x         1x       1x       1x           1x       1x       51x                                         44x                                                    
"use client";
 
import { fetchApi } from "@/lib/api";
import { useState } from "react";
import { useT } from "@/lib/i18n/translate";
 
/**
 * Revocation of an issued permit (Art. 63(3)). A refusal of a later
 * application changes nothing for a permit already issued; only this stops
 * the transfers, and the public register lists it (Art. 57(1)(j)(iv)).
 */
export function RevokeForm({
  permitId,
  onDone,
}: {
  permitId: string;
  onDone: () => Promise<void>;
}) {
  const t = useT("access");
  const [reason, setReason] = useState("");
  const [busy, setBusy] = useState(false);
  const [error, setError] = useState<string | null>(null);
  const [done, setDone] = useState<string | null>(null);
 
  const revoke = async () => {
    setBusy(true);
    setError(null);
    try {
      const r = await fetchApi("/api/compliance/permits/revoke", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ permitId, reason }),
      });
      const body = (await r.json().catch(() => ({}))) as Record<
        string,
        unknown
      >;
      Iif (!r.ok) {
        setError(String(body.error ?? `HTTP ${r.status}`));
        return;
      }
      setDone(
        t(
          "Data permit {id} revoked; transfers under it stop now and the register lists the measure (Art. 63(3)).",
          { id: permitId },
        ),
      );
      await onDone();
    } catch (e) {
      setError(e instanceof Error ? e.message : String(e));
    } finally {
      setBusy(false);
    }
  };
 
  return (
    <form
      className="rounded-lg border border-(--danger-text)/40 bg-(--bg) p-3 space-y-2 text-xs"
      onSubmit={(e) => e.preventDefault()}
      aria-label={t("Revoke data permit")}
    >
      <div className="font-semibold text-(--text-primary)">
        {t("Revoke this permit")}
        <span className="font-normal text-(--text-secondary)">
          {" "}
          · {t("Art. 63(3), on non-compliance by the data user")}
        </span>
      </div>
      <label className="flex flex-col gap-1">
        <span className="text-(--text-secondary)">
          {t("Reason (published with the measure, Art. 57(1)(j)(iv))")}
        </span>
        <textarea
          id={`revoke-reason-${permitId}`}
          rows={2}
          value={reason}
          onChange={(e) => setReason(e.target.value)}
          className="rounded-sm border border-(--border) bg-(--surface) px-2 py-1"
        />
      </label>
      <button
        type="button"
        disabled={busy || !reason.trim() || done !== null}
        onClick={revoke}
        className="px-3 py-1.5 rounded-sm font-semibold border border-(--danger-text) text-(--danger-text) disabled:opacity-50"
        title={reason.trim() ? "" : t("A revocation needs a reason")}
      >
        {busy ? t("Revoking…") : t("Revoke permit")}
      </button>
      {error && (
        <p className="text-(--danger-text)" role="alert">
          {error}
        </p>
      )}
      {done && (
        <p className="text-(--success-text)" role="status">
          {done}
        </p>
      )}
    </form>
  );
}