|
| 1 | +import { useState, type FC } from "react"; |
| 2 | +import { Link, useNavigate } from "react-router-dom"; |
| 3 | +import { useNotify, useToastNotification } from "@canonical/react-components"; |
| 4 | +import { renameImageRegistry } from "api/image-registries"; |
| 5 | +import RenameHeader from "components/RenameHeader"; |
| 6 | +import type { RenameHeaderValues } from "components/RenameHeader"; |
| 7 | +import { useFormik } from "formik"; |
| 8 | +import ImageRegistryRichChip from "pages/images/ImageRegistryRichChip"; |
| 9 | +import DeleteImageRegistryBtn from "pages/images/actions/DeleteImageRegistryBtn"; |
| 10 | +import type { LxdImageRegistry } from "types/image"; |
| 11 | +import { useImageRegistriesEntitlements } from "util/entitlements/images"; |
| 12 | +import { checkDuplicateName } from "util/helpers"; |
| 13 | +import { ROOT_PATH } from "util/rootPath"; |
| 14 | +import * as Yup from "yup"; |
| 15 | + |
| 16 | +interface Props { |
| 17 | + imageRegistry: LxdImageRegistry; |
| 18 | + project: string; |
| 19 | +} |
| 20 | + |
| 21 | +const ImageRegistryDetailHeader: FC<Props> = ({ imageRegistry, project }) => { |
| 22 | + const navigate = useNavigate(); |
| 23 | + const notify = useNotify(); |
| 24 | + const toastNotify = useToastNotification(); |
| 25 | + const { canEditImageRegistry } = useImageRegistriesEntitlements(); |
| 26 | + const controllerState = useState<AbortController | null>(null); |
| 27 | + |
| 28 | + const RenameSchema = Yup.object().shape({ |
| 29 | + name: Yup.string() |
| 30 | + .test( |
| 31 | + "deduplicate", |
| 32 | + "An image registry with this name already exists", |
| 33 | + async (value) => |
| 34 | + checkDuplicateName( |
| 35 | + value, |
| 36 | + project, |
| 37 | + controllerState, |
| 38 | + "image-registries", |
| 39 | + ), |
| 40 | + ) |
| 41 | + .required("Image registry name is required"), |
| 42 | + }); |
| 43 | + |
| 44 | + const getDisabledReason = () => { |
| 45 | + if (!canEditImageRegistry(imageRegistry)) { |
| 46 | + return "You do not have permission to rename this image registry"; |
| 47 | + } |
| 48 | + |
| 49 | + if (!imageRegistry) { |
| 50 | + return "Invalid Image Registry: Cannot be renamed"; |
| 51 | + } |
| 52 | + |
| 53 | + return undefined; |
| 54 | + }; |
| 55 | + |
| 56 | + const onSuccess = (imageRegistryName: string) => { |
| 57 | + const url = `${ROOT_PATH}/ui/image-registry/${encodeURIComponent(imageRegistryName)}`; |
| 58 | + navigate(url); |
| 59 | + console.log("Navigating to", url); |
| 60 | + toastNotify.success( |
| 61 | + <> |
| 62 | + Image registry <strong>{imageRegistry.name}</strong> renamed to{" "} |
| 63 | + <ImageRegistryRichChip imageRegistryName={imageRegistryName} /> |
| 64 | + </>, |
| 65 | + ); |
| 66 | + }; |
| 67 | + |
| 68 | + const onFailure = (imageRegistryName: string, e: unknown) => { |
| 69 | + notify.failure(`Renaming of image registry ${imageRegistryName} failed`, e); |
| 70 | + }; |
| 71 | + |
| 72 | + const formik = useFormik<RenameHeaderValues>({ |
| 73 | + initialValues: { |
| 74 | + name: imageRegistry.name, |
| 75 | + isRenaming: false, |
| 76 | + }, |
| 77 | + validationSchema: RenameSchema, |
| 78 | + onSubmit: (values) => { |
| 79 | + if (imageRegistry.name === values.name) { |
| 80 | + formik.setFieldValue("isRenaming", false); |
| 81 | + formik.setSubmitting(false); |
| 82 | + return; |
| 83 | + } |
| 84 | + renameImageRegistry(imageRegistry.name, values.name) |
| 85 | + .then(() => { |
| 86 | + onSuccess(values.name); |
| 87 | + formik.setFieldValue("isRenaming", false); |
| 88 | + }) |
| 89 | + .catch((e) => { |
| 90 | + onFailure(values.name, e); |
| 91 | + }) |
| 92 | + .finally(() => { |
| 93 | + formik.setSubmitting(false); |
| 94 | + }); |
| 95 | + }, |
| 96 | + }); |
| 97 | + |
| 98 | + return ( |
| 99 | + <RenameHeader |
| 100 | + name={imageRegistry.name} |
| 101 | + parentItems={[ |
| 102 | + <Link |
| 103 | + to={`${ROOT_PATH}/ui/project/${encodeURIComponent(project)}/image-registries`} |
| 104 | + key={1} |
| 105 | + > |
| 106 | + Image registries |
| 107 | + </Link>, |
| 108 | + ]} |
| 109 | + controls={[ |
| 110 | + <DeleteImageRegistryBtn |
| 111 | + key="delete" |
| 112 | + imageRegistry={imageRegistry} |
| 113 | + shouldExpand |
| 114 | + />, |
| 115 | + ]} |
| 116 | + isLoaded={Boolean(imageRegistry.name)} |
| 117 | + formik={formik} |
| 118 | + renameDisabledReason={getDisabledReason()} |
| 119 | + /> |
| 120 | + ); |
| 121 | +}; |
| 122 | + |
| 123 | +export default ImageRegistryDetailHeader; |
0 commit comments