All files / publisher/pages/RequestReservedName RequestReservedName.tsx

91.3% Statements 21/23
80% Branches 16/20
100% Functions 4/4
90.9% Lines 20/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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155                                  77x 77x   77x 77x 77x 77x 77x 77x   154x         77x                                                   2x   2x   2x                     2x         2x   2x 1x   1x     2x                                                                       66x                                                                
import { useState } from "react";
import { Link, useSearchParams } from "react-router-dom";
import { useRecoilValue } from "recoil";
import {
  Form,
  Button,
  Input,
  Textarea,
  Row,
  Col,
  Notification,
  Icon,
} from "@canonical/react-components";
 
import { brandStoresState } from "../../state/brandStoreState";
 
function RequestReservedName(): React.JSX.Element {
  const [claimComment, setClaimComment] = useState<string>();
  const [isSubmitting, setIsSubmitting] = useState<boolean>();
  const [showSuccessNotification, setShowSuccessNotification] =
    useState<boolean>(false);
  const [errorNotification, setErrorNotification] = useState<string>();
  const stores = useRecoilValue(brandStoresState);
  const [searchParams] = useSearchParams();
  const snapName = searchParams.get("snap_name");
  const store = searchParams.get("store");
 
  const selectedStore = stores.find((s) => s.name === store) || {
    name: "Global",
    id: "ubuntu",
  };
 
  return (
    <>
      <h1 className="p-heading--2">
        Request reserved name <strong>{snapName}</strong>
      </h1>
 
      {showSuccessNotification && (
        <Notification severity="positive">
          Your claim has been submitted and will be reviewed
        </Notification>
      )}
 
      {errorNotification && (
        <Notification severity="caution">{errorNotification}</Notification>
      )}
 
      {stores.length === 0 && (
        <>
          <Icon name="spinner" className="u-animation--spin" />
          &nbsp;Loading data...
        </>
      )}
 
      {stores.length > 0 && (
        <Form
          onSubmit={async (e) => {
            e.preventDefault();
 
            setIsSubmitting(true);
 
            const response = await fetch("/api/register-name-dispute", {
              method: "POST",
              headers: {
                "X-CSRFToken": window.CSRF_TOKEN,
              },
              body: JSON.stringify({
                "snap-name": snapName,
                "claim-comment": claimComment,
              }),
            });
 
            Iif (!response.ok) {
              setIsSubmitting(false);
              throw new Error("Unable to request reserved name");
            }
 
            const responseData = await response.json();
 
            if (!responseData.success) {
              setErrorNotification(responseData.message);
            } else {
              setShowSuccessNotification(true);
            }
 
            setIsSubmitting(false);
          }}
        >
          <Row>
            <Col size={2}>
              <label htmlFor="store">Store</label>
            </Col>
            <Col size={6}>
              <Input
                type="text"
                readOnly
                id="store"
                value={selectedStore.name}
              />
            </Col>
          </Row>
 
          <Row>
            <Col size={2}>
              <label htmlFor="snap">Snap name</label>
            </Col>
            <Col size={6}>
              <Input type="text" id="snap" readOnly value={snapName || ""} />
            </Col>
          </Row>
 
          <Row>
            <Col size={2}>
              <label htmlFor="comment">Comment</label>
            </Col>
            <Col size={6}>
              <Textarea
                id="comment"
                help="Why you have rights to claim this name"
                value={claimComment}
                onChange={(e) => {
                  setClaimComment(e.target.value);
                }}
              />
            </Col>
          </Row>
          <hr />
          <div className="u-align--right">
            <Link to="/register-snap" className="p-button">
              Register snap
            </Link>
            <Button
              type="submit"
              appearance="positive"
              disabled={!claimComment || isSubmitting}
            >
              {isSubmitting ? (
                <>
                  <Icon name="spinner" className="u-animation--spin" light />
                  &nbsp;Requesting
                </>
              ) : (
                <>Yes, I am sure</>
              )}
            </Button>
          </div>
        </Form>
      )}
    </>
  );
}
 
export default RequestReservedName;