-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: ibolton336 <[email protected]>
- Loading branch information
1 parent
8ce3163
commit 8176e70
Showing
3 changed files
with
175 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
168 changes: 168 additions & 0 deletions
168
client/src/app/pages/assessment/import-questionnaire-form/import-questionnaire-form.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
import React, { useState } from "react"; | ||
import { AxiosResponse } from "axios"; | ||
import { useTranslation } from "react-i18next"; | ||
import * as yup from "yup"; | ||
|
||
import { | ||
ActionGroup, | ||
Button, | ||
ButtonVariant, | ||
FileUpload, | ||
Form, | ||
FormHelperText, | ||
HelperText, | ||
HelperTextItem, | ||
} from "@patternfly/react-core"; | ||
|
||
import { HookFormPFGroupController } from "@app/components/HookFormPFFields"; | ||
import { useForm } from "react-hook-form"; | ||
import { FileLoadError, IReadFile } from "@app/api/models"; | ||
import { yupResolver } from "@hookform/resolvers/yup"; | ||
import { useCreateFileMutation } from "@app/queries/targets"; | ||
|
||
export interface ImportQuestionnaireFormProps { | ||
onSaved: (response?: AxiosResponse) => void; | ||
} | ||
export interface ImportQuestionnaireFormValues { | ||
yamlFile: IReadFile; | ||
} | ||
|
||
export const yamlFileSchema: yup.SchemaOf<IReadFile> = yup.object({ | ||
fileName: yup.string().required(), | ||
fullFile: yup.mixed<File>(), | ||
loadError: yup.mixed<FileLoadError>(), | ||
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<string>(); | ||
const [isFileRejected, setIsFileRejected] = useState(false); | ||
const validationSchema: yup.SchemaOf<ImportQuestionnaireFormValues> = yup | ||
.object() | ||
.shape({ | ||
yamlFile: yamlFileSchema, | ||
}); | ||
const methods = useForm<ImportQuestionnaireFormValues>({ | ||
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 ( | ||
<Form onSubmit={handleSubmit(onSubmit)}> | ||
<HookFormPFGroupController | ||
control={control} | ||
name="yamlFile" | ||
label={t("terms.uploadYamlFile")} | ||
fieldId="yamlFile" | ||
helperText={t("dialog.uploadYamlFile")} | ||
renderInput={({ field: { onChange, name }, fieldState: { error } }) => ( | ||
<FileUpload | ||
id={`${name}-file-upload`} | ||
name={name} | ||
value={filename} | ||
filename={filename} | ||
filenamePlaceholder={t("dialog.dragAndDropFile")} | ||
dropzoneProps={{ | ||
accept: { | ||
"text/yaml": [".yml", ".yaml"], | ||
}, | ||
maxSize: 1000000, | ||
onDropRejected: (event) => { | ||
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. | ||
handleFileUpload(file) | ||
.then((res) => { | ||
setFocus(name); | ||
clearErrors(name); | ||
trigger(name); | ||
}) | ||
.catch((err) => { | ||
//Handle new api error here | ||
}); | ||
}} | ||
onClearClick={() => { | ||
//TODO | ||
console.log("clearing file"); | ||
}} | ||
browseButtonText="Upload" | ||
/> | ||
)} | ||
/> | ||
|
||
{isFileRejected && ( | ||
<FormHelperText> | ||
<HelperText> | ||
<HelperTextItem variant="error"> | ||
You should select a YAML file. | ||
</HelperTextItem> | ||
</HelperText> | ||
</FormHelperText> | ||
)} | ||
<ActionGroup> | ||
<Button | ||
type="submit" | ||
aria-label="submit" | ||
id="import-questionnaire-submit-button" | ||
variant={ButtonVariant.primary} | ||
isDisabled={!isValid || isSubmitting || isValidating || !isDirty} | ||
> | ||
{t("actions.import")} | ||
</Button> | ||
</ActionGroup> | ||
</Form> | ||
); | ||
}; |