Profile.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /**
  2. * Profile Settings页面
  3. * @邠心vbe on 2021/04/27
  4. */
  5. import React, { Component } from 'react';
  6. import { View, Text, StyleSheet, Image, ScrollView, Switch, Pressable } from 'react-native';
  7. import apiUser from '../../api/apiUser';
  8. import { host } from '../../api/http';
  9. import Button, { ElevationObject } from '../../components/Button';
  10. import Dialog from '../../components/Dialog';
  11. import { StationBack } from '../../components/Toolbar';
  12. import ProfileBackground from '../../icons/ProfileBackground';
  13. import TopChargeBackground from '../../icons/TopChargeBackground';
  14. import utils from '../../utils/utils';
  15. import { PageList } from '../Router';
  16. import VehicleList from './VehicleList';
  17. export default class Profile extends Component {
  18. constructor(props) {
  19. super(props);
  20. this.state = {
  21. userInfo: {
  22. notifyLowBalance: true,
  23. notifyChargingComplete: true,
  24. notifyPromotionsOffers: true
  25. },
  26. refreshId: 0,
  27. totalVehicle: 0
  28. };
  29. this.changed = false;
  30. }
  31. componentDidMount() {
  32. this.props.navigation.addListener('focus', () => {
  33. getUserInfo(info => {
  34. this.setState({
  35. userInfo: info,
  36. refreshId: this.state.refreshId + 1
  37. });
  38. }, true);
  39. });
  40. }
  41. componentWillUnmount() {
  42. if (this.changed) {
  43. this.setChangedSwitch();
  44. }
  45. }
  46. removeVehicle(id) {
  47. Dialog.showDialog({
  48. title: 'Remove Vehicle',
  49. message: 'Are you sure you want to remove this vehicle?',
  50. callback: btn => {
  51. if (btn == 'ok') {
  52. Dialog.dismissLoading();
  53. this.deleteVehicle(id);
  54. }
  55. }
  56. })
  57. }
  58. deleteVehicle(id) {
  59. Dialog.showProgressDialog();
  60. apiUser.deleteVehicle({
  61. vehiclePk: id
  62. }).then(res => {
  63. Dialog.dismissLoading();
  64. toastShort('Delete successfully');
  65. this.setState({
  66. refreshId: this.state.refreshId + 1
  67. })
  68. }).catch(err => {
  69. Dialog.dismissLoading();
  70. toastShort(err);
  71. });
  72. }
  73. changeSwitch(key, value) {
  74. userInfo[key] = value;
  75. this.setState({
  76. userInfo: userInfo
  77. });
  78. this.changed = true;
  79. }
  80. setChangedSwitch() {
  81. apiUser.setNotifySwitch(userInfo);
  82. }
  83. deleteAccount() {
  84. Dialog.showDialog({
  85. title: 'Delete My Account',
  86. message: 'Are you sure you want to delete your account? This operation cannot be revoke.',
  87. ok: 'CONFIRM',
  88. callback: button => {
  89. if (button == Dialog.BUTTON_OK) {
  90. this.deleteMyAccount();
  91. }
  92. }
  93. })
  94. }
  95. deleteMyAccount() {
  96. Dialog.showProgressDialog();
  97. apiUser.deleteAccount().then(res => {
  98. toastShort('Successfully deleted!')
  99. Dialog.dismissLoading();
  100. setTimeout(() => {
  101. startPage(PageList.login);
  102. }, 500);
  103. }).catch(err => {
  104. Dialog.dismissLoading();
  105. toastShort(err)
  106. })
  107. }
  108. render() {
  109. return (
  110. <ScrollView style={styles.container}>
  111. {/* Profile Info */}
  112. <View style={styles.headerView}>
  113. <View style={styles.background}>
  114. {/* <ProfileBackground/> */}
  115. </View>
  116. {/* <Image
  117. style={styles.background}
  118. source={require('../../images/user/bg-profile.svg')}/> */}
  119. <Pressable
  120. style={styles.profileView}
  121. onPress={() => {
  122. startPage(PageList.editProfile);
  123. }}>
  124. { userInfo.photoUrl
  125. ? <Image
  126. style={styles.avatar}
  127. source={{uri: host + userInfo.photoUrl}}/>
  128. : <Image
  129. style={styles.avatar}
  130. source={require('../../images/user/icon-default.jpg')}/>
  131. }
  132. <View style={styles.infoContent}>
  133. <Text
  134. style={styles.nickname}
  135. ellipsizeMode='tail'
  136. numberOfLines={1}>{userInfo.nickName}</Text>
  137. <Text style={styles.userText}>{userInfo.email}</Text>
  138. <Text style={styles.userText}>{(utils.isNotEmpty(userInfo.callingCode) && "+" + userInfo.callingCode) + userInfo.phone}</Text>
  139. </View>
  140. <FontAwesome
  141. size={34}
  142. color='#333'
  143. name='angle-right'/>
  144. </Pressable>
  145. <StationBack bottom={52} scale={0.8}/>
  146. </View>
  147. {/* Summary Info */}
  148. <View style={styles.cardView}>
  149. <View style={styles.cardItem}>
  150. <Image
  151. style={styles.cardIcon}
  152. source={require('../../images/user/card-vehicle.png')}/>
  153. <View style={styles.cardInfo}>
  154. <Text style={styles.cardPrimary}>{this.state.totalVehicle}</Text>
  155. <Text style={styles.cardLabel}>My Vehicles</Text>
  156. </View>
  157. </View>
  158. <View style={[styles.cardItem, styles.cardDivide]}>
  159. <Image
  160. style={styles.cardIcon}
  161. source={require('../../images/user/card-wallet.png')}/>
  162. <Pressable
  163. style={styles.cardInfo}
  164. onPress={() => {
  165. startPage(PageList.wallet);
  166. }}>
  167. <Text style={styles.cardPrimary}>{currency}{userInfo.credit}</Text>
  168. <Text style={styles.cardLabel}>Credits</Text>
  169. </Pressable>
  170. </View>
  171. </View>
  172. {/* Vehicle List */}
  173. <View style={styles.titleView}>
  174. <Text style={styles.title}>My Vehicles</Text>
  175. <Button
  176. style={{backgroundColor: '#fff'}}
  177. borderRadius={3}
  178. viewStyle={styles.titleAdd}
  179. onClick={() => {
  180. startPage(PageList.addVehicle)
  181. }}>
  182. <Entypo name='plus' size={14} color='#333'/>
  183. <Text style={{fontSize: 12, paddingLeft: 1, color: '#333'}}>Add Vehicle</Text>
  184. </Button>
  185. </View>
  186. <View style={styles.verhicleList}>
  187. <VehicleList
  188. refreshId={this.state.refreshId}
  189. onResult={count => {
  190. this.setState({
  191. totalVehicle: count
  192. })
  193. }}
  194. onDelete={id => this.removeVehicle(id)}
  195. />
  196. </View>
  197. {/* Account List */}
  198. {/* <View style={styles.titleView}>
  199. <Text style={styles.title}>My Cards</Text>
  200. <Button
  201. textColor='#333'
  202. style={styles.titleAdd}
  203. onClick={() => {
  204. startPage(PageList.addCard)
  205. }}>
  206. <Entypo name='plus' size={14} color='#333'/>
  207. <Text style={{fontSize: 12, paddingLeft: 1}}>Add Card</Text>
  208. </Button>
  209. </View>
  210. <View style={styles.accountList}>
  211. <CardList refreshId={this.state.refreshId}/>
  212. </View> */}
  213. {/* Notifications */}
  214. <View style={styles.titleView}>
  215. <Text style={styles.title}>Notifications</Text>
  216. </View>
  217. <View style={styles.notificationView}>
  218. <Pressable
  219. style={styles.notificationItem}
  220. android_ripple={ripple}
  221. onPress={() => {
  222. this.changeSwitch("notifyChargingComplete", !userInfo.notifyChargingComplete);
  223. }}>
  224. <Text style={styles.notiLabel}>Notify me when charging complete</Text>
  225. <Switch
  226. value={this.state.userInfo.notifyChargingComplete}
  227. trackColor={isIOS ? { false: "#B2B2B2", true: colorAccent } : null}
  228. onValueChange={v => {
  229. this.changeSwitch("notifyChargingComplete", v);
  230. }}/>
  231. </Pressable>
  232. <Pressable
  233. style={[styles.notificationItem, styles.divide]}
  234. android_ripple={ripple}
  235. onPress={() => {
  236. this.changeSwitch("notifyLowBalance", !userInfo.notifyLowBalance);
  237. }}>
  238. <Text style={styles.notiLabel}>Notify me when credits is low balance</Text>
  239. <Switch
  240. value={this.state.userInfo.notifyLowBalance}
  241. trackColor={isIOS ? { false: "#B2B2B2", true: colorAccent } : null}
  242. onValueChange={v => {
  243. this.changeSwitch("notifyLowBalance", v);
  244. }}/>
  245. </Pressable>
  246. <Pressable
  247. style={[styles.notificationItem, styles.divide]}
  248. android_ripple={ripple}
  249. onPress={() => {
  250. this.changeSwitch("notifyPromotionsOffers", !userInfo.notifyPromotionsOffers);
  251. }}>
  252. <Text style={styles.notiLabel}>Notify me for promotions and offers</Text>
  253. <Switch
  254. value={this.state.userInfo.notifyPromotionsOffers}
  255. trackColor={isIOS ? { false: "#B2B2B2", true: colorAccent } : null}
  256. onValueChange={v => {
  257. this.changeSwitch("notifyPromotionsOffers", v);
  258. }}/>
  259. </Pressable>
  260. </View>
  261. <Button
  262. style={styles.deleteButton}
  263. text="DELETE MY ACCOUNT"
  264. textColor="#fff"
  265. onClick={() => this.deleteAccount()}
  266. />
  267. </ScrollView>
  268. );
  269. }
  270. }
  271. const styles = StyleSheet.create({
  272. container: {
  273. flex: 1,
  274. backgroundColor: 'white'
  275. },
  276. headerView: {
  277. paddingBottom: 72
  278. },
  279. background: {
  280. left: 0,
  281. right: 0,
  282. bottom: 0,
  283. height: $vw(62.4),
  284. alignItems: 'center',
  285. position: 'absolute'
  286. },
  287. profileView: {
  288. padding: 16,
  289. zIndex: 10,
  290. alignItems: 'center',
  291. flexDirection: 'row'
  292. },
  293. avatar: {
  294. width: 66,
  295. height: 66,
  296. borderWidth: 2,
  297. borderRadius: 80,
  298. borderColor: '#fff'
  299. },
  300. infoContent: {
  301. flex: 1,
  302. paddingLeft: 16,
  303. },
  304. nickname: {
  305. color: '#000',
  306. fontSize: 16,
  307. paddingTop: 1,
  308. paddingBottom: 1.5,
  309. },
  310. userText: {
  311. color: '#333',
  312. fontSize: 13,
  313. paddingTop: 1.5
  314. },
  315. cardView: {
  316. padding: 16,
  317. marginTop: -56,
  318. marginLeft: 16,
  319. marginRight: 16,
  320. borderRadius: 10,
  321. ...ElevationObject(2),
  322. alignItems: 'center',
  323. flexDirection: 'row',
  324. backgroundColor: 'white',
  325. },
  326. cardItem: {
  327. flex: 1,
  328. alignItems: 'center',
  329. flexDirection: 'row',
  330. justifyContent: 'center'
  331. },
  332. cardDivide: {
  333. borderLeftWidth: 1,
  334. borderLeftColor: '#EEE'
  335. },
  336. cardIcon: {
  337. width: 32,
  338. height: 32
  339. },
  340. cardInfo: {
  341. paddingLeft: 16,
  342. alignItems: 'center'
  343. },
  344. cardPrimary: {
  345. color: '#333',
  346. fontSize: 18,
  347. paddingBottom: 2
  348. },
  349. cardLabel: {
  350. color: '#333',
  351. fontSize: 13,
  352. },
  353. titleView: {
  354. padding: 12,
  355. alignItems: 'center',
  356. flexDirection: 'row'
  357. },
  358. title: {
  359. flex: 1,
  360. padding: 8,
  361. color: '#000',
  362. fontSize: 16,
  363. },
  364. titleAdd: {
  365. padding: 8,
  366. color: '#333',
  367. alignItems: 'center',
  368. flexDirection: 'row'
  369. },
  370. verhicleList: {
  371. paddingLeft: 16,
  372. paddingRight: 16,
  373. marginBottom: -16
  374. },
  375. accountList: {
  376. paddingLeft: 16,
  377. paddingRight: 16,
  378. marginBottom: -16
  379. },
  380. notificationView: {
  381. marginLeft: 16,
  382. marginRight: 16,
  383. marginBottom: 20,
  384. borderRadius: 10,
  385. overflow: 'hidden',
  386. paddingLeft: 16,
  387. paddingRight: 16,
  388. borderColor: '#f5f5f5',
  389. borderWidth: 1,
  390. backgroundColor: 'white',
  391. ...ElevationObject(1.5)
  392. },
  393. notificationItem: {
  394. paddingTop: 16,
  395. paddingBottom: 16,
  396. alignItems: 'center',
  397. flexDirection: 'row'
  398. },
  399. notiLabel: {
  400. flex: 1,
  401. color: '#333',
  402. fontSize: 14
  403. },
  404. divide: {
  405. borderTopWidth: 1,
  406. borderTopColor: '#eee'
  407. },
  408. deleteButton: {
  409. ...$margin(32, 16, 16),
  410. backgroundColor: '#EA0A2A'
  411. }
  412. })