EditAddress.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import React, { Component } from 'react';
  2. import { View, Text, StyleSheet, TextInput, ScrollView } from 'react-native';
  3. import Button from '../../components/Button';
  4. import { CountryDropCode, GetCountryList } from '../../components/CountryIcon';
  5. import Dropdown from '../../components/Dropdown';
  6. export default class EditAddress extends Component {
  7. constructor(props) {
  8. super(props);
  9. this.state = {
  10. addressDto: {
  11. countryCode: "",
  12. city: "",
  13. street: "",
  14. zipCode: "",
  15. houseNumber: ""
  16. }
  17. };
  18. }
  19. componentDidMount() {
  20. const address = userInfo.address ?? {}
  21. if (!address.countryCode) {
  22. address.countryCode = "SG"
  23. }
  24. this.setState({
  25. addressDto: address,
  26. countryList: [],
  27. })
  28. userInfo.editAddress = false;
  29. GetCountryList(list => {
  30. this.setState({
  31. countryList: list
  32. })
  33. })
  34. }
  35. changeCountry(value, index) {
  36. this.state.addressDto.countryCode = value;
  37. this.setState({
  38. addressDto: this.state.addressDto
  39. })
  40. }
  41. saveInfo() {
  42. const dto = this.state.addressDto
  43. if (!dto.city) {
  44. toastShort($t('profile.plsInputCity'));
  45. return;
  46. }
  47. if (!dto.street) {
  48. toastShort($t('profile.plsInputStreet'));
  49. return;
  50. }
  51. if (!dto.houseNumber) {
  52. toastShort($t('profile.plsInputUnitNo'));
  53. return;
  54. }
  55. if (!dto.zipCode) {
  56. toastShort($t('profile.plsInputPostal'));
  57. return;
  58. }
  59. userInfo.address = dto;
  60. userInfo.addressLine = dto.houseNumber + ', ' + dto.street + ', ' + dto.zipCode
  61. console.log(userInfo.addressLine);
  62. userInfo.editAddress = true;
  63. goBack();
  64. }
  65. render() {
  66. return (
  67. <View style={styles.container}>
  68. <ScrollView style={ui.flex1}>
  69. <View style={styles.formView}>
  70. <Text style={styles.label}>{$t('sign.labelCountry')}</Text>
  71. <Dropdown
  72. style={styles.selectView}
  73. title={$t('sign.labelCountry')}
  74. list={this.state.countryList}
  75. value={this.state.addressDto.countryCode}
  76. nameKey='countryName'
  77. valueKey='countryCode'
  78. onChange={(value, index)=> this.changeCountry(value, index)}
  79. customerItemView={
  80. (item, index, onClick) =>
  81. <CountryDropCode
  82. key={index}
  83. country={item}
  84. value={this.state.addressDto.countryCode}
  85. onClick={onClick}/>
  86. }/>
  87. </View>
  88. <View style={styles.formView}>
  89. <Text style={styles.label}>{$t('profile.labelCity')}</Text>
  90. <TextInput
  91. style={styles.formInput}
  92. placeholder={$t('profile.labelCity')}
  93. maxLength={50}
  94. placeholderTextColor={textPlacehoder}
  95. defaultValue={this.state.addressDto?.city}
  96. onChangeText={text => this.state.addressDto.city = text}
  97. />
  98. </View>
  99. <View style={styles.formView}>
  100. <Text style={styles.label}>{$t('profile.street')}</Text>
  101. <TextInput
  102. style={styles.formInput}
  103. placeholder={$t('profile.hintStreet')}
  104. maxLength={50}
  105. placeholderTextColor={textPlacehoder}
  106. defaultValue={this.state.addressDto?.street}
  107. onChangeText={text => this.state.addressDto.street = text}
  108. />
  109. </View>
  110. <View style={styles.formView}>
  111. <Text style={styles.label}>{$t('profile.unitNumber')}</Text>
  112. <TextInput
  113. style={styles.formInput}
  114. placeholder={$t('profile.hintUnitNo')}
  115. maxLength={15}
  116. placeholderTextColor={textPlacehoder}
  117. defaultValue={this.state.addressDto?.houseNumber}
  118. onChangeText={text => this.state.addressDto.houseNumber = text}
  119. />
  120. </View>
  121. <View style={styles.formView}>
  122. <Text style={styles.label}>{$t('profile.postalCode')}</Text>
  123. <TextInput
  124. style={styles.formInput}
  125. placeholder={$t('profile.hintPostal')}
  126. maxLength={10}
  127. placeholderTextColor={textPlacehoder}
  128. defaultValue={this.state.addressDto?.zipCode}
  129. onChangeText={text => this.state.addressDto.zipCode = text}
  130. />
  131. </View>
  132. </ScrollView>
  133. <View style={styles.buttonView}>
  134. <Button
  135. text={$t('common.save')}
  136. elevation={1.5}
  137. onClick={() => {
  138. this.saveInfo();
  139. }}/>
  140. </View>
  141. </View>
  142. );
  143. }
  144. }
  145. const styles = StyleSheet.create({
  146. container: {
  147. flex: 1,
  148. backgroundColor: pageBackground
  149. },
  150. formView: {
  151. paddingTop: 16,
  152. paddingLeft: 16,
  153. paddingRight: 16,
  154. paddingBottom: 4
  155. },
  156. selectView: {
  157. marginTop: 4,
  158. marginBottom: -8,
  159. ...$padding(8, 0, 8, 4),
  160. alignItems: 'center',
  161. flexDirection: 'row',
  162. borderBottomColor: '#EEE',
  163. borderBottomWidth: 1
  164. },
  165. label: {
  166. color: textPrimary,
  167. fontSize: 14
  168. },
  169. formInput: {
  170. color: textPrimary,
  171. fontSize: 14,
  172. height: 44,
  173. paddingBottom: 2,
  174. borderBottomColor: '#EEE',
  175. borderBottomWidth: 1
  176. },
  177. buttonView: {
  178. padding: 16,
  179. marginBottom: 4
  180. }
  181. })