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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | 106x 106x 106x 106x 106x 106x 106x 106x 62x 44x 106x 24x 1x 1x 89x | import { useState, Dispatch, SetStateAction, FormEvent } from "react";
import { Link, useNavigate } from "react-router-dom";
import {
Form,
Select,
Input,
Button,
Icon,
Row,
Col,
} from "@canonical/react-components";
import type { RegistrationResponse } from "./local-types";
type Props = {
isSending: boolean;
setIsSending: Dispatch<SetStateAction<boolean>>;
setRegistrationResponse: Dispatch<
SetStateAction<RegistrationResponse | undefined>
>;
availableStores: { name: string; id: string }[];
selectedStore: string;
setSelectedStore: Dispatch<SetStateAction<string>>;
};
function RegisterSnapForm({
isSending,
setIsSending,
setRegistrationResponse,
availableStores,
selectedStore,
setSelectedStore,
}: Props): React.JSX.Element {
const [snapName, setSnapName] = useState<string>();
const [privacy, setPrivacy] = useState<string>("private");
const [showSnapNameConstraints, setShowSnapNameConstraints] =
useState<boolean>(false);
const navigate = useNavigate();
const handleSubmit = async (e: FormEvent) => {
e.preventDefault();
setIsSending(true);
const formData = new FormData();
formData.set("csrf_token", window.CSRF_TOKEN);
formData.set("snap_name", snapName || "");
formData.set("store", selectedStore);
const response = await fetch("/api/register-snap", {
method: "POST",
headers: {
"X-CSRFToken": window.CSRF_TOKEN,
},
body: formData,
});
if (!response.ok) {
setIsSending(false);
throw new Error("Unable to register snap name");
}
const responseData = await response.json();
setTimeout(() => {
if (responseData.success) {
navigate("/snaps");
} else {
setRegistrationResponse(responseData.data);
setIsSending(false);
}
}, 1000);
};
// Must satisfy the following requriements:
// - Contain no more than 40 characters
// - Consist of only lowercase letters, numbers, and hyphens
// - Contain at least one letter
// - Not start or end with a hyphen
//
// See: https://documentation.ubuntu.com/snapcraft/stable/how-to/publishing/register-a-snap/#name-your-snap
const isValid = (): boolean => {
const snapNamePattern = /^(?<!-)\d*[a-z][a-z0-9-]*(?<!-)$/;
if (snapName && snapName.length < 40 && snapName.match(snapNamePattern)) {
return true;
}
return false;
};
return (
<Form onSubmit={handleSubmit}>
<Row>
<Col size={8}>
<Select
label="Store"
options={availableStores.map((store) => ({
label: store.name,
value: store.id,
}))}
onChange={(e) => {
setSelectedStore(e.target.value);
Iif (e.target.value === "ubuntu") {
setPrivacy("private");
}
}}
required
/>
<Input
type="text"
label="Snap name"
defaultValue={snapName}
onChange={(e) => {
setSnapName(e.target.value);
}}
required
/>
<Button
type="button"
appearance="link"
onClick={() => {
setShowSnapNameConstraints(!showSnapNameConstraints);
}}
>
<small>
{showSnapNameConstraints ? <>Hide</> : <>Show</>} snap name
constraints
</small>
</Button>
{showSnapNameConstraints && (
<ul>
<li>
<small>Contain no more than 40 characters</small>
</li>
<li>
<small>
Consist of only lowercase letters, numbers, and hyphens
</small>
</li>
<li>
<small>Contain at least one letter</small>
</li>
<li>
<small>Not start or end with a hyphen</small>
</li>
</ul>
)}
<p>
<label htmlFor="public">Snap privacy</label>
</p>
<p className="p-form-help-text">
This can be changed at any time after the initial upload
</p>
<Input
type="radio"
label="Public"
name="public"
disabled={selectedStore === "ubuntu"}
value="public"
checked={privacy === "public"}
onChange={(e) => {
setPrivacy(e.target.value);
}}
/>
<Input
type="radio"
label="Private"
name="public"
help="Snap is hidden in stores and only accessible by the publisher and collaborators"
disabled={selectedStore === "ubuntu"}
value="private"
checked={privacy === "private" || selectedStore === "ubuntu"}
onChange={(e) => {
setPrivacy(e.target.value);
}}
/>
</Col>
</Row>
<hr />
<div className="u-align--right">
<Button
type="submit"
appearance="positive"
disabled={isSending || !isValid()}
>
{isSending ? (
<>
<Icon name="spinner" className="u-animation--spin" light />
Registering
</>
) : (
<>Register</>
)}
</Button>
<Link className="p-button" to="/snaps">
Cancel
</Link>
</div>
</Form>
);
}
export default RegisterSnapForm;
|