-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
handleSubmit.js
133 lines (125 loc) · 3.29 KB
/
handleSubmit.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// @flow
import isPromise from 'is-promise'
import type { SubmitFunction } from './types'
import type { Props } from './createReduxForm'
import { isSubmissionError } from './SubmissionError'
const mergeErrors = ({ asyncErrors, syncErrors }) =>
asyncErrors && typeof asyncErrors.merge === 'function'
? asyncErrors.merge(syncErrors).toJS()
: { ...asyncErrors, ...syncErrors }
const executeSubmit = (submit: SubmitFunction, fields: string[], props: Props) => {
const {
dispatch,
submitAsSideEffect,
onSubmitFail,
onSubmitSuccess,
startSubmit,
stopSubmit,
setSubmitFailed,
setSubmitSucceeded,
values
} = props
let result
try {
result = submit(values, dispatch, props)
} catch (submitError) {
const error = isSubmissionError(submitError) ? submitError.errors : undefined
stopSubmit(error)
setSubmitFailed(...fields)
if (onSubmitFail) {
onSubmitFail(error, dispatch, submitError, props)
}
if (error || onSubmitFail) {
// if you've provided an onSubmitFail callback, don't re-throw the error
return error
} else {
throw submitError
}
}
if (submitAsSideEffect) {
if (result) {
dispatch(result)
}
} else {
if (isPromise(result)) {
startSubmit()
return result.then(
submitResult => {
stopSubmit()
setSubmitSucceeded()
if (onSubmitSuccess) {
onSubmitSuccess(submitResult, dispatch, props)
}
return submitResult
},
submitError => {
const error = isSubmissionError(submitError) ? submitError.errors : undefined
stopSubmit(error)
setSubmitFailed(...fields)
if (onSubmitFail) {
onSubmitFail(error, dispatch, submitError, props)
}
if (error || onSubmitFail) {
// if you've provided an onSubmitFail callback, don't re-throw the error
return error
} else {
throw submitError
}
}
)
} else {
setSubmitSucceeded()
if (onSubmitSuccess) {
onSubmitSuccess(result, dispatch, props)
}
}
}
return result
}
const handleSubmit = (
submit: SubmitFunction,
props: Props,
valid: boolean,
asyncValidate: Function,
fields: string[]
) => {
const {
dispatch,
onSubmitFail,
setSubmitFailed,
syncErrors,
asyncErrors,
touch,
persistentSubmitErrors
} = props
touch(...fields)
if (valid || persistentSubmitErrors) {
const asyncValidateResult = asyncValidate && asyncValidate()
if (asyncValidateResult) {
return asyncValidateResult
.then(asyncErrors => {
if (asyncErrors) {
throw asyncErrors
}
return executeSubmit(submit, fields, props)
})
.catch(asyncErrors => {
setSubmitFailed(...fields)
if (onSubmitFail) {
onSubmitFail(asyncErrors, dispatch, null, props)
}
return Promise.reject(asyncErrors)
})
} else {
return executeSubmit(submit, fields, props)
}
} else {
setSubmitFailed(...fields)
const errors = mergeErrors({ asyncErrors, syncErrors })
if (onSubmitFail) {
onSubmitFail(errors, dispatch, null, props)
}
return errors
}
}
export default handleSubmit