Skip to content

Commit

Permalink
add action setSubmitSuccess
Browse files Browse the repository at this point in the history
  • Loading branch information
eliseumds committed Jun 28, 2016
1 parent fe4d751 commit 7e07256
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 11 deletions.
42 changes: 33 additions & 9 deletions src/__tests__/handleSubmit.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ describe('handleSubmit', () => {
const stopSubmit = createSpy()
const touch = createSpy()
const setSubmitFailed = createSpy()
const setSubmitSuccess = createSpy()
const asyncValidate = createSpy()
const props = { startSubmit, stopSubmit, touch, setSubmitFailed, values }
const props = { startSubmit, stopSubmit, touch, setSubmitFailed, setSubmitSuccess, values }

handleSubmit(submit, props, false, asyncValidate, [ 'foo', 'baz' ])

expect(submit).toNotHaveBeenCalled()
expect(startSubmit).toNotHaveBeenCalled()
expect(stopSubmit).toNotHaveBeenCalled()
expect(setSubmitSuccess).toNotHaveBeenCalled()
expect(touch)
.toHaveBeenCalled()
.toHaveBeenCalledWith('foo', 'baz')
Expand All @@ -37,12 +39,14 @@ describe('handleSubmit', () => {
const stopSubmit = createSpy()
const touch = createSpy()
const setSubmitFailed = createSpy()
const setSubmitSuccess = createSpy()
const asyncValidate = createSpy()
const props = {
startSubmit,
stopSubmit,
touch,
setSubmitFailed,
setSubmitSuccess,
syncErrors,
values
}
Expand All @@ -54,6 +58,7 @@ describe('handleSubmit', () => {
expect(submit).toNotHaveBeenCalled()
expect(startSubmit).toNotHaveBeenCalled()
expect(stopSubmit).toNotHaveBeenCalled()
expect(setSubmitSuccess).toNotHaveBeenCalled()
expect(touch)
.toHaveBeenCalled()
.toHaveBeenCalledWith('foo', 'baz')
Expand All @@ -74,8 +79,9 @@ describe('handleSubmit', () => {
const stopSubmit = createSpy()
const touch = createSpy()
const setSubmitFailed = createSpy()
const setSubmitSuccess = createSpy()
const asyncValidate = undefined
const props = { dispatch, startSubmit, stopSubmit, touch, setSubmitFailed, values }
const props = { dispatch, startSubmit, stopSubmit, touch, setSubmitFailed, setSubmitSuccess, values }

expect(handleSubmit(submit, props, true, asyncValidate, [ 'foo', 'baz' ])).toBe(69)

Expand All @@ -88,6 +94,7 @@ describe('handleSubmit', () => {
.toHaveBeenCalled()
.toHaveBeenCalledWith('foo', 'baz')
expect(setSubmitFailed).toNotHaveBeenCalled()
expect(setSubmitSuccess).toHaveBeenCalledWith(69)
})

it('should not submit if async validation fails', done => {
Expand All @@ -98,8 +105,9 @@ describe('handleSubmit', () => {
const stopSubmit = createSpy()
const touch = createSpy()
const setSubmitFailed = createSpy()
const setSubmitSuccess = createSpy()
const asyncValidate = createSpy().andReturn(Promise.reject())
const props = { dispatch, startSubmit, stopSubmit, touch, setSubmitFailed, values }
const props = { dispatch, startSubmit, stopSubmit, touch, setSubmitFailed, setSubmitSuccess, values }

return handleSubmit(submit, props, true, asyncValidate, [ 'foo', 'baz' ])
.catch(result => {
Expand All @@ -116,6 +124,7 @@ describe('handleSubmit', () => {
expect(setSubmitFailed)
.toHaveBeenCalled()
.toHaveBeenCalledWith('foo', 'baz')
expect(setSubmitSuccess).toNotHaveBeenCalled()
done()
})
})
Expand All @@ -128,10 +137,11 @@ describe('handleSubmit', () => {
const stopSubmit = createSpy()
const touch = createSpy()
const setSubmitFailed = createSpy()
const setSubmitSuccess = createSpy()
const asyncErrors = { foo: 'async error' }
const asyncValidate = createSpy().andReturn(Promise.reject(asyncErrors))
const props = {
dispatch, startSubmit, stopSubmit, touch, setSubmitFailed, values
dispatch, startSubmit, stopSubmit, touch, setSubmitFailed, setSubmitSuccess, values
}

return handleSubmit(submit, props, true, asyncValidate, [ 'foo', 'baz' ])
Expand All @@ -149,6 +159,7 @@ describe('handleSubmit', () => {
expect(setSubmitFailed)
.toHaveBeenCalled()
.toHaveBeenCalledWith('foo', 'baz')
expect(setSubmitSuccess).toNotHaveBeenCalled()
})
})

Expand All @@ -160,8 +171,9 @@ describe('handleSubmit', () => {
const stopSubmit = createSpy()
const touch = createSpy()
const setSubmitFailed = createSpy()
const setSubmitSuccess = createSpy()
const asyncValidate = createSpy().andReturn(Promise.resolve())
const props = { dispatch, startSubmit, stopSubmit, touch, setSubmitFailed, values }
const props = { dispatch, startSubmit, stopSubmit, touch, setSubmitFailed, setSubmitSuccess, values }

return handleSubmit(submit, props, true, asyncValidate, [ 'foo', 'baz' ])
.then(result => {
Expand All @@ -178,6 +190,7 @@ describe('handleSubmit', () => {
.toHaveBeenCalled()
.toHaveBeenCalledWith('foo', 'baz')
expect(setSubmitFailed).toNotHaveBeenCalled()
expect(setSubmitSuccess).toHaveBeenCalledWith(69)
})
})

Expand All @@ -189,8 +202,9 @@ describe('handleSubmit', () => {
const stopSubmit = createSpy()
const touch = createSpy()
const setSubmitFailed = createSpy()
const setSubmitSuccess = createSpy()
const asyncValidate = createSpy().andReturn(Promise.resolve())
const props = { dispatch, startSubmit, stopSubmit, touch, setSubmitFailed, values }
const props = { dispatch, startSubmit, stopSubmit, touch, setSubmitFailed, setSubmitSuccess, values }

return handleSubmit(submit, props, true, asyncValidate, [ 'foo', 'baz' ])
.then(result => {
Expand All @@ -211,6 +225,8 @@ describe('handleSubmit', () => {
.toHaveBeenCalledWith('foo', 'baz')
expect(setSubmitFailed)
.toNotHaveBeenCalled()
expect(setSubmitSuccess)
.toHaveBeenCalledWith(69)
})
})

Expand All @@ -223,8 +239,9 @@ describe('handleSubmit', () => {
const stopSubmit = createSpy()
const touch = createSpy()
const setSubmitFailed = createSpy()
const setSubmitSuccess = createSpy()
const asyncValidate = createSpy().andReturn(Promise.resolve())
const props = { dispatch, startSubmit, stopSubmit, touch, setSubmitFailed, values }
const props = { dispatch, startSubmit, stopSubmit, touch, setSubmitFailed, setSubmitSuccess, values }

return handleSubmit(submit, props, true, asyncValidate, [ 'foo', 'baz' ])
.catch(error => {
Expand All @@ -245,6 +262,8 @@ describe('handleSubmit', () => {
.toHaveBeenCalledWith('foo', 'baz')
expect(setSubmitFailed)
.toNotHaveBeenCalled()
expect(setSubmitSuccess)
.toNotHaveBeenCalled()
done()
})
})
Expand All @@ -258,8 +277,9 @@ describe('handleSubmit', () => {
const stopSubmit = createSpy()
const touch = createSpy()
const setSubmitFailed = createSpy()
const setSubmitSuccess = createSpy()
const asyncValidate = createSpy().andReturn(Promise.resolve())
const props = { dispatch, startSubmit, stopSubmit, touch, setSubmitFailed, values }
const props = { dispatch, startSubmit, stopSubmit, touch, setSubmitFailed, setSubmitSuccess, values }

return handleSubmit(submit, props, true, asyncValidate, [ 'foo', 'baz' ])
.catch(result => {
Expand All @@ -280,6 +300,8 @@ describe('handleSubmit', () => {
.toHaveBeenCalledWith('foo', 'baz')
expect(setSubmitFailed)
.toNotHaveBeenCalled()
expect(setSubmitSuccess)
.toNotHaveBeenCalled()
})
})

Expand All @@ -292,9 +314,10 @@ describe('handleSubmit', () => {
const stopSubmit = createSpy()
const touch = createSpy()
const setSubmitFailed = createSpy()
const setSubmitSuccess = createSpy()
const asyncValidate = createSpy().andReturn(Promise.resolve())
const props = {
dispatch, startSubmit, stopSubmit, touch, setSubmitFailed, values
dispatch, startSubmit, stopSubmit, touch, setSubmitFailed, setSubmitSuccess, values
}

return handleSubmit(submit, props, true, asyncValidate, [ 'foo', 'baz' ])
Expand All @@ -315,6 +338,7 @@ describe('handleSubmit', () => {
.toHaveBeenCalled()
.toHaveBeenCalledWith('foo', 'baz')
expect(setSubmitFailed).toNotHaveBeenCalled()
expect(setSubmitSuccess).toNotHaveBeenCalled()
})
})
})
1 change: 1 addition & 0 deletions src/actionTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const INITIALIZE = 'redux-form/INITIALIZE'
export const REGISTER_FIELD = 'redux-form/REGISTER_FIELD'
export const RESET = 'redux-form/RESET'
export const SET_SUBMIT_FAILED = 'redux-form/SET_SUBMIT_FAILED'
export const SET_SUBMIT_SUCCESS = 'redux-form/SET_SUBMIT_SUCCESS'
export const START_ASYNC_VALIDATION = 'redux-form/START_ASYNC_VALIDATION'
export const START_SUBMIT = 'redux-form/START_SUBMIT'
export const STOP_ASYNC_VALIDATION = 'redux-form/STOP_ASYNC_VALIDATION'
Expand Down
5 changes: 4 additions & 1 deletion src/actions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
ARRAY_INSERT, ARRAY_MOVE, ARRAY_POP, ARRAY_PUSH, ARRAY_REMOVE, ARRAY_REMOVE_ALL, ARRAY_SHIFT,
ARRAY_SPLICE, ARRAY_SWAP, ARRAY_UNSHIFT, BLUR, CHANGE, DESTROY, FOCUS, INITIALIZE,
REGISTER_FIELD, RESET, SET_SUBMIT_FAILED, START_ASYNC_VALIDATION, START_SUBMIT,
REGISTER_FIELD, RESET, SET_SUBMIT_FAILED, SET_SUBMIT_SUCCESS, START_ASYNC_VALIDATION, START_SUBMIT,
STOP_ASYNC_VALIDATION, STOP_SUBMIT, TOUCH, UNREGISTER_FIELD, UNTOUCH
} from './actionTypes'

Expand Down Expand Up @@ -104,6 +104,9 @@ export const stopSubmit = (form, errors) => {
export const setSubmitFailed = (form, ...fields) =>
({ type: SET_SUBMIT_FAILED, meta: { form, fields }, error: true })

export const setSubmitSuccess = (form, result) =>
({ type: SET_SUBMIT_SUCCESS, meta: { form }, payload: result })

export const touch = (form, ...fields) =>
({ type: TOUCH, meta: { form, fields } })

Expand Down
6 changes: 5 additions & 1 deletion src/handleSubmit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import SubmissionError from './SubmissionError'

const handleSubmit = (submit, props, valid, asyncValidate, fields) => {
const {
dispatch, startSubmit, stopSubmit, setSubmitFailed, syncErrors, touch, values
dispatch, startSubmit, stopSubmit, setSubmitFailed, setSubmitSuccess, syncErrors, touch, values
} = props

touch(...fields) // mark all fields as touched
Expand All @@ -16,13 +16,17 @@ const handleSubmit = (submit, props, valid, asyncValidate, fields) => {
return result
.then(submitResult => {
stopSubmit()
setSubmitSuccess(submitResult)
return submitResult
}, submitError => {
const error = submitError instanceof SubmissionError ? submitError.errors : undefined
stopSubmit(error)
return Promise.reject(error)
})
} else {
setSubmitSuccess(result)
}

return result
}

Expand Down
1 change: 1 addition & 0 deletions src/immutable.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const {
propTypes,
reset,
setSubmitFailed,
setSubmitSuccess,
startAsyncValidation,
startSubmit,
stopAsyncValidation,
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const {
propTypes,
reset,
setSubmitFailed,
setSubmitSuccess,
startAsyncValidation,
startSubmit,
stopAsyncValidation,
Expand Down

0 comments on commit 7e07256

Please sign in to comment.