Skip to content

Commit 69fa37d

Browse files
authored
Theme upload validation workaround (#1025)
* Fix Can't create theme when uploading a file When trying to create a theme, you can upload files. After the upload completes, you should be able to proceed to the next step. However, the "Next" button remained disabled, making it impossible to proceed. (Well, not actually impossible, going a step back and then forward again would fix the issue, but w/e). This fixes that, but crudely. We may want to rewrite the theme dialog because uploading each file individually and forcing the user to wait on that seems pretty bad UX to me.
1 parent b9fbb45 commit 69fa37d

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

src/components/shared/wizard/FileUpload.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useTranslation } from "react-i18next";
2-
import React, { useRef, useState } from "react";
2+
import React, { useEffect, useRef, useState } from "react";
33
import axios from "axios";
44
import { NOTIFICATION_CONTEXT } from "../../../configs/modalConfig";
55
import { useAppDispatch } from "../../../store";
@@ -43,6 +43,17 @@ const FileUpload = <T extends RequiredFormProps>({
4343
// reference used for activating file input when button is clicked
4444
const hiddenFileInput = useRef<HTMLInputElement>(null);
4545

46+
// Trigger formik validation
47+
// Setting formik fields in a promise callback does not trigger formik
48+
// validation (or at the very least, does not trigger it with the new
49+
// values). Therefore, this useEffect gets manually triggered, causing an
50+
// additional rerender which then triggers formik validation.
51+
useEffect(() => {
52+
formik.validateForm()
53+
// eslint-disable-next-line react-hooks/exhaustive-deps
54+
}, [formik.values.fileId, formik.values.fileName, loaded]);
55+
56+
4657
const handleDelete = () => {
4758
setFile(undefined);
4859
setLoaded(0);
@@ -68,7 +79,9 @@ const FileUpload = <T extends RequiredFormProps>({
6879
if (res.status === 201) {
6980
// set information about file later needed for POST request and summary
7081
formik.setFieldValue(fileId, res.data);
71-
formik.setFieldValue(fileName, file.name);
82+
formik.setFieldValue(fileName, file.name)
83+
// Purely for triggering useEffect. The state change does not matter.
84+
setLoaded(1337)
7285
}
7386
})
7487
.catch((res) => {

0 commit comments

Comments
 (0)