use the submitter id to determine if the submit button clicked was the close button or submit

This commit is contained in:
jasquat 2023-05-15 14:18:18 -04:00
parent eb6a6f5808
commit 145a535fb9

View File

@ -88,19 +88,12 @@ function TypeaheadWidget({
); );
} }
enum FormSubmitType {
Default,
Draft,
}
export default function TaskShow() { export default function TaskShow() {
const [task, setTask] = useState<Task | null>(null); const [task, setTask] = useState<Task | null>(null);
const [userTasks] = useState(null); const [userTasks] = useState(null);
const params = useParams(); const params = useParams();
const navigate = useNavigate(); const navigate = useNavigate();
const [disabled, setDisabled] = useState(false); const [disabled, setDisabled] = useState(false);
// save current form data so that we can avoid validations in certain situations
const [currentFormObject, setCurrentFormObject] = useState<any>({});
const { addError, removeError } = useAPIError(); const { addError, removeError } = useAPIError();
@ -167,11 +160,7 @@ export default function TaskShow() {
} }
}; };
const handleFormSubmit = ( const handleFormSubmit = (formObject: any, event: any) => {
formObject: any,
_event: any,
submitType: FormSubmitType = FormSubmitType.Default
) => {
if (disabled) { if (disabled) {
return; return;
} }
@ -181,7 +170,7 @@ export default function TaskShow() {
return; return;
} }
let queryParams = ''; let queryParams = '';
if (submitType === FormSubmitType.Draft) { if (event && event.submitter.id === 'close-button') {
queryParams = '?save_as_draft=true'; queryParams = '?save_as_draft=true';
} }
setDisabled(true); setDisabled(true);
@ -301,11 +290,6 @@ export default function TaskShow() {
return errors; return errors;
}; };
const updateFormData = (formObject: any) => {
currentFormObject.formData = formObject.formData;
setCurrentFormObject(currentFormObject);
};
const formElement = () => { const formElement = () => {
if (!task) { if (!task) {
return null; return null;
@ -357,12 +341,10 @@ export default function TaskShow() {
closeButton = ( closeButton = (
<Button <Button
id="close-button" id="close-button"
type="submit"
disabled={disabled} disabled={disabled}
kind="secondary" kind="secondary"
title="Save changes without submitting." title="Save changes without submitting."
onClick={() =>
handleFormSubmit(currentFormObject, null, FormSubmitType.Draft)
}
> >
Close Close
</Button> </Button>
@ -406,10 +388,8 @@ export default function TaskShow() {
uiSchema={formUiSchema} uiSchema={formUiSchema}
widgets={widgets} widgets={widgets}
validator={validator} validator={validator}
onChange={updateFormData}
customValidate={customValidate} customValidate={customValidate}
omitExtraData omitExtraData
liveOmit
> >
{reactFragmentToHideSubmitButton} {reactFragmentToHideSubmitButton}
</Form> </Form>