VehicleList.js 5.2 KB

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