Last active
January 5, 2018 10:16
-
-
Save fernandocamargo/26144729f16bfa7312f830e002befbbf 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
import { pick } from 'ramda'; | |
import { | |
ALREADY_EXISTS, | |
COMMUNICATION_ID_NOT_FOUND, | |
GENERAL_SIGNUP_ERROR, | |
} from 'constants/signUp'; | |
import { | |
ATTRACTION_ALREADY_ASSIGNED_TO_COMMUNICATION_ID as ALREADY_EXISTS_ERROR_CODE, | |
COMMUNICATION_ID_NOT_FOUND as COMMUNICATION_ID_NOT_FOUND_ERROR_CODE, | |
} from 'common/enum/ErrorCode'; | |
import * as API from 'api'; | |
export const translate = code => | |
({ | |
[ALREADY_EXISTS_ERROR_CODE]: ALREADY_EXISTS, | |
[COMMUNICATION_ID_NOT_FOUND_ERROR_CODE]: COMMUNICATION_ID_NOT_FOUND, | |
}[code] || GENERAL_SIGNUP_ERROR); | |
export const using = pick(['communicationId', 'name', 'email', 'password']); | |
export const getErrorFrom = ({ error: { errorCode } }) => translate(errorCode); | |
export const deny = response => Promise.reject(getErrorFrom(response)); | |
export const allow = response => Promise.resolve(response); | |
export const check = response => (response.error ? deny : allow)(response); | |
export const signUp = data => API.signUp(using(data)).then(check); | |
export default { | |
signUp, | |
}; |
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
import { pick } from 'ramda'; | |
import { | |
ALREADY_EXISTS, | |
COMMUNICATION_ID_NOT_FOUND, | |
GENERAL_SIGNUP_ERROR, | |
} from 'constants/signUp'; | |
import { | |
ATTRACTION_ALREADY_ASSIGNED_TO_COMMUNICATION_ID as ALREADY_EXISTS_ERROR_CODE, | |
COMMUNICATION_ID_NOT_FOUND as COMMUNICATION_ID_NOT_FOUND_ERROR_CODE, | |
} from 'common/enum/ErrorCode'; | |
import * as api from 'api'; | |
const errorCodesToConstants = { | |
[ALREADY_EXISTS_ERROR_CODE]: ALREADY_EXISTS, | |
[COMMUNICATION_ID_NOT_FOUND_ERROR_CODE]: COMMUNICATION_ID_NOT_FOUND, | |
}; | |
const pickSignUpData = pick(['communicationId', 'name', 'email', 'password']); | |
export default { | |
// load: ({ params: { activationCode } }) => | |
// activationCode && api.userActivate(activationCode), | |
signUp: data => | |
api | |
.signUp(pickSignUpData(data)) | |
.then( | |
response => | |
response.error | |
? Promise.reject( | |
errorCodesToConstants[response.error.errorCode] || | |
GENERAL_SIGNUP_ERROR, | |
) | |
: Promise.resolve(response), | |
), | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment