All files / publisher/pages/RegisterNameDispute RegisterNameDisputeForm.tsx

33.33% Statements 4/12
50% Branches 5/10
66.66% Functions 2/3
33.33% Lines 4/12

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 103 104 105 106 107 108 109 110 111 112 113                                            45x 45x   45x                                                                                                               34x                                                              
import { useState, Dispatch, SetStateAction } from "react";
import {
  Form,
  Input,
  Textarea,
  Row,
  Col,
  Button,
} from "@canonical/react-components";
import { Link } from "react-router-dom";
 
type Props = {
  snapName: string | null;
  store: string | undefined;
  setClaimSubmitted: Dispatch<SetStateAction<boolean>>;
};
 
function RegisterNameDisputeForm({
  snapName,
  store,
  setClaimSubmitted,
}: Props): React.JSX.Element {
  const [claimComment, setClaimComment] = useState<string>();
  const [isSending, setIsSending] = useState<boolean>();
 
  return (
    <>
      <h1 className="p-heading--2">
        Claim the name <strong>{snapName}</strong>
      </h1>
      <p>Request the transfer of this snap name from its current owner</p>
      <Form
        stacked
        onSubmit={async (e) => {
          e.preventDefault();
 
          setIsSending(true);
 
          const response = await fetch("/api/register-name-dispute", {
            method: "POST",
            headers: {
              "X-CSRF-Token": window.CSRF_TOKEN,
            },
            body: JSON.stringify({
              "snap-name": snapName,
              "claim-comment": claimComment,
              store,
            }),
          });
 
          if (!response.ok) {
            setIsSending(false);
            throw new Error("Unable to register name dispute");
          }
 
          setIsSending(false);
          setClaimSubmitted(true);
        }}
      >
        <Row>
          <Col size={8}>
            <Input
              type="text"
              readOnly
              stacked
              label="Store"
              value={store || ""}
            />
            <Input
              type="text"
              readOnly
              stacked
              label="Snap name"
              value={snapName || ""}
            />
            <Textarea
              label="Comment"
              help="Why you have rights to claim this name"
              stacked
              defaultValue={claimComment}
              onChange={(e) => {
                setClaimComment(e.target.value);
              }}
            />
          </Col>
        </Row>
        <hr />
        <div className="u-align--right">
          <Link className="p-button" to="/register-snap">
            Register a new name
          </Link>
          <Button
            type="submit"
            appearance="positive"
            disabled={isSending || !claimComment}
          >
            {isSending ? (
              <>
                <i className="p-icon--spinner u-animation--spin is-light"></i>
                &nbsp;Submitting
              </>
            ) : (
              <>Submit name claim</>
            )}
          </Button>
        </div>
      </Form>
    </>
  );
}
 
export default RegisterNameDisputeForm;