diff --git a/client/public/locales/en/translation.json b/client/public/locales/en/translation.json index ee02060da8..af3fd3c148 100644 --- a/client/public/locales/en/translation.json +++ b/client/public/locales/en/translation.json @@ -96,7 +96,10 @@ "discardAssessment": "The assessment for <1>{{applicationName}} will be discarded, as well as the review result. Do you wish to continue?", "leavePage": "Are you sure you want to leave this page? Be sure to save your changes, or they will be lost.", "pageError": "Oops! Something went wrong.", - "refreshPage": "Try to refresh your page or contact your admin." + "refreshPage": "Try to refresh your page or contact your admin.", + "maxfileSize": "Max file size of 1MB exceeded. Upload a smaller file.", + "dragAndDropFile": "Drag and drop your file here or upload one.", + "uploadYamlFile": "Upload your YAML file" }, "title": { "applicationAnalysis": "Application analysis", @@ -362,6 +365,7 @@ "unknown": "Unknown", "unsuitableForContainers": "Unsuitable for containers", "uploadApplicationFile": "Upload your application file", + "uploadYamlFile": "Upload your YAML file", "url": "URL", "user": "User", "version": "Version", diff --git a/client/src/app/pages/assessment-management/assessment-settings/assessment-settings-page.tsx b/client/src/app/pages/assessment-management/assessment-settings/assessment-settings-page.tsx index df7f83c200..db5a5a78ea 100644 --- a/client/src/app/pages/assessment-management/assessment-settings/assessment-settings-page.tsx +++ b/client/src/app/pages/assessment-management/assessment-settings/assessment-settings-page.tsx @@ -50,6 +50,7 @@ import { getAxiosErrorMessage } from "@app/utils/utils"; import { Questionnaire } from "@app/api/models"; import { useHistory } from "react-router-dom"; import { Paths } from "@app/Paths"; +import { ImportQuestionnaireForm } from "@app/pages/assessment/import-questionnaire-form/import-questionnaire-form"; const AssessmentSettings: React.FC = () => { const { t } = useTranslation(); @@ -382,7 +383,7 @@ const AssessmentSettings: React.FC = () => { isOpen={isImportModal} onClose={() => setIsImportModal(false)} > - TODO Import questionnaire component + setIsImportModal(false)} /> void; +} +export interface ImportQuestionnaireFormValues { + yamlFile: IReadFile; +} + +export const yamlFileSchema: yup.SchemaOf = yup.object({ + fileName: yup.string().required(), + fullFile: yup.mixed(), + loadError: yup.mixed(), + loadPercentage: yup.number(), + loadResult: yup.mixed<"danger" | "success" | undefined>(), + data: yup.string(), + responseID: yup.number(), +}); + +export const ImportQuestionnaireForm: React.FC< + ImportQuestionnaireFormProps +> = ({ onSaved }) => { + const { t } = useTranslation(); + + const [filename, setFilename] = useState(); + const [isFileRejected, setIsFileRejected] = useState(false); + const validationSchema: yup.SchemaOf = yup + .object() + .shape({ + yamlFile: yamlFileSchema, + }); + const methods = useForm({ + resolver: yupResolver(validationSchema), + mode: "onChange", + }); + + const { + handleSubmit, + formState: { isSubmitting, isValidating, isValid, isDirty }, + getValues, + setValue, + control, + watch, + setFocus, + clearErrors, + trigger, + reset, + } = methods; + + const { mutateAsync: createYamlFileAsync } = useCreateFileMutation(); + + const handleFileUpload = async (file: File) => { + setFilename(file.name); + const formFile = new FormData(); + formFile.append("file", file); + + const newYamlFile: IReadFile = { + fileName: file.name, + fullFile: file, + }; + + return createYamlFileAsync({ + formData: formFile, + file: newYamlFile, + }); + }; + + const onSubmit = (values: ImportQuestionnaireFormValues) => { + console.log("values", values); + onSaved(); + }; + return ( +
+ ( + { + const currentFile = event[0]; + if (currentFile.file.size > 1000000) { + methods.setError(name, { + type: "custom", + message: t("dialog.maxFileSize"), + }); + } + setIsFileRejected(true); + }, + }} + validated={isFileRejected || error ? "error" : "default"} + onFileInputChange={async (_, file) => { + console.log("uploading file", file); + //TODO: handle new api here. This is just a placeholder. + try { + await handleFileUpload(file); + setFocus(name); + clearErrors(name); + trigger(name); + } catch (err) { + //Handle new api error here + } + }} + onClearClick={() => { + //TODO + console.log("clearing file"); + }} + browseButtonText="Upload" + /> + )} + /> + + {isFileRejected && ( + + + + You should select a YAML file. + + + + )} + + + + + ); +};