Last active
May 6, 2016 18:42
-
-
Save cybersiddhu/980696bc8118d27654a2bae41bd23682 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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