Skip to content

Instantly share code, notes, and snippets.

@cybersiddhu
Last active May 6, 2016 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cybersiddhu/980696bc8118d27654a2bae41bd23682 to your computer and use it in GitHub Desktop.
Save cybersiddhu/980696bc8118d27654a2bae41bd23682 to your computer and use it in GitHub Desktop.
/*
* Return a promise object
* On success, it passes (response, true/false) to the resolve
* passes true if the user exists
* false if the user does not exist
* On error passes the error object, the structure should
* be similar as defined in the JSON AP specification
*
*/
const userExists = (email) => {
let config = {
method: 'GET',
headers: { 'Content-Type': 'application/json' }
}
fetch(`${server}/users/${email}`, config)
.then(response => {
if (response.status === 200) {
return Promise.resolve(response, true)
} else if (response.status === 404) {
return Promise.resolve(response, false)
} else {
return Promise.reject(new Error('Error'))
}
})
}
export const submitShippingAddress = (values, dispatch) => {
const email = values.email
return new Promise((resolve, reject) => {
userExists(email)
.then((response, isthere) => {
if (isthere) { # HTTP GET with 200
# PATCH it up
# run another separate method that returns promise
# it will look like this
patchDataStr = # write some code
updateUser(patchDataStr)
.then(patchResponse => {
dispatch("... to payee screen") # update reducer with state
resolve()
})
.catch(error => {
dispatch("... show error")
reject({})
})
} else {
# HTTP GET with 404, no user
# POST the user
# run the create user method which returns promise
postDataStr = # write some code
createUser(postDataStr)
.then(response => {
dispatch("... to payee screen") # update reducer with state
resolve()
})
.catch(error => {
dispatch("show error")
reject({})
})
}
})
.catch(error => {
dispatch("...show error")
reject({})
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment