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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 | 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 10x 10x 14x 10x 14x 10x 10x 10x 14x | import { useState, useEffect } from "react"; import { FieldValues, useForm, useWatch } from "react-hook-form"; import { Button, Form, Strip, Row, Col, Notification, Tooltip, } from "@canonical/react-components"; import SaveAndPreview from "../../components/SaveAndPreview"; import SearchAutocomplete from "../../components/SearchAutocomplete"; import UpdateMetadataModal from "../../components/UpdateMetadataModal"; import SaveStateNotifications from "../../components/SaveStateNotifications"; import { UnregisterSnapModal } from "./UnregisterSnapModal"; import { getSettingsData, getSettingsFormData } from "../../utils"; type Props = { settings: FieldValues; }; function PublisherSettingsForm({ settings }: Props) { const settingsData = getSettingsData(settings); const countries = settingsData.countries; const [isSaving, setIsSaving] = useState(false); const [hasSaved, setHasSaved] = useState(false); const [savedError, setSavedError] = useState< boolean | { code: string; message: string }[] >(false); const [unregisterError, setUnregisterError] = useState(false); const [unregisterErrorMessage, setUnregisterErrorMessage] = useState(""); const [unregisterModalOpen, setUnregisterModalOpen] = useState(false); const [isUsersSnap, setIsUsersSnap] = useState(false); const [formData, setFormData] = useState({}); const [showMetadataWarningModal, setShowMetadataWarningModal] = useState(false); const { register, handleSubmit, reset, formState, getValues, setValue, control, } = useForm({ defaultValues: settingsData, mode: "onChange", }); const isDirty = formState.isDirty; const isValid = formState.isValid; const dirtyFields: { [key: string]: unknown } = formState.dirtyFields; const whitelistCountryKeyValues = useWatch({ control, name: "whitelist_country_keys", }); const blacklistCountryKeyValues = useWatch({ control, name: "blacklist_country_keys", }); const onSubmit = (data: FieldValues) => { if (getValues("update_metadata_on_release") && dirtyFields.visibility) { setShowMetadataWarningModal(true); setFormData(data); } else { submitForm(data); } }; const submitForm = async (data: FieldValues) => { setIsSaving(true); setHasSaved(false); setSavedError(false); if (data.visibility === "private" || data.visibility_locked) { data.private = true; data.unlisted = false; } else if (data.visibility === "unlisted") { data.private = false; data.unlisted = true; } else if (data.visibility === "public") { data.private = false; data.unlisted = false; } const response = await fetch(`/api/${data.snap_name}/settings`, { method: "POST", body: getSettingsFormData(settingsData, dirtyFields, data), }); if (response.status !== 200) { setIsSaving(false); setSavedError(true); return; } let newData = await response.json(); setIsSaving(false); if (newData.error_list) { setSavedError(newData.error_list); setIsSaving(false); return; } setHasSaved(true); setIsSaving(false); newData = { ...data, ...newData }; if (newData.private) { newData.visibility = "private"; } else if (newData.unlisted) { newData.visibility = "unlisted"; } reset(newData); window.scrollTo(0, 0); }; const getPublishedStatus = () => { Iif (!settingsData || !settingsData.status) { return "No status"; } return ( settingsData.status.charAt(0).toUpperCase() + settingsData.status.slice(1) ); }; useEffect(() => { Eif (whitelistCountryKeyValues) { setValue("blacklist_country_keys", ""); } }, [whitelistCountryKeyValues]); useEffect(() => { Iif (blacklistCountryKeyValues) { setValue("whitelist_country_keys", ""); } }, [blacklistCountryKeyValues]); useEffect(() => { const snapName = settingsData?.snap_name; fetch(`/snap_info/user_snap/${snapName}`) .then((response) => { if (!response.ok) { throw new Error(`Error: ${response.status}}`); } return response.json(); }) .then((data) => { setIsUsersSnap(data.is_users_snap); }) .catch((error) => { console.error("Error:", error); }); }, [settingsData?.snap_name]); return ( <> {settingsData?.visibility_locked && ( <div className="u-fixed-width"> <Notification severity="information" title=""> Your Snap is in the queue for manual review. When approved, you will receive an email, and you will be able to change the visibility of your Snap. </Notification> </div> )} <Form onSubmit={handleSubmit(onSubmit)} stacked={true}> <SaveAndPreview snapName={settingsData?.snap_name} isDirty={isDirty} reset={reset} isSaving={isSaving} isValid={isValid} /> <Strip shallow={true}> <SaveStateNotifications hasSaved={hasSaved} setHasSaved={setHasSaved} savedError={savedError} setSavedError={setSavedError} /> <Row className="p-form__group"> <Col size={2}> <label className="p-form__label" htmlFor="private"> Visibility: </label> </Col> <Col size={8}> <div className="p-form__control"> <label className="p-radio"> <input type="radio" className="p-radio__input" value="public" disabled={settingsData?.visibility_locked} defaultChecked={settingsData.visibility === "public"} {...register("visibility")} /> <span className="p-radio__label">Public</span> <br /> <span className="p-form-help-text"> Anyone can find your snap in the Snap Store </span> </label> </div> <div className="p-form__control"> <label className="p-radio"> <input type="radio" className="p-radio__input" value="unlisted" disabled={settingsData?.visibility_locked} defaultChecked={settingsData.visibility === "unlisted"} {...register("visibility")} /> <span className="p-radio__label">Unlisted</span> <br /> <span className="p-form-help-text"> Does not appear in search results. Anyone can visit or install using its snap name </span> </label> </div> <div className="p-form__control"> <label className="p-radio"> <input type="radio" className="p-radio__input" value="private" disabled={settingsData?.visibility_locked} defaultChecked={settingsData.visibility === "private"} {...register("visibility")} /> <span className="p-radio__label">Private</span> <br /> <span className="p-form-help-text"> Hidden in the Snap Store. Only accessible by the publisher and collaborators </span> </label> </div> </Col> </Row> <div className="u-fixed-width"> <hr /> </div> <Row className="p-form__group"> <Col size={2}> <label className="p-form__label" htmlFor="distribution"> Distribution: </label> </Col> <Col size={8}> <p className="u-no-margin--bottom"> This snap should be available in: </p> <ul className="p-inline-list"> <li className="p-inline-list__item"> <label className="p-radio u-no-margin--bottom" style={{ display: "inline-block" }} > <input type="radio" className="p-radio__input" value="all" defaultChecked={ settingsData.territory_distribution_status === "all" } {...register("territory_distribution_status")} /> <span className="p-radio__label">All territories</span> </label> </li> <li className="p-inline-list__item"> <label className="p-radio u-no-margin--bottom" style={{ display: "inline-block" }} > <input type="radio" className="p-radio__input" value="custom" defaultChecked={ settingsData.territory_distribution_status === "custom" } {...register("territory_distribution_status")} /> <span className="p-radio__label">Selected territories</span> </label> </li> </ul> {getValues("territory_distribution_status") === "custom" && ( <> <label className="p-radio"> <input type="radio" className="p-radio__input" value="include" defaultChecked={ settingsData.country_keys_status === "include" } {...register("country_keys_status")} /> <span className="p-radio__label"> Only these territories </span> </label> <SearchAutocomplete data={countries} field="whitelist_country_keys" currentValues={settingsData?.whitelist_countries} register={register} setValue={setValue} getValues={getValues} control={control} disabled={getValues("country_keys_status") === "exclude"} /> <label className="p-radio"> <input type="radio" className="p-radio__input" value="exclude" defaultChecked={ settingsData.country_keys_status === "exclude" } {...register("country_keys_status")} /> <span className="p-radio__label"> Exclude these territories </span> </label> <SearchAutocomplete data={countries} field="blacklist_country_keys" currentValues={settingsData?.blacklist_countries} register={register} setValue={setValue} getValues={getValues} control={control} disabled={getValues("country_keys_status") === "include"} /> </> )} </Col> </Row> <div className="u-fixed-width"> <hr /> </div> <Row className="p-form__group"> <Col size={2}> <label className="p-form__label" htmlFor="update_metadata_on_release" > Update metadata on release: </label> </Col> <Col size={8}> <div className="p-form__control"> <label className="p-checkbox"> <input id="update_metadata_on_release" type="checkbox" className="p-checkbox__input" defaultChecked={settingsData.update_metadata_on_release} {...register("update_metadata_on_release", { setValueAs: (value) => value === "on" || value === true, })} /> <span className="p-checkbox__label">Enabled</span> <br /> <span className="p-form-help-text"> The information on the Listing page of this snap will be automatically updated to the version in snapcraft.yaml of the latest revision pushed to the stable channel. If you manually edit the Listing page, the automatic updates will be turned off.{" "} <a href="/docs/snapcraft-top-level-metadata">Learn more</a>. </span> </label> {getValues("update_metadata_on_release") && ( <Notification severity="caution" title="Warning"> This snap is set to have its metadata updated when a new revision is published in the stable channel. Any changes you make here will be overwritten by the contents of any snap published. If this is not desirable, please disable “Update metadata on release” for this snap. </Notification> )} </div> </Col> </Row> <div className="u-fixed-width"> <hr /> </div> <Row className="p-form__group"> <Col size={2}> <p id="collaboration-label" style={{ marginBottom: ".625rem" }}> Collaboration: </p> </Col> <Col size={8}> <div className="p-form__control" aria-labelledby="collaboration-label" > <a href={`https://dashboard.snapcraft.io/snaps/${settingsData?.snap_name}/collaboration/`} > Manage collaborators in dashboard.snapcraft.io </a> </div> </Col> </Row> <div className="u-fixed-width"> <hr /> </div> <Row className="p-form__group"> <Col size={2}> <p id="store-label" style={{ marginBottom: ".625rem" }}> Store: </p> </Col> <Col size={8}> <div className="p-form__control" aria-labelledby="store-label"> {settingsData?.store} </div> </Col> </Row> <div className="u-fixed-width"> <hr /> </div> <Row className="p-form__group"> {unregisterModalOpen && ( <UnregisterSnapModal snapName={settingsData.snap_name} setUnregisterModalOpen={setUnregisterModalOpen} setUnregisterError={setUnregisterError} setUnregisterErrorMessage={setUnregisterErrorMessage} /> )} <Col size={2}> <p id="status-label" style={{ marginBottom: ".625rem" }}> Status: </p> </Col> <Col size={8}> <div className="p-form__control" aria-labelledby="status-label"> {settingsData?.status === "unpublished" ? ( <div> <span className="u-margin--right">Registered</span> {!isUsersSnap ? ( <Tooltip message={ <>Snaps can only be unregistered by their owner.</> } position="top-center" > <Button inline={true} disabled> Unregister </Button> </Tooltip> ) : ( <Button inline={true} onClick={(event) => { event.preventDefault(); setUnregisterModalOpen(true); }} > Unregister </Button> )} </div> ) : ( getPublishedStatus() )} </div> </Col> {unregisterError && ( <div className="u-fixed-width"> <Notification severity="negative" title={unregisterErrorMessage} onDismiss={() => { setUnregisterError(false); }} /> </div> )} </Row> </Strip> </Form> {showMetadataWarningModal ? ( <UpdateMetadataModal setShowMetadataWarningModal={setShowMetadataWarningModal} submitForm={submitForm} formData={formData} /> ) : null} </> ); } export default PublisherSettingsForm; |