Skip to content

Instantly share code, notes, and snippets.

@lancetw
Last active August 29, 2015 14:24
Show Gist options
  • Save lancetw/65362a954c444b205b67 to your computer and use it in GitHub Desktop.
Save lancetw/65362a954c444b205b67 to your computer and use it in GitHub Desktop.
import request from 'superagent'
import {
SIGNUP_USER_STARTED,
SIGNUP_USER_COMPLETED,
SIGNUP_USER_FAILED
} from 'shared/constants/ActionTypes'
import { auth } from 'shared/actions/AuthActions'
export function submit (form) {
return async dispatch => {
dispatch({ type: SIGNUP_USER_STARTED })
try {
const res = await sendForm(form)
if (res.email)
return dispatch({
type: SIGNUP_USER_COMPLETED,
response: await auth(form)
})
else
return dispatch({
type: SIGNUP_USER_FAILED,
errors: res.errors ? res.errors : res
})
} catch (err) {
return dispatch({
type: SIGNUP_USER_FAILED,
errors: err.message
})
}
}
}
async function sendForm (form) {
return new Promise((resolve, reject) => {
request
.post('/api/v1/users')
.send(form)
.set('Accept', 'application/json')
.end(function (err, res) {
if (!err && res.body)
resolve(res.body)
else
reject(err)
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment