VehicleList.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /**
  2. * 车辆列表
  3. * @邠心vbe on 2021/06/08
  4. */
  5. import React, { Component } from 'react';
  6. import { View, Text, Image, StyleSheet, Pressable } from 'react-native';
  7. import apiUser from '../../api/apiUser';
  8. import { ElevationObject } from '../../components/Button';
  9. import Dialog from '../../components/Dialog';
  10. import VehicleType from '../../icons/VehicleType';
  11. import { PageList } from '../Router';
  12. export default class VehicleList extends Component {
  13. constructor(props) {
  14. super(props);
  15. this.state = {
  16. vehicleList: [],
  17. refreshId: 0,
  18. };
  19. }
  20. componentDidMount() {
  21. //this.getVehicleList();
  22. this.props.navigation.addListener('focus', () => {
  23. this.getVehicleList();
  24. })
  25. }
  26. componentDidUpdate() {
  27. if (this.state.refreshId != this.props.refreshId) {
  28. this.setState({
  29. refreshId: this.props.refreshId,
  30. });
  31. this.getVehicleList();
  32. }
  33. }
  34. getVehicleList() {
  35. apiUser.getVehicles().then(res => {
  36. if (res.data) {
  37. this.setState({
  38. vehicleList: res.data
  39. });
  40. if (this.props.onResult) {
  41. this.props.onResult(res.data.length)
  42. }
  43. }
  44. }).catch(err => {
  45. this.setState({
  46. vehicleList: []
  47. });
  48. if (this.props.onResult) {
  49. this.props.onResult(0)
  50. }
  51. });
  52. }
  53. removeVehicle(id) {
  54. Dialog.showDialog({
  55. title: $t('profile.removeVehicle'),
  56. message: $t('profile.tipRemoveVehicle'),
  57. callback: btn => {
  58. if (btn == 'ok') {
  59. Dialog.dismissLoading();
  60. this.deleteVehicle(id);
  61. }
  62. }
  63. })
  64. }
  65. deleteVehicle(id) {
  66. Dialog.showProgressDialog();
  67. apiUser.deleteVehicle({
  68. vehiclePk: id
  69. }).then(res => {
  70. Dialog.dismissLoading();
  71. toastShort($t('common.deleteSuccess'));
  72. this.getVehicleList();
  73. }).catch(err => {
  74. Dialog.dismissLoading();
  75. toastShort(err);
  76. });
  77. }
  78. render() {
  79. return (
  80. this.state.vehicleList.length > 0
  81. ? this.state.vehicleList.map((item, index) => {
  82. //const type = getConnectTypeByKey(item.connectorType)
  83. return (
  84. <Pressable
  85. key={index}
  86. style={styles.vehicleView}
  87. onPress={() => {
  88. startPage(PageList.editVehicle, {id: item.vehiclePk});
  89. }}
  90. onLongPress={() => {
  91. if (this.props.onDelete) {
  92. this.props.onDelete(item.vehiclePk)
  93. } else {
  94. this.removeVehicle(item.vehiclePk)
  95. }
  96. }}>
  97. <Image
  98. resizeMode="contain"
  99. style={styles.vehicleViewBg}
  100. source={require('../../images/user/bg-vehicles.png')}/>
  101. <View style={styles.vehicleRow}>
  102. <Image
  103. style={styles.vehicleIcon}
  104. source={require('../../images/user/ic-vehicle-model.png')}/>
  105. <Text style={styles.vehicleName}>{item.brand} {item.model}</Text>
  106. </View>
  107. <Text style={styles.vehicleModle}>{item.licensePlate}</Text>
  108. <View style={styles.vehicleRow}>
  109. <View style={styles.vehicleTypeRow}>
  110. <View style={styles.vehicleTypeIcon}>
  111. <VehicleType size={10}/>
  112. </View>
  113. <Text style={styles.vehicleType}>TYPE {item.connectorType}</Text>
  114. </View>
  115. {/* <Image
  116. style={styles.vehicleIcon}
  117. source={type?.icon}/>
  118. <Text style={styles.vehicleName}>{type?.name}</Text> */}
  119. </View>
  120. <Pressable
  121. style={styles.closeIcon}
  122. android_ripple={rippleLess}
  123. onPress={() => this.removeVehicle(item.vehiclePk)}>
  124. <MaterialCommunityIcons
  125. name="close"
  126. size={20}
  127. color={textCancel}
  128. />
  129. </Pressable>
  130. </Pressable>
  131. );
  132. })
  133. : <Text style={ui.noData}>{$t('profile.noVehicles')}</Text>
  134. );
  135. }
  136. }
  137. const styles = StyleSheet.create({
  138. vehicleView: {
  139. borderRadius: 8,
  140. overflow: 'hidden',
  141. ...ElevationObject(5),
  142. ...$margin(16, 16, 0),
  143. ...$padding(12, 16, 20),
  144. backgroundColor: colorLight
  145. },
  146. vehicleViewBg: {
  147. right: 8,
  148. bottom: 8,
  149. width: 111,
  150. height: 54,
  151. position: 'absolute'
  152. },
  153. vehicleRow: {
  154. alignItems: 'center',
  155. flexDirection: 'row',
  156. },
  157. vehicleIcon: {
  158. width: 32,
  159. height: 32,
  160. },
  161. vehicleName: {
  162. color: textDark,
  163. fontSize: 20,
  164. paddingLeft: 12,
  165. fontWeight: 'bold'
  166. },
  167. vehicleModle: {
  168. color: textPrimary,
  169. fontSize: 16,
  170. paddingLeft: 44,
  171. paddingTop: 2,
  172. paddingBottom: 7
  173. },
  174. vehicleTypeRow: {
  175. height: 20,
  176. marginLeft: 44,
  177. borderRadius: 4,
  178. overflow: 'hidden',
  179. alignItems: 'center',
  180. flexDirection: 'row',
  181. backgroundColor: '#E5EFDE'
  182. },
  183. vehicleTypeIcon: {
  184. width: 20,
  185. height: 20,
  186. alignItems: 'center',
  187. justifyContent: 'center',
  188. backgroundColor: colorAccent
  189. },
  190. vehicleType: {
  191. color: colorAccent,
  192. fontSize: 13,
  193. paddingLeft: 6,
  194. paddingRight: 8,
  195. },
  196. closeIcon: {
  197. top: 0,
  198. right: 0,
  199. padding: 12,
  200. position: 'absolute'
  201. }
  202. });