Skip to content

Instantly share code, notes, and snippets.

@zoeabryant
Created February 8, 2017 15:04
Show Gist options
  • Save zoeabryant/5a96d7e9fb84f782a70c6e723768a514 to your computer and use it in GitHub Desktop.
Save zoeabryant/5a96d7e9fb84f782a70c6e723768a514 to your computer and use it in GitHub Desktop.
// UI
const errorCodeToInput = {
missingFirstName: {
field: 'firstName',
message: 'MyDetails.Validation.FirstName'
},
invalidLengthFirstName: {
field: 'firstName',
message: 'MyDetails.Validation.FirstNameLength'
},
invalidFirstName: {
field: 'firstName',
message: 'MyDetails.Validation.FirstNameInvalid'
},
missingLastName: {
field: 'lastName',
message: 'MyDetails.Validation.LastName'
},
invalidLengthLastName: {
field: 'lastName',
message: 'MyDetails.Validation.LastNameLength'
},
invalidLastName: {
field: 'lastName',
message: 'MyDetails.Validation.LastNameInvalid'
},
missingEmailAddress: {
field: 'emailAddress',
message: 'MyDetails.Validation.EmailAddress'
},
invalidLengthEmailAddress: {
field: 'emailAddress',
message: 'MyDetails.Validation.EmailAddressLength'
},
invalidEmailAddress: {
field: 'emailAddress',
message: 'MyDetails.Validation.EmailAddressFormat'
},
emailAddressAlreadyTaken: {
field: 'emailAddress',
message: 'MyDetails.Validation.EmailAddressTaken'
},
invalidDateOfBirth: {
field: 'dateOfBirth',
message: 'MyDetails.Validation.DateOfBirthValue'
},
invalidGender: {
field: 'gender',
message: 'MyDetails.Validation.Gender'
},
missingGender: {
field: 'gender',
message: 'MyDetails.Validation.Gender'
},
}
const inputField = errorCodeToInput[errorCode];
// input: from state
errorCodes = [
'invalidLengthFirstName',
'invalidEmailAddress',
'invalidGender',
]
// output: give to redux form
errors = {
firstName: 'MyDetails.Validation.FirstNameLength',
emailAddress: 'MyDetails.Validation.EmailAddressFormat',
gender: 'MyDetails.Validation.Gender',
}
//reducer
const state = {
..state,
loaded: true,
fatalError: false,
errorCodes: [
'invalidLengthFirstName',
'invalidEmailAddress',
'invalidGender',
]
}
// response
Response: {
ok: false,
status: 400,
json: await () => (
[
{
"errorCode": "invalidLengthLastName",
"message": "lastName must not exceed 100 characters"
},
{
"errorCode": "invalidLengthLastName",
"message": "lastName must not exceed 100 characters"
},
]
)
}
// reducer input
Error: {
statusCode: 400
body: [
{
"errorCode": "invalidLengthLastName",
"message": "lastName must not exceed 100 characters"
},
{
"errorCode": "invalidLengthLastName",
"message": "lastName must not exceed 100 characters"
},
]
}
//reducer output
const state = {
..state,
loaded: true,
fatalError: false,
errorCodes: [
'invalidLengthFirstName',
'invalidEmailAddress',
'invalidGender',
]
}
// reducer scenarios:
// error
// successful payload
// response: {
// statusCode: 400,
// ok: false,
// body: [
// ...whatever
// ]
// }
// response: {
// statusCode: 200,
// ok: true,
// body: [
// ...whatever
// ]
// }
// const futurisedFetch = (
// path,
// options,
// fetchFn = futureFetch,
// getJsonFn = getJson,
// checkResponseFn = checkResponse,
// ) =>
// fetchFn(path, options)
// .chain(getJsonFn);
// export const checkResponse = (response) => Future((reject, resolve) => {
// if (!response.ok) {
// reject(response);
// } else {
// resolve(response);
// }
// });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment