Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 3315b3a
Author: arodidev <[email protected]>
Date:   Mon Aug 14 13:32:46 2023 +0300

    Moved validation state to parent component
  • Loading branch information
arodidev committed Aug 14, 2023
1 parent fb3a72b commit ffb8e1c
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 32 deletions.
8 changes: 8 additions & 0 deletions src/components/form-editor/form-editor.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const FormEditor: React.FC = () => {
const { form, formError, isLoadingForm } = useForm(formUuid);
const { clobdata, clobdataError, isLoadingClobdata } = useClobdata(form);
const [status, setStatus] = useState<Status>("idle");
const [isValidating, setIsValidating] = useState<boolean>(false)

const isLoadingFormOrSchema =
formUuid && (isLoadingClobdata || isLoadingForm);
Expand Down Expand Up @@ -98,6 +99,10 @@ const FormEditor: React.FC = () => {
localStorage.setItem("formJSON", JSON.stringify(updatedSchema));
}, []);

const validationSetter = ()=>{
setIsValidating(prevState => !prevState)
}

const DraftSchemaModal = () => {
return (
<ComposedModal
Expand Down Expand Up @@ -163,6 +168,7 @@ const FormEditor: React.FC = () => {
schema={schema}
onSchemaChange={updateSchema}
isLoading={isLoadingFormOrSchema}
validateStateSetter={validationSetter}
/>
</>
</TabPanel>
Expand All @@ -187,6 +193,8 @@ const FormEditor: React.FC = () => {
schema={schema}
onSchemaChange={updateSchema}
isLoading={isLoadingFormOrSchema}
isFormValidating={isValidating}
setIsFormValidating={validationSetter}
/>
</TabPanel>
</TabPanels>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,16 @@ type InteractiveBuilderProps = {
isLoading: boolean;
onSchemaChange: (schema: Schema) => void;
schema: Schema;
isFormValidating;
setIsFormValidating;
};

const InteractiveBuilder: React.FC<InteractiveBuilderProps> = ({
isLoading,
onSchemaChange,
schema,
isFormValidating,
setIsFormValidating
}) => {
const mouseSensor = useSensor(MouseSensor, {
activationConstraint: {
Expand Down Expand Up @@ -74,24 +78,26 @@ const InteractiveBuilder: React.FC<InteractiveBuilderProps> = ({
const [responses, setResponses] = useState([]);

const validateForm = () => {
handleFormValidation(schema, dataTypeToRenderingMap).then((response) => {
const [errorsArray] = response;
setResponses(errorsArray);
errorsArray.length
? showToast({
title: "Validation completed",
kind: "error",
critical: true,
description: "Errors found during validation",
})
: showToast({
title: "Validation completed",
kind: "success",
critical: true,
description: "No errors found during form validation",
});
});
};
handleFormValidation(schema, dataTypeToRenderingMap).then(response => {
const [errorsArray] = response
setResponses(errorsArray)
errorsArray.length
? showToast({
title: "Validation completed",
kind: "error",
critical: true,
description: "Errors found during validation",
})
: showToast({
title: "Validation completed",
kind: "success",
critical: true,
description: "No errors found during form validation",
});
}).then(
setIsFormValidating()
)
}

const initializeSchema = useCallback(() => {
const dummySchema: OHRIFormSchema = {
Expand Down Expand Up @@ -322,6 +328,9 @@ const InteractiveBuilder: React.FC<InteractiveBuilderProps> = ({
allQuestions={section.questions}
/>
);

isFormValidating && validateForm()

return (
<div className={styles.container}>
{isLoading ? (
Expand All @@ -331,13 +340,7 @@ const InteractiveBuilder: React.FC<InteractiveBuilderProps> = ({
) : null}
<div className={styles.validator}>
<ActionButtons schema={schema} t={t} />
<Button
className = {styles.validateButton}
onClick={validateForm}
disabled={schema? false : true}
>
Validate Form
</Button>

</div>

{showNewFormModal ? (
Expand Down
5 changes: 0 additions & 5 deletions src/components/interactive-builder/interactive-builder.scss
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,3 @@
font-size: 13px !important;
margin-left: 20px !important;
}

.validateButton{
position: absolute;
top: 202px;
}
15 changes: 13 additions & 2 deletions src/components/schema-editor/schema-editor.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useParams } from "react-router-dom";
import AceEditor from "react-ace";
import "ace-builds/webpack-resolver";
import "ace-builds/src-noconflict/ext-language_tools";
import { Button, InlineLoading } from "@carbon/react";
import { Button, InlineLoading, ButtonSet } from "@carbon/react";
import { useTranslation } from "react-i18next";
import { OHRIFormSchema } from "@openmrs/openmrs-form-engine-lib";

Expand All @@ -14,12 +14,14 @@ type SchemaEditorProps = {
isLoading: boolean;
onSchemaChange: (schema: Schema) => void;
schema: Schema;
validateStateSetter;
};

const SchemaEditor: React.FC<SchemaEditorProps> = ({
isLoading,
onSchemaChange,
schema,
validateStateSetter
}) => {
const { t } = useTranslation();
const { formUuid } = useParams<RouteParams>();
Expand Down Expand Up @@ -142,7 +144,14 @@ const SchemaEditor: React.FC<SchemaEditorProps> = ({
) : null}

{schema ? (
<Button
<ButtonSet>
<Button
onClick={()=> validateStateSetter()}
disabled={schema? false : true}
>
Validate Form
</Button>
<Button
disabled={isRendering}
kind="primary"
onClick={renderSchemaChanges}
Expand All @@ -156,6 +165,8 @@ const SchemaEditor: React.FC<SchemaEditorProps> = ({
<span>{t("renderChanges", "Render changes")}</span>
)}
</Button>
</ButtonSet>

) : null}
</div>

Expand Down
1 change: 1 addition & 0 deletions src/components/schema-editor/schema-editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
align-items: center;
justify-content: flex-end;
margin: 1rem 0;
margin-right: 2.9rem;

button {
margin-left: 1rem
Expand Down

0 comments on commit ffb8e1c

Please sign in to comment.