This example shows how you can chain up lodash.get()
commands to put priority on multiple data sources combined with the same naming of fields
Last active
October 4, 2019 19:14
-
-
Save stephenway/3682d43da3da0175f4062ce39d7fedb2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* | |
* AccountHolder Default Values | |
*/ | |
import find from 'lodash.find' | |
import get from 'lodash.get' | |
import { | |
AccountHolderMutationVariables, AccountTypeEnum, GetAccountHolderQuery, GetPersonQuery | |
} from '@types-generated' | |
import addressDefaultValues from 'components/AddressSubform/AddressSubform.i' | |
import patriotActDefaultValues from 'components/PatriotActSubform/PatriotActSubform.i' | |
import personDefaultValues, { | |
simplePersonDefaultValues | |
} from 'components/PersonSubform/PersonSubform.i' | |
import * as C from './AccountHolder.c' | |
export default (personData?: GetPersonQuery, submissionData?: GetAccountHolderQuery['submission']['data']) => { | |
const patriotAct = get(submissionData, 'patriotAct') | |
const subAddress = get(submissionData, 'address') | |
return { | |
[C.ACCOUNT_TYPE]: get(submissionData, C.ACCOUNT_TYPE) || AccountTypeEnum.Individual, | |
[C.EXISTING_ACCOUNTS]: get(submissionData, C.EXISTING_ACCOUNTS) || false, | |
...personDefaultValues( | |
get(personData, 'currentUser') || undefined, | |
get(submissionData, 'accountHolder') || undefined, | |
), | |
[C.ADDRESS]: [ | |
addressDefaultValues( | |
'primary', | |
get(personData, 'currentUser.permanentAddress'), | |
find(subAddress, ['type', 'permanent']), | |
), | |
addressDefaultValues( | |
'mailing', | |
get(personData, 'currentUser.mailingAddress'), | |
find(subAddress, ['type', 'mailing']), | |
), | |
], | |
[C.RESIDENT_CHECK]: get(submissionData, 'accountHolder.residentCheck') || false, | |
[C.GET_MAILING]: | |
get(find(subAddress, ['type', 'mailing']), 'line1') || get(personData, 'accountHolder.mailingAddress.line1') | |
? true | |
: false, | |
[C.MARITAL_STATUS]: get(submissionData, 'maritalStatus') || '', | |
[C.DEPENDENTS]: get(submissionData, 'dependents') || 0, | |
[C.PATRIOT_ACT]: patriotActDefaultValues(patriotAct), | |
// TODO: Add co-applicant data when added to schema | |
[C.CO_APPLICANT]: simplePersonDefaultValues, | |
} as AccountHolderMutationVariables | |
} |
This file contains hidden or 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 get from 'lodash.get' | |
import { AddressDocument, Maybe } from '@types-generated' | |
import * as C from './AddressSubform.c' | |
export default ( | |
type: AddressDocument['type'], | |
personAddress?: Maybe<AddressDocument>, | |
submissionAddress?: Maybe<AddressDocument>, | |
) => { | |
const getField = (field: string, fallback?: string) => | |
get(submissionAddress, field) || get(personAddress, field) || fallback || '' | |
return { | |
[C.ADDRESS_TYPE]: type || '', | |
[C.ADDRESS_LINE1]: getField(C.ADDRESS_LINE1), | |
[C.ADDRESS_LINE2]: getField(C.ADDRESS_LINE2), | |
[C.STATE]: getField(C.STATE), | |
[C.CITY]: getField(C.CITY), | |
[C.POSTAL_CODE]: getField(C.POSTAL_CODE), | |
[C.COUNTRY]: getField(C.COUNTRY, 'US'), | |
[C.PROOF]: getField(C.PROOF), | |
} as AddressDocument | |
} |
This file contains hidden or 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 { IDENTIFICATION } from './IdentificationSubform.c' | |
export default (data?: string[] | null) => ({ | |
[IDENTIFICATION]: data || [], | |
}) |
This file contains hidden or 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 get from 'lodash.get' | |
import { AccountSchema } from '@types-generated' | |
import identificationSubformDefaultValues from 'components/IdentificationSubform/IdentificationSubform.i' | |
import * as C from './PatriotActSubform.c' | |
export default (data?: AccountSchema['patriotAct']) => ({ | |
[C.COUNTRY_CITIZENSHIP]: get(data, C.COUNTRY_CITIZENSHIP) || 'USA', | |
[C.BIRTHDAY]: get(data, C.BIRTHDAY) || '', | |
[C.TAX_ID]: get(data, C.TAX_ID) || '', | |
...identificationSubformDefaultValues(get(data, C.IDENTIFICATION)), | |
}) |
This file contains hidden or 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 get from 'lodash.get' | |
import { AccountSchema, GetPersonQuery } from '@types-generated' | |
import phoneDefaultValues from 'components/PhoneSubform/PhoneSubform.i' | |
import * as C from './PersonSubform.c' | |
export const simplePersonDefaultValues = { | |
[C.GIVEN_NAME]: '', | |
[C.FAMILY_NAME]: '', | |
[C.EMAIL]: '', | |
} | |
export default ( | |
personData?: GetPersonQuery['currentUser'] | undefined, | |
submissionData?: AccountSchema['accountHolder'], | |
) => { | |
const getField = (field: string, fallback?: string) => | |
get(submissionData, field) || get(personData, field) || fallback || '' | |
return { | |
[C.HONORIFIC_PREFIX]: getField(C.HONORIFIC_PREFIX, undefined), | |
[C.GIVEN_NAME]: getField(C.GIVEN_NAME), | |
[C.ADDITIONAL_NAME]: getField(C.ADDITIONAL_NAME), | |
[C.FAMILY_NAME]: getField(C.FAMILY_NAME), | |
[C.HONORIFIC_SUFFIX]: getField(C.HONORIFIC_SUFFIX, undefined), | |
[C.EMAIL]: getField(C.EMAIL), | |
...phoneDefaultValues(getField(C.TEL_NATIONAL, undefined)), | |
} as GetPersonQuery['currentUser'] | |
} |
This file contains hidden or 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
/** | |
* | |
* PhoneSubform Default Values | |
*/ | |
import * as C from './PhoneSubform.c' | |
export default (userPhone?: number) => ({ | |
[C.PHONES]: userPhone || undefined, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment