VehicleList.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 { getConnectTypeByKey } from '../charge/Charging';
  10. import { PageList } from '../Router';
  11. export default class VehicleList extends Component {
  12. constructor(props) {
  13. super(props);
  14. this.state = {
  15. vehicleList: [],
  16. refreshId: 0,
  17. };
  18. }
  19. componentDidMount() {
  20. this.getVehicleList();
  21. }
  22. componentDidUpdate() {
  23. if (this.state.refreshId != this.props.refreshId) {
  24. this.setState({
  25. refreshId: this.props.refreshId,
  26. });
  27. this.getVehicleList();
  28. }
  29. }
  30. getVehicleList() {
  31. apiUser.getVehicles().then(res => {
  32. if (res.data) {
  33. this.setState({
  34. vehicleList: res.data
  35. });
  36. if (this.props.onResult) {
  37. this.props.onResult(res.data.length)
  38. }
  39. }
  40. }).catch(err => {
  41. this.setState({
  42. vehicleList: []
  43. });
  44. if (this.props.onResult) {
  45. this.props.onResult(0)
  46. }
  47. });
  48. }
  49. render() {
  50. return (
  51. this.state.vehicleList.length > 0
  52. ? this.state.vehicleList.map((item, index) => {
  53. const type = getConnectTypeByKey(item.connectorType)
  54. return (
  55. <Pressable
  56. key={index}
  57. onPress={() => {
  58. startPage(PageList.editVehicle, {id: item.vehiclePk});
  59. }}
  60. onLongPress={() => {
  61. if (this.props.onDelete) {
  62. this.props.onDelete(item.vehiclePk)
  63. }
  64. }}>
  65. <ImageBackground
  66. style={styles.vehicleView}
  67. source={require('../../images/user/bg-vehicles.png')}>
  68. <View style={styles.vehicleRow}>
  69. <Image
  70. style={styles.vehicleIcon}
  71. source={require('../../images/user/ic-vehicle-model.png')}/>
  72. <Text style={styles.vehicleName}>{item.brand} {item.model}</Text>
  73. </View>
  74. <Text style={styles.vehicleModle}>{item.licensePlate}</Text>
  75. <View style={styles.vehicleRow}>
  76. <Image
  77. style={styles.vehicleIcon}
  78. source={type?.icon}/>
  79. <Text style={styles.vehicleName}>{type?.name}</Text>
  80. </View>
  81. </ImageBackground>
  82. </Pressable>
  83. );
  84. })
  85. : <Text style={ui.noData}>No Vehicles</Text>
  86. );
  87. }
  88. }
  89. const styles = StyleSheet.create({
  90. vehicleView: {
  91. padding: 20,
  92. borderRadius: 10,
  93. overflow: 'hidden',
  94. marginBottom: 16,
  95. ...ElevationObject(1.5)
  96. },
  97. vehicleRow: {
  98. alignItems: 'center',
  99. flexDirection: 'row',
  100. },
  101. vehicleIcon: {
  102. width: 20,
  103. height: 20,
  104. },
  105. vehicleName: {
  106. color: '#000',
  107. fontSize: 14,
  108. paddingLeft: 12
  109. },
  110. vehicleModle: {
  111. color: '#333',
  112. fontSize: 12,
  113. paddingLeft: 32,
  114. paddingTop: 2,
  115. paddingBottom: 7
  116. },
  117. });