All files / publisher/pages/PublisherSettings UnregisterSnapModal.tsx

93.75% Statements 15/16
100% Branches 6/6
80% Functions 4/5
93.75% Lines 15/16

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                                10x     10x 4x 4x             3x 1x 1x 1x 1x   2x     1x       10x                                 1x                     4x 4x                                                    
import { Dispatch, SetStateAction, useState } from "react";
import { Button, Modal } from "@canonical/react-components";
 
type UnregisterSnapModalProps = {
  snapName: string;
  setUnregisterModalOpen: Dispatch<SetStateAction<boolean>>;
  setUnregisterError: Dispatch<SetStateAction<boolean>>;
  setUnregisterErrorMessage: Dispatch<SetStateAction<string>>;
};
 
export function UnregisterSnapModal({
  snapName,
  setUnregisterModalOpen,
  setUnregisterError,
  setUnregisterErrorMessage,
}: UnregisterSnapModalProps) {
  const [unregisterPackageInProgress, setUnregisterPackageInProgress] =
    useState(false);
 
  const unregisterPackage = async () => {
    try {
      const response = await fetch(`/packages/${snapName}`, {
        method: "DELETE",
        headers: {
          "X-CSRFToken": window["CSRF_TOKEN"],
        },
      });
 
      if (!response.ok) {
        const responseData = await response.json();
        setUnregisterModalOpen(false);
        setUnregisterError(true);
        setUnregisterErrorMessage(responseData.error);
      } else {
        window.location.href = "/snaps";
      }
    } catch (error) {
      console.error(error);
    }
  };
 
  return (
    <>
      <Modal
        close={() => {
          setUnregisterModalOpen(false);
        }}
        title={
          <span className="u-has-icon">
            <i className="p-icon--warning modal-header-icon"></i>
            Unregister "{snapName}"
          </span>
        }
        buttonRow={
          <>
            <Button
              className="u-no-margin--bottom"
              onClick={() => {
                setUnregisterModalOpen(false);
              }}
            >
              Cancel
            </Button>
            <Button
              appearance="negative"
              className={`u-no-margin--bottom ${
                unregisterPackageInProgress ? "has-icon is-dark" : ""
              }`}
              onClick={() => {
                setUnregisterPackageInProgress(true);
                unregisterPackage();
              }}
              disabled={unregisterPackageInProgress}
            >
              {unregisterPackageInProgress ? (
                <>
                  <i className="p-icon--spinner u-animation--spin is-light"></i>
                  <span>Unregistering...</span>
                </>
              ) : (
                "Unregister"
              )}
            </Button>
          </>
        }
      >
        <p>
          Are you sure you want to unregister "{snapName}"?
          <br />
          This name will be removed from your registered names and become
          available to others. This action is permanent and cannot be undone.
        </p>
      </Modal>
    </>
  );
}