|
| 1 | +import { ReactNode, useState } from "react"; |
| 2 | +import { useRecoilValue } from "recoil"; |
| 3 | +import { |
| 4 | + Form, |
| 5 | + Button, |
| 6 | + Input, |
| 7 | + Textarea, |
| 8 | + Row, |
| 9 | + Col, |
| 10 | + Notification, |
| 11 | + Icon, |
| 12 | +} from "@canonical/react-components"; |
| 13 | + |
| 14 | +import { brandStoresState } from "../../state/brandStoreState"; |
| 15 | + |
| 16 | +function RequestReservedName(): ReactNode { |
| 17 | + const [claimComment, setClaimComment] = useState<string>(); |
| 18 | + const [isSubmitting, setIsSubmitting] = useState<boolean>(); |
| 19 | + const [showSuccessNotification, setShowSuccessNotification] = |
| 20 | + useState<boolean>(false); |
| 21 | + const [errorNotification, setErrorNotification] = useState<string>(); |
| 22 | + const stores = useRecoilValue(brandStoresState); |
| 23 | + const params = new URLSearchParams(document.location.search); |
| 24 | + const snapName = params.get("snap_name"); |
| 25 | + const store = params.get("store"); |
| 26 | + |
| 27 | + const selectedStore = stores.find((s) => s.name === store) || { |
| 28 | + name: "Global", |
| 29 | + id: "ubuntu", |
| 30 | + }; |
| 31 | + |
| 32 | + return ( |
| 33 | + <> |
| 34 | + <h1 className="p-heading--2"> |
| 35 | + Request reserved name <strong>{snapName}</strong> |
| 36 | + </h1> |
| 37 | + |
| 38 | + {showSuccessNotification && ( |
| 39 | + <Notification severity="positive"> |
| 40 | + Your claim has been submitted and will be reviewed |
| 41 | + </Notification> |
| 42 | + )} |
| 43 | + |
| 44 | + {errorNotification && ( |
| 45 | + <Notification severity="caution">{errorNotification}</Notification> |
| 46 | + )} |
| 47 | + |
| 48 | + {stores.length === 0 && ( |
| 49 | + <> |
| 50 | + <Icon name="spinner" className="u-animation--spin" /> |
| 51 | + Loading data... |
| 52 | + </> |
| 53 | + )} |
| 54 | + |
| 55 | + {stores.length > 0 && ( |
| 56 | + <Form |
| 57 | + onSubmit={async (e) => { |
| 58 | + e.preventDefault(); |
| 59 | + |
| 60 | + setIsSubmitting(true); |
| 61 | + |
| 62 | + const response = await fetch("/api/register-name-dispute", { |
| 63 | + method: "POST", |
| 64 | + headers: { |
| 65 | + "X-CSRFToken": window.CSRF_TOKEN, |
| 66 | + }, |
| 67 | + body: JSON.stringify({ |
| 68 | + "snap-name": snapName, |
| 69 | + "claim-comment": claimComment, |
| 70 | + }), |
| 71 | + }); |
| 72 | + |
| 73 | + if (!response.ok) { |
| 74 | + setIsSubmitting(false); |
| 75 | + throw new Error("Unable to request reserved name"); |
| 76 | + } |
| 77 | + |
| 78 | + const responseData = await response.json(); |
| 79 | + |
| 80 | + setTimeout(() => { |
| 81 | + if (!responseData.success) { |
| 82 | + setErrorNotification(responseData.message); |
| 83 | + } else { |
| 84 | + setShowSuccessNotification(true); |
| 85 | + } |
| 86 | + |
| 87 | + setIsSubmitting(false); |
| 88 | + }, 1500); |
| 89 | + }} |
| 90 | + > |
| 91 | + <Row> |
| 92 | + <Col size={2}> |
| 93 | + <label htmlFor="store">Store</label> |
| 94 | + </Col> |
| 95 | + <Col size={6}> |
| 96 | + <Input |
| 97 | + type="text" |
| 98 | + readOnly |
| 99 | + id="store" |
| 100 | + value={selectedStore.name} |
| 101 | + /> |
| 102 | + </Col> |
| 103 | + </Row> |
| 104 | + |
| 105 | + <Row> |
| 106 | + <Col size={2}> |
| 107 | + <label htmlFor="snap">Snap name</label> |
| 108 | + </Col> |
| 109 | + <Col size={6}> |
| 110 | + <Input type="text" id="snap" readOnly value={snapName || ""} /> |
| 111 | + </Col> |
| 112 | + </Row> |
| 113 | + |
| 114 | + <Row> |
| 115 | + <Col size={2}> |
| 116 | + <label htmlFor="comment">Comment</label> |
| 117 | + </Col> |
| 118 | + <Col size={6}> |
| 119 | + <Textarea |
| 120 | + id="comment" |
| 121 | + help="Why you have rights to claim this name" |
| 122 | + value={claimComment} |
| 123 | + onChange={(e) => { |
| 124 | + setClaimComment(e.target.value); |
| 125 | + }} |
| 126 | + /> |
| 127 | + </Col> |
| 128 | + </Row> |
| 129 | + <hr /> |
| 130 | + <div className="u-align--right"> |
| 131 | + <a className="p-button" href="/register-snap"> |
| 132 | + Register snap |
| 133 | + </a> |
| 134 | + <Button |
| 135 | + type="submit" |
| 136 | + appearance="positive" |
| 137 | + disabled={!claimComment || isSubmitting} |
| 138 | + > |
| 139 | + {isSubmitting ? ( |
| 140 | + <> |
| 141 | + <Icon name="spinner" className="u-animation--spin" light /> |
| 142 | + Requesting |
| 143 | + </> |
| 144 | + ) : ( |
| 145 | + <>Yes, I am sure</> |
| 146 | + )} |
| 147 | + </Button> |
| 148 | + </div> |
| 149 | + </Form> |
| 150 | + )} |
| 151 | + </> |
| 152 | + ); |
| 153 | +} |
| 154 | + |
| 155 | +export default RequestReservedName; |
0 commit comments