EditProfile.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /**
  2. * Edit Profile页面
  3. * @邠心vbe on 2021/05/07
  4. */
  5. import React, { Component } from 'react';
  6. import { View, Text, StyleSheet, Pressable, Image, TextInput } from 'react-native';
  7. import ImagePicker from 'react-native-image-crop-picker';
  8. import Modal from 'react-native-modal';
  9. import apiUpload from '../../api/apiUpload';
  10. import apiUser from '../../api/apiUser';
  11. import { host } from '../../api/http';
  12. import { ModalProps } from '../../components/BottomModal';
  13. import Button, { ViewHeight } from '../../components/Button';
  14. import { CountryDropNum, GetCountryList } from '../../components/CountryIcon';
  15. import Dropdown from '../../components/Dropdown';
  16. import utils from '../../utils/utils';
  17. import { DialogMaxWidth } from '../charge/InfoDialog';
  18. import { PageList } from '../Router';
  19. //Image Picker参数
  20. const options = {
  21. cropping: true,
  22. multiple: false,
  23. minFiles: 1,
  24. maxFiles: 3,
  25. mediaType: 'photo',
  26. compressImageQuality: 0.8,
  27. cropperCircleOverlay: true,
  28. cropperStatusBarColor: colorAccent,
  29. cropperToolbarColor: colorAccent,
  30. cropperActiveWidgetColor: colorAccent
  31. }
  32. //向右的箭头
  33. const ArrowRight = () => {
  34. return (
  35. <Entypo
  36. size={16}
  37. color='#999'
  38. name='chevron-thin-right'
  39. style={{
  40. marginLeft: 16
  41. }}
  42. />
  43. );
  44. }
  45. //分割线
  46. const Divide = () => {
  47. return (
  48. <View style={{
  49. paddingLeft: 16,
  50. paddingRight: 16
  51. }}>
  52. <Text style={{
  53. height: 1,
  54. backgroundColor: '#eee'
  55. }}></Text>
  56. </View>
  57. );
  58. }
  59. //编辑弹窗
  60. const EditDialog = ({visible, title, value, keyType, countryList, areaNo, showCalling=false, onChangedText}) => {
  61. var changedText = value;
  62. var callingCode = areaNo;
  63. return (
  64. <Modal
  65. isVisible={visible}
  66. {...ModalProps}
  67. onBackdropPress={() => onChangedText('')}
  68. onBackButtonPress={() => onChangedText('')}>
  69. <View style={styles.dialog}>
  70. <Text style={styles.editNickname}>{title}</Text>
  71. <View style={ui.flexc}>
  72. { showCalling &&
  73. <Dropdown
  74. title='Country'
  75. prefixText="+"
  76. list={countryList}
  77. value={callingCode}
  78. nameKey='countryNum'
  79. valueKey='countryNum'
  80. style={styles.countryView}
  81. textStyle={styles.selectText}
  82. onChange={(value, index)=> {
  83. callingCode = value;
  84. }}
  85. customerItemView={
  86. (item, index, onClick) =>
  87. <CountryDropNum
  88. key={index}
  89. country={item}
  90. value={callingCode}
  91. onClick={onClick}/>
  92. }
  93. />
  94. }
  95. <TextInput
  96. style={styles.nickInput}
  97. defaultValue={value}
  98. keyboardType={keyType}
  99. onChangeText={text => {
  100. changedText = text;
  101. }}/>
  102. </View>
  103. <View style={styles.modalButtons}>
  104. <Button
  105. style={styles.btnCancel}
  106. viewStyle={ViewHeight(48)}
  107. textColor={colorAccent}
  108. text='Cancel'
  109. onClick={() => {
  110. onChangedText('');
  111. }}/>
  112. <Button
  113. style={styles.btnOk}
  114. viewStyle={ViewHeight(48)}
  115. text='Save'
  116. onClick={() => {
  117. onChangedText(changedText, callingCode);
  118. }}/>
  119. </View>
  120. {/* <Ionicons
  121. name='close'
  122. size={28}
  123. color={'#999'}
  124. style={styles.closeIcon}
  125. onPress={() => {
  126. onChangedText('');
  127. }} /> */}
  128. </View>
  129. </Modal>
  130. );
  131. }
  132. //页面
  133. export default class EditProfile extends Component {
  134. constructor(props) {
  135. super(props);
  136. this.state = {
  137. editDialog: {
  138. key: '',
  139. value: '',
  140. title: '',
  141. visible: false
  142. },
  143. userInfo: {},
  144. countryNums: []
  145. };
  146. this.editAddress = false;
  147. }
  148. componentDidMount() {
  149. /*this.props.navigation.addListener('beforeRemove', e => {
  150. if (this.canUpdate) {
  151. e.preventDefault();
  152. //console.log('----update---', userInfo);
  153. }
  154. });*/
  155. this.props.navigation.addListener('focus', e => {
  156. if (this.editAddress && userInfo.editAddress) {
  157. this.updateProfile(global.userInfo);
  158. }
  159. this.editAddress = false;
  160. });
  161. this.setState({
  162. userInfo: userInfo
  163. })
  164. GetCountryList(list => {
  165. this.setState({
  166. countryNums: list
  167. })
  168. })
  169. }
  170. updateProfile(_userInfo) {
  171. apiUser.setProfile(_userInfo).then(res => {
  172. toastShort('Update profile successfully');
  173. userInfo = _userInfo;
  174. this.setState({
  175. userInfo: _userInfo
  176. });
  177. //this.canUpdate = false;
  178. //goBack();
  179. }).catch(err => {
  180. //this.canUpdate = false;
  181. toastShort(err);
  182. });
  183. }
  184. hideDialog() {
  185. this.state.editDialog.visible = false;
  186. this.setState({
  187. editDialog: this.state.editDialog
  188. });
  189. }
  190. render() {
  191. return (
  192. <View style={styles.container}>
  193. <Pressable
  194. style={styles.profileView}
  195. android_ripple={ripple}
  196. onPress={() => {
  197. ImagePicker.openPicker({
  198. width: 200,
  199. height: 200,
  200. ...options
  201. }).then(image => {
  202. if (image.path) {
  203. apiUpload.uploadImage(image.path, image.mime, 'USER_PROFILE').then(res => {
  204. if (res.success && res.data.picturePath) {
  205. //this.canUpdate = true;
  206. this.state.userInfo.photoUrl = res.data.picturePath
  207. /*this.setState({
  208. userInfo: userInfo
  209. });*/
  210. this.updateProfile(this.state.userInfo);
  211. } else {
  212. toastShort('Upload failed, please retry');
  213. }
  214. }).catch(err => {
  215. toastShort(err);
  216. });
  217. }
  218. }).catch(err => {
  219. });
  220. }}>
  221. <Text style={styles.label}>Avatar</Text>
  222. <Text style={ui.flex1}></Text>
  223. { userInfo.photoUrl
  224. ? <Image
  225. style={styles.avatar}
  226. source={{uri: host + userInfo.photoUrl}}/>
  227. : <Image
  228. style={styles.avatar}
  229. source={require('../../images/user/ic-avatar-default.png')}/>
  230. }
  231. <ArrowRight/>
  232. </Pressable>
  233. <Divide/>
  234. <Pressable
  235. style={styles.profileView}
  236. android_ripple={ripple}
  237. onPress={() => {
  238. this.setState({
  239. editDialog: {
  240. key: 'nickName',
  241. title: 'Update Nickname',
  242. value: this.state.userInfo.nickName,
  243. visible: true
  244. }
  245. });
  246. }}>
  247. <Text style={styles.label}>Nickname</Text>
  248. <Text style={styles.infoText}>{this.state.userInfo.nickName}</Text>
  249. <ArrowRight/>
  250. </Pressable>
  251. <Divide/>
  252. <Pressable
  253. style={styles.profileView}
  254. android_ripple={ripple}
  255. onPress={() => {
  256. this.setState({
  257. editDialog: {
  258. key: 'phone',
  259. visible: true,
  260. showCalling: true,
  261. title: 'Update Phone Number',
  262. value: this.state.userInfo.phone,
  263. areaNo: this.state.userInfo.callingCode
  264. }
  265. });
  266. }}>
  267. <Text style={styles.label}>Phone Number</Text>
  268. <Text style={styles.infoText}>{(utils.isNotEmpty(this.state.userInfo.callingCode) && "+" + this.state.userInfo.callingCode)} {this.state.userInfo.phone}</Text>
  269. <ArrowRight/>
  270. </Pressable>
  271. <Divide/>
  272. <Pressable
  273. style={[styles.profileView, {
  274. paddingRight: 48
  275. }]}>
  276. <Text style={styles.label}>Email</Text>
  277. <Text style={styles.infoText}>{userInfo.email}</Text>
  278. {/*<ArrowRight/>*/}
  279. </Pressable>
  280. <Divide/>
  281. <Pressable
  282. style={styles.profileView}
  283. android_ripple={ripple}
  284. onPress={() => {
  285. /*this.setState({
  286. editDialog: {
  287. key: 'addressLine',
  288. title: 'Update Address',
  289. value: userInfo.addressLine,
  290. visible: true
  291. }
  292. });*/
  293. this.editAddress = true;
  294. startPage(PageList.editAddress);
  295. }}>
  296. <Text style={styles.label}>Address</Text>
  297. <Text style={styles.infoText}>{userInfo.addressLine}</Text>
  298. <ArrowRight/>
  299. </Pressable>
  300. <Divide/>
  301. <EditDialog
  302. title={this.state.editDialog.title}
  303. visible={this.state.editDialog.visible}
  304. value={this.state.editDialog.value}
  305. areaNo={this.state.editDialog.areaNo}
  306. countryList={this.state.countryNums}
  307. showCalling={this.state.editDialog.showCalling}
  308. keyType={this.state.editDialog.key == 'phone' ? 'phone-pad' : 'default'}
  309. onChangedText={(text, areaNo) => {
  310. if (text) {
  311. var info = Object.assign({}, userInfo);
  312. if (this.state.editDialog.key == 'phone') {
  313. if (!/^[0-9\+]\d{6,15}$/.test(text)) {
  314. toastShort('Phone Number is incorrect format');
  315. return;
  316. }
  317. info.callingCode = areaNo;
  318. }
  319. info[this.state.editDialog.key] = text;
  320. /*this.setState({
  321. userInfo: userInfo
  322. });
  323. this.canUpdate = true;*/
  324. this.updateProfile(info);
  325. }
  326. this.hideDialog();
  327. }}/>
  328. </View>
  329. );
  330. }
  331. }
  332. const styles = StyleSheet.create({
  333. container: {
  334. flex: 1,
  335. backgroundColor: 'white'
  336. },
  337. profileView: {
  338. padding: 16,
  339. alignItems: 'center',
  340. flexDirection: 'row'
  341. },
  342. label: {
  343. color: '#666',
  344. fontSize: 14
  345. },
  346. infoText: {
  347. flex: 1,
  348. color: '#333',
  349. fontSize: 14,
  350. paddingLeft: 16,
  351. textAlign: 'right'
  352. },
  353. avatar: {
  354. width: 62,
  355. height: 62,
  356. borderRadius: 80,
  357. },
  358. dialog: {
  359. width: DialogMaxWidth,
  360. paddingTop: 16,
  361. paddingLeft: 20,
  362. paddingRight: 20,
  363. paddingBottom: 16,
  364. marginLeft: 'auto',
  365. marginRight: 'auto',
  366. backgroundColor: 'white',
  367. borderRadius: isIOS ? 20 : 3
  368. },
  369. editNickname: {
  370. color: '#000',
  371. fontSize: 18,
  372. textAlign: 'center'
  373. },
  374. nickInput: {
  375. flex: 1,
  376. color: '#333',
  377. fontSize: 14,
  378. paddingTop: 8,
  379. paddingLeft: 16,
  380. paddingRight: 16,
  381. paddingBottom: 8,
  382. borderRadius: 4,
  383. marginTop: 24,
  384. backgroundColor: '#F6F6F6'
  385. },
  386. modalButtons: {
  387. marginTop: 24,
  388. paddingBottom: 16,
  389. alignItems: 'center',
  390. flexDirection: 'row'
  391. },
  392. btnCancel: {
  393. flex: 1,
  394. borderWidth: 1,
  395. borderColor: colorAccent,
  396. backgroundColor: 'white'
  397. },
  398. btnOk: {
  399. flex: 1,
  400. marginLeft: 16,
  401. borderWidth: 1,
  402. borderColor: colorAccent,
  403. },
  404. closeIcon: {
  405. top: 12,
  406. right: 12,
  407. position: "absolute"
  408. },
  409. countryView: {
  410. width: 80,
  411. marginTop: 24,
  412. marginRight: 8,
  413. minHeight: 43,
  414. borderRadius: 4,
  415. flexDirection: 'row',
  416. backgroundColor: '#F6F6F6'
  417. },
  418. selectText: {
  419. color: '#333',
  420. fontSize: 15,
  421. ...$padding(11, 10),
  422. },
  423. });