EditProfile.js 11 KB

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