Created
February 1, 2017 16:25
-
-
Save KTruong008/24e531afc98b7460294e0684aed3257d 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
autofillCityProvince = (defaultValues) => { | |
this.setCurrentValue( | |
this.safeKey('financing_city'), | |
defaultValues.city, | |
false | |
); | |
this.setCurrentValue( | |
this.safeKey('financing_province'), | |
defaultValues.province, | |
false | |
); | |
} | |
case 'financing_street_name': | |
financingStreetNameChanged( | |
value, | |
this.currentValue(this.safeKey('financing_postal_code')), | |
this.fieldStateUpdaters[this.safeKey('financing_city')].updateTextValue, | |
this.fieldStateUpdaters[this.safeKey('financing_province')].updateTextValue | |
); | |
break; | |
// services | |
export function financingStreetNameChanged( | |
streetName, | |
postalCode, | |
financingCityHandler, | |
financingProvinceHandler | |
) { | |
if (streetName && postalCode && financingCityHandler && financingProvinceHandler) { | |
financingCityHandler(streetName, postalCode); | |
financingProvinceHandler(streetName, postalCode); | |
} | |
} | |
// CityField | |
updateTextValue = (streetNameOrTypeAndName, postalCode, addressType='civic') => { | |
this.setState({ updating: true }); | |
// updateTextValue works slightly differently if called with streetName vs. typeAndName (for PO box addresses) | |
const cityProvince = new Promise((resolve, reject) => { | |
getCityProvinceFromStreetNamePostalCode( | |
streetNameOrTypeAndName, postalCode, addressType, resolve, reject | |
); | |
}); | |
cityProvince.then((response) => { | |
this.setValue({}, { | |
name: this.attrs.name, | |
value: response.city, | |
label: response.city, | |
}); | |
this.setState({ updating: false }); | |
}) | |
.catch((error) => {}); | |
} | |
} | |
// ProvinceField | |
updateTextValue = (streetNameOrTypeAndName, postalCode, addressType='civic') => { | |
this.setState({ updating: true }); | |
const cityProvince = new Promise((resolve, reject) => { | |
getCityProvinceFromStreetNamePostalCode( | |
streetNameOrTypeAndName, postalCode, addressType, resolve, reject | |
); | |
}); | |
cityProvince.then((response) => { | |
this.setValue({}, { | |
name: this.attrs.name, | |
value: response.province, | |
label: response.province, | |
}); | |
this.setState({ updating: false }); | |
}) | |
.catch((error) => { | |
}); | |
} | |
// fetch.js | |
export function getCityProvinceFromStreetNamePostalCode( | |
streetNameOrTypeAndName, postalCode, addressType='civic', resolve, reject | |
) { | |
api.post( | |
JASMINE_POSTAL_CODE_ROOT_URL, | |
{ postal_code: postalCode } | |
).then((addresses) => { | |
let correctAddress; | |
if (addressType === 'civic') { | |
correctAddress = _.find(addresses.content, (address) => { | |
return address.name === streetNameOrTypeAndName; | |
}); | |
} else { | |
correctAddress = _.find(addresses.content, (address) => { | |
const typeName = streetNameOrTypeAndName.split(' '); | |
const typeDescription = typeName[0]; | |
const qualifierName = typeName[1]; | |
return ( | |
address.qualifier_name === qualifierName && address.type_description === typeDescription | |
); | |
}); | |
} | |
const cityProvince = correctAddress.city.name | |
.split(',') | |
.map((item) => { | |
return item.trim(); | |
}); | |
const data = { | |
city: cityProvince[0], | |
province: cityProvince[1], | |
}; | |
resolve(data); | |
}).catch((error) => { | |
reject(error) | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment