Skip to content

Commit

Permalink
FIX: no need to handle async/sync separately
Browse files Browse the repository at this point in the history
  • Loading branch information
sandhya-spend committed Oct 18, 2024
1 parent 10b205f commit 65d4311
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
19 changes: 6 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@ let defaultGetSnapshot = form => F.flattenObject(toJS(gatherFormValues(form)))

let defaultGetNestedSnapshot = form => F.unflattenObject(form.getSnapshot())

const handleSubmitErr = (state, err) => {
if (err instanceof ValidationError) {
state.errors = err.cause
}
throw err
}

export default ({
submit: configSubmit,
value = {},
Expand Down Expand Up @@ -185,16 +178,16 @@ export default ({
return F.getOrReturn('message', form.submit.state.error)
},
})
let submit = Command(() => {
let submit = Command(async () => {
if (_.isEmpty(form.validate())) {
form.submit.state.error = null
try {
// Handle both sync and sync configSubmit
return Promise.resolve(configSubmit(form.getSnapshot(), form)).catch(
err => handleSubmitErr(state, err)
)
return await configSubmit(form.getSnapshot(), form)
} catch (err) {
handleSubmitErr(state, err)
if (err instanceof ValidationError) {
state.errors = { '': err.message, ...err.cause }
}
throw err
}
}
throw 'Validation Error'
Expand Down
2 changes: 2 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ describe('submit()', () => {
'location.addresses.0.tenants.0': ['invalid format'],
})
expect(form.errors).toEqual({
'': 'My submit failed',
'location.addresses.0.tenants.0': ['invalid format'],
})
expect(form.submitError).toBe('My submit failed')
Expand All @@ -440,6 +441,7 @@ describe('submit()', () => {
'location.addresses.0.tenants.0': ['invalid format'],
})
expect(form.errors).toEqual({
'': 'My submit failed',
'location.addresses.0.tenants.0': ['invalid format'],
})
expect(form.submitError).toBe('My submit failed')
Expand Down

0 comments on commit 65d4311

Please sign in to comment.