Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ui-commons): prompt form Form displayed on dialog validation #2089

Merged
merged 3 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function CreateVariantDialog(props: Props) {

return (
<FormDialog
maxWidth="sm"
maxWidth="sm" // Study name source can be long
title={t("studies.createNewStudy")}
titleIcon={AddCircleIcon}
open={open}
Expand All @@ -68,7 +68,7 @@ function CreateVariantDialog(props: Props) {
config={{ defaultValues }}
>
{({ control }) => (
<Fieldset fieldWidth={550}>
<Fieldset fullFieldWidth>
<StringFE
label={t("variants.newVariant")}
name="name"
Expand All @@ -80,14 +80,14 @@ function CreateVariantDialog(props: Props) {
/>
<SelectFE
label={t("study.versionSource")}
name="sourceId"
variant="outlined"
options={sourceList.map((ver) => ({
label: ver.name,
value: ver.id,
}))}
name="sourceId"
control={control}
required
rules={{ required: true }}
/>
</Fieldset>
)}
Expand Down
6 changes: 5 additions & 1 deletion webapp/src/components/common/Fieldset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ function Fieldset(props: FieldsetProps) {
m: 0,
},
},
// Remove padding from the last child of the dialog content
".MuiDialogContent-root .Form__Content > &:last-child": {
pb: 0,
},
},
sx,
)}
Expand All @@ -59,7 +63,7 @@ function Fieldset(props: FieldsetProps) {
<Divider sx={{ mt: 1 }} />
</>
)}
<Box {...contentProps} sx={mergeSxProp({ pt: 2 }, contentProps?.sx)}>
<Box {...contentProps} sx={mergeSxProp({ pt: 1 }, contentProps?.sx)}>
{children}
</Box>
</Box>
Expand Down
17 changes: 9 additions & 8 deletions webapp/src/components/common/Form/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { FormEvent, useEffect, useMemo, useRef } from "react";
import { FormEvent, useEffect, useMemo, useRef, useState } from "react";
import {
DeepPartial,
FieldPath,
Expand Down Expand Up @@ -116,6 +116,7 @@ function Form<TFieldValues extends FieldValues, TContext>(
const { t } = useTranslation();
const autoSubmitConfig = toAutoSubmitConfig(autoSubmit);

const [isInProgress, setIsInProgress] = useState(false);
Copy link
Member

@hdinia hdinia Jul 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider this instead:

 const [isSubmitting, setIsSubmitting] = useState(false);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isSubmitting already exist...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the same component? Ok mybad didnt see that

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe you can just add the 'submit' to indicate it refers to a submitting state like isSubmitInProgress (it's a detail tho feel free to skip)

const [showAutoSubmitLoader, setShowAutoSubmitLoader] = useDebouncedState(
false,
750,
Expand All @@ -130,7 +131,6 @@ function Form<TFieldValues extends FieldValues, TContext>(
const lastSubmittedData = useRef<TFieldValues>();
// eslint-disable-next-line @typescript-eslint/no-empty-function
const submitSuccessfulCb = useRef(() => {});
const preventClose = useRef(false);

const contextValue = useMemo(
() => ({ isAutoSubmitEnabled: autoSubmitConfig.enable }),
Expand Down Expand Up @@ -224,7 +224,7 @@ function Form<TFieldValues extends FieldValues, TContext>(
// Prevent browser close if a submit is pending
useEffect(() => {
const listener = (event: BeforeUnloadEvent) => {
if (preventClose.current) {
if (isInProgress) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isSubmitting

// eslint-disable-next-line no-param-reassign
event.returnValue = t("form.submit.inProgress");
} else if (isDirty) {
Expand All @@ -238,14 +238,14 @@ function Form<TFieldValues extends FieldValues, TContext>(
return () => {
window.removeEventListener("beforeunload", listener);
};
}, [t, isDirty]);
}, [t, isInProgress, isDirty]);

useUpdateEffect(() => onStateChange?.(formState), [formState]);

useEffect(() => setRef(apiRef, formApiPlus));

usePrompt(t("form.submit.inProgress"), preventClose.current);
usePrompt(t("form.changeNotSaved"), isDirty);
usePrompt(t("form.submit.inProgress"), isInProgress);
usePrompt(t("form.changeNotSaved"), isDirty && !isInProgress);
Comment on lines +247 to +248
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optional: maybe you can wrap those in a useEffect, use strict conditions, and add a cleanup for the prompt
(this can potentially lead to overlapping or competing prompts)

  useEffect(() => {
    if (isSubmitting) {
      usePrompt(t("form.submit.inProgress"), true);
    } else if (isDirty) {
      usePrompt(t("form.changeNotSaved"), true);
    } else {
      usePrompt(null, false); // Clear the prompt when no conditions are met
    }

    // Cleanup function to clear the prompt when the component unmounts
    return () => usePrompt(null, false);
  }, [isSubmitting, isDirty, t]);


////////////////////////////////////////////////////////////////
// Submit
Expand Down Expand Up @@ -299,7 +299,7 @@ function Form<TFieldValues extends FieldValues, TContext>(
});
})
.finally(() => {
preventClose.current = false;
setIsInProgress(false);
});
}, onInvalid);

Expand All @@ -309,7 +309,8 @@ function Form<TFieldValues extends FieldValues, TContext>(
const submitDebounced = useDebounce(submit, autoSubmitConfig.wait);

const requestSubmit = () => {
preventClose.current = true;
setIsInProgress(true);

if (autoSubmitConfig.enable) {
submitDebounced();
} else {
Expand Down
Loading