diff --git a/client/src/app/pages/migration-targets/components/custom-target-form.tsx b/client/src/app/pages/migration-targets/components/custom-target-form.tsx index e7e37f2bcc..48fcc654af 100644 --- a/client/src/app/pages/migration-targets/components/custom-target-form.tsx +++ b/client/src/app/pages/migration-targets/components/custom-target-form.tsx @@ -71,11 +71,12 @@ export const CustomTargetForm: React.FC = ({ const { pushNotification } = useContext(NotificationsContext); const { t } = useTranslation(); const [target, setTarget] = useState(initialTarget); + const [imageRejectedError, setImageRejectedError] = useState( + null + ); const [filename, setFilename] = React.useState("default.png"); - const [isImageFileRejected, setIsImageFileRejected] = useState(false); - const repositoryTypeOptions: OptionWithValue[] = [ { value: "git", @@ -408,52 +409,72 @@ export const CustomTargetForm: React.FC = ({ fieldId="custom-migration-target-upload-image" helperText="Upload a png or jpeg file (Max size: 1 MB)" renderInput={({ field: { onChange, name }, fieldState: { error } }) => ( - { - const currentFile = event[0]; - if (currentFile.file.size > 1000000) { - methods.setError("imageID", { - type: "custom", - message: "Max image file size of 1 MB exceeded.", - }); + <> + {imageRejectedError && ( + { + onChange(null); + setFilename("default.png"); + setValue("imageID", null); + setImageRejectedError(null); + }} + /> } - setIsImageFileRejected(true); - }, - }} - validated={isImageFileRejected || error ? "error" : "default"} - onFileInputChange={async (_, file) => { - handleFileUpload(file) - .then((res) => { - setValue("imageID", res.id); - setFocus("imageID"); - clearErrors("imageID"); - trigger("imageID"); - }) - .catch((err) => { - setValue("imageID", null); - }); - }} - onClearClick={() => { - onChange(0); - setFilename("default.png"); - setValue("imageID", null); - setIsImageFileRejected(false); - }} - browseButtonText="Upload" - /> + /> + )} + { + const currentFile = event[0]; + if (currentFile.file.size > 1000000) { + setImageRejectedError( + "Max image file size of 1 MB exceeded." + ); + } + setFilename(currentFile.file.name); + }, + }} + validated={"default"} + onFileInputChange={async (_, file) => { + handleFileUpload(file) + .then((res) => { + setValue("imageID", res.id); + setFocus("imageID"); + clearErrors("imageID"); + trigger("imageID"); + }) + .catch((err) => { + setValue("imageID", null); + }); + }} + onClearClick={() => { + setImageRejectedError(null); + onChange(null); + setFilename("default.png"); + setValue("imageID", null); + }} + browseButtonText="Upload" + /> + )} /> + = ({ aria-label="submit" id="submit" variant={ButtonVariant.primary} - isDisabled={!isValid || isSubmitting || isValidating || !isDirty} + isDisabled={ + !isValid || + isSubmitting || + isValidating || + !isDirty || + !!imageRejectedError + } > {!target ? t("actions.create") : t("actions.save")}