VehicleList.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /**
  2. * 车辆列表
  3. * @邠心vbe on 2021/06/08
  4. */
  5. import React, { Component } from 'react';
  6. import { View, Text, ImageBackground, 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 { getConnectTypeByKey } from '../charge/Charging';
  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: 'Remove Vehicle',
  57. message: 'Are you sure you want to remove this vehicle?',
  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('Delete successfully');
  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. 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. style={$padding(16, 16, 0)}>
  98. <ImageBackground
  99. style={styles.vehicleView}
  100. resizeMode="contain"
  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. <Text style={styles.vehicleName}>{item.brand} {item.model}</Text>
  107. </View>
  108. <Text style={styles.vehicleModle}>{item.licensePlate}</Text>
  109. <View style={styles.vehicleRow}>
  110. <View style={styles.vehicleTypeRow}>
  111. <View style={styles.vehicleTypeIcon}>
  112. <VehicleType size={10}/>
  113. </View>
  114. <Text style={styles.vehicleType}>TYPE {item.connectorType}</Text>
  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. </ImageBackground>
  132. </Pressable>
  133. );
  134. })
  135. : <Text style={ui.noData}>No Vehicles</Text>
  136. );
  137. }
  138. }
  139. const styles = StyleSheet.create({
  140. vehicleView: {
  141. ...$padding(12, 16, 20),
  142. borderRadius: 8,
  143. overflow: 'hidden',
  144. ...ElevationObject(5)
  145. },
  146. vehicleRow: {
  147. alignItems: 'center',
  148. flexDirection: 'row',
  149. },
  150. vehicleIcon: {
  151. width: 32,
  152. height: 32,
  153. },
  154. vehicleName: {
  155. color: textDark,
  156. fontSize: 20,
  157. paddingLeft: 12,
  158. fontWeight: 'bold'
  159. },
  160. vehicleModle: {
  161. color: textPrimary,
  162. fontSize: 16,
  163. paddingLeft: 44,
  164. paddingTop: 2,
  165. paddingBottom: 7
  166. },
  167. vehicleTypeRow: {
  168. height: 20,
  169. marginLeft: 44,
  170. borderRadius: 4,
  171. overflow: 'hidden',
  172. alignItems: 'center',
  173. flexDirection: 'row',
  174. backgroundColor: '#E5EFDE'
  175. },
  176. vehicleTypeIcon: {
  177. width: 20,
  178. height: 20,
  179. alignItems: 'center',
  180. justifyContent: 'center',
  181. backgroundColor: colorAccent
  182. },
  183. vehicleType: {
  184. color: colorAccent,
  185. fontSize: 13,
  186. paddingLeft: 6,
  187. paddingRight: 8,
  188. },
  189. closeIcon: {
  190. top: 0,
  191. right: 0,
  192. padding: 12,
  193. position: 'absolute'
  194. }
  195. });