|
| 1 | +import { |
| 2 | + ReactNode, |
| 3 | + useState, |
| 4 | + Dispatch, |
| 5 | + SetStateAction, |
| 6 | + FormEvent, |
| 7 | +} from "react"; |
| 8 | +import { Link, useNavigate } from "react-router-dom"; |
| 9 | +import { |
| 10 | + Form, |
| 11 | + Select, |
| 12 | + Input, |
| 13 | + Button, |
| 14 | + Icon, |
| 15 | + Row, |
| 16 | + Col, |
| 17 | +} from "@canonical/react-components"; |
| 18 | + |
| 19 | +import type { RegistrationResponse } from "./local-types"; |
| 20 | + |
| 21 | +type Props = { |
| 22 | + isSending: boolean; |
| 23 | + setIsSending: Dispatch<SetStateAction<boolean>>; |
| 24 | + setRegistrationResponse: Dispatch< |
| 25 | + SetStateAction<RegistrationResponse | undefined> |
| 26 | + >; |
| 27 | + availableStores: { name: string; id: string }[]; |
| 28 | + selectedStore: string; |
| 29 | + setSelectedStore: Dispatch<SetStateAction<string>>; |
| 30 | +}; |
| 31 | + |
| 32 | +function RegisterSnapForm({ |
| 33 | + isSending, |
| 34 | + setIsSending, |
| 35 | + setRegistrationResponse, |
| 36 | + availableStores, |
| 37 | + selectedStore, |
| 38 | + setSelectedStore, |
| 39 | +}: Props): ReactNode { |
| 40 | + const [snapName, setSnapName] = useState<string>(); |
| 41 | + const [privacy, setPrivacy] = useState<string>("private"); |
| 42 | + |
| 43 | + const navigate = useNavigate(); |
| 44 | + |
| 45 | + const handleSubmit = async (e: FormEvent) => { |
| 46 | + e.preventDefault(); |
| 47 | + |
| 48 | + setIsSending(true); |
| 49 | + |
| 50 | + const formData = new FormData(); |
| 51 | + formData.set("csrf_token", window.CSRF_TOKEN); |
| 52 | + formData.set("snap_name", snapName || ""); |
| 53 | + formData.set("store", selectedStore); |
| 54 | + |
| 55 | + const response = await fetch("/api/register-snap", { |
| 56 | + method: "POST", |
| 57 | + headers: { |
| 58 | + "X-CSRFToken": window.CSRF_TOKEN, |
| 59 | + }, |
| 60 | + body: formData, |
| 61 | + }); |
| 62 | + |
| 63 | + if (!response.ok) { |
| 64 | + setIsSending(false); |
| 65 | + throw new Error("Unable to register snap name"); |
| 66 | + } |
| 67 | + |
| 68 | + const responseData = await response.json(); |
| 69 | + |
| 70 | + setTimeout(() => { |
| 71 | + if (responseData.success) { |
| 72 | + navigate("/snaps"); |
| 73 | + } else { |
| 74 | + setRegistrationResponse(responseData.data); |
| 75 | + setIsSending(false); |
| 76 | + } |
| 77 | + }, 1000); |
| 78 | + }; |
| 79 | + |
| 80 | + return ( |
| 81 | + <Form onSubmit={handleSubmit}> |
| 82 | + <Row> |
| 83 | + <Col size={8}> |
| 84 | + <Select |
| 85 | + label="Store" |
| 86 | + options={availableStores.map((store) => ({ |
| 87 | + label: store.name, |
| 88 | + value: store.id, |
| 89 | + }))} |
| 90 | + onChange={(e) => { |
| 91 | + setSelectedStore(e.target.value); |
| 92 | + |
| 93 | + if (e.target.value === "ubuntu") { |
| 94 | + setPrivacy("private"); |
| 95 | + } |
| 96 | + }} |
| 97 | + required |
| 98 | + /> |
| 99 | + <Input |
| 100 | + type="text" |
| 101 | + label="Snap name" |
| 102 | + defaultValue={snapName} |
| 103 | + onChange={(e) => { |
| 104 | + setSnapName(e.target.value); |
| 105 | + }} |
| 106 | + required |
| 107 | + /> |
| 108 | + <label htmlFor="public">Snap privacy</label> |
| 109 | + <p className="p-form-help-text"> |
| 110 | + This can be changed at any time after the initial upload |
| 111 | + </p> |
| 112 | + <Input |
| 113 | + type="radio" |
| 114 | + label="Public" |
| 115 | + name="public" |
| 116 | + disabled={selectedStore === "ubuntu"} |
| 117 | + value="public" |
| 118 | + checked={privacy === "public"} |
| 119 | + onChange={(e) => { |
| 120 | + setPrivacy(e.target.value); |
| 121 | + }} |
| 122 | + /> |
| 123 | + <Input |
| 124 | + type="radio" |
| 125 | + label="Private" |
| 126 | + name="public" |
| 127 | + help="Snap is hidden in stores and only accessible by the publisher and collaborators" |
| 128 | + disabled={selectedStore === "ubuntu"} |
| 129 | + value="private" |
| 130 | + checked={privacy === "private" || selectedStore === "ubuntu"} |
| 131 | + onChange={(e) => { |
| 132 | + setPrivacy(e.target.value); |
| 133 | + }} |
| 134 | + /> |
| 135 | + </Col> |
| 136 | + </Row> |
| 137 | + <hr /> |
| 138 | + <div className="u-align--right"> |
| 139 | + <Button |
| 140 | + type="submit" |
| 141 | + appearance="positive" |
| 142 | + disabled={!snapName || isSending} |
| 143 | + > |
| 144 | + {isSending ? ( |
| 145 | + <> |
| 146 | + <Icon name="spinner" className="u-animation--spin" light /> |
| 147 | + Registering |
| 148 | + </> |
| 149 | + ) : ( |
| 150 | + <>Register</> |
| 151 | + )} |
| 152 | + </Button> |
| 153 | + <Link className="p-button" to="/snaps"> |
| 154 | + Cancel |
| 155 | + </Link> |
| 156 | + </div> |
| 157 | + </Form> |
| 158 | + ); |
| 159 | +} |
| 160 | + |
| 161 | +export default RegisterSnapForm; |
0 commit comments