forked from primeroIMS/primero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform-submission.js
37 lines (31 loc) · 1.05 KB
/
form-submission.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Copyright (c) 2014 - 2023 UNICEF. All rights reserved.
/* eslint-disable import/prefer-default-export */
import isEmpty from "lodash/isEmpty";
import { enqueueSnackbar } from "../../notifier/action-creators";
import { touchedFormData } from "./touched-data";
export const submitHandler = ({
data,
dispatch,
dirtyFields,
isEdit = false,
initialValues,
onSubmit,
submitAllFields,
submitAllArrayData = false,
message,
submitAlways
}) => {
// formState needs to be called here otherwise touched will not work.
// https://github.com/react-hook-form/react-hook-form-website/issues/154
const changedFormData = touchedFormData(dirtyFields, data, isEdit, initialValues, submitAllArrayData);
const snackbarMessage = message !== undefined ? message : null;
if (isEmpty(changedFormData) && !submitAlways) {
return dispatch(
enqueueSnackbar(snackbarMessage, {
...(!snackbarMessage && { messageKey: "messages.no_changes" }),
type: "error"
})
);
}
return onSubmit(submitAllFields ? data : changedFormData);
};