EditVehicle.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /**
  2. * Edit Vehicle页面
  3. * @邠心vbe on 2021/06/08
  4. */
  5. import React, { Component } from 'react';
  6. import { View, Text, Pressable, Image, TextInput } from 'react-native';
  7. import apiUser from '../../api/apiUser';
  8. import Dialog from '../../components/Dialog';
  9. import { TypeImageList } from '../charge/Charging';
  10. import { VehicleStyles } from './AddVehicle';
  11. export default class EditVehicle extends Component {
  12. constructor(props) {
  13. super(props);
  14. this.state = {
  15. vehicleInfo: {},
  16. connectorIndex: 0,
  17. connectorType: TypeImageList
  18. };
  19. this.form = {}
  20. }
  21. componentDidMount() {
  22. const params = this.props.route.params;
  23. if (params.id) {
  24. Dialog.showProgressDialog();
  25. this.getVehicleData(params.id);
  26. }
  27. }
  28. getVehicleData(id) {
  29. apiUser.getVehicleById({
  30. vehiclePk: id
  31. }).then(res => {
  32. Dialog.dismissLoading();
  33. if (res.data) {
  34. const info = res.data;
  35. var typeIndex = 0
  36. TypeImageList.forEach((item, index) => {
  37. if (item.key == info.connectorType) {
  38. typeIndex = index;
  39. }
  40. })
  41. this.setState({
  42. vehicleInfo: info,
  43. connectorIndex: typeIndex
  44. });
  45. this.form = info;
  46. }
  47. }).catch(err => {
  48. Dialog.dismissLoading();
  49. toastShort(err)
  50. });
  51. }
  52. selectConnector(index) {
  53. this.setState({
  54. connectorIndex: index
  55. });
  56. }
  57. validate() {
  58. if (!this.form.brand) {
  59. toastShort('Please type brand');
  60. return;
  61. }
  62. if (!this.form.model) {
  63. toastShort('Please type model');
  64. return;
  65. }
  66. if (!this.form.licensePlate) {
  67. toastShort('Please type license plate');
  68. return;
  69. }
  70. const params = this.form;
  71. params.connectorType = this.state.connectorType[this.state.connectorIndex].key
  72. this.updateVehicle(params)
  73. }
  74. updateVehicle(params) {
  75. Dialog.showProgressDialog();
  76. apiUser.updateVehicle(params).then(res => {
  77. Dialog.dismissLoading();
  78. toastShort('Update successfully');
  79. goBack();
  80. }).catch(err => {
  81. Dialog.dismissLoading();
  82. toastShort(err);
  83. });
  84. }
  85. deleteVehicle() {
  86. Dialog.showProgressDialog();
  87. apiUser.deleteVehicle({
  88. vehiclePk: this.form.vehiclePk
  89. }).then(res => {
  90. Dialog.dismissLoading();
  91. toastShort('Delete successfully');
  92. goBack();
  93. }).catch(err => {
  94. Dialog.dismissLoading();
  95. toastShort(err);
  96. });
  97. }
  98. removeVehicle() {
  99. Dialog.showDialog({
  100. title: 'Remove Vehicle',
  101. message: 'Are you sure you want to remove this vehicle?',
  102. callback: btn => {
  103. if (btn == 'ok') {
  104. Dialog.dismissLoading();
  105. this.deleteVehicle();
  106. }
  107. }
  108. })
  109. }
  110. render() {
  111. return (
  112. this.state.vehicleInfo.vehiclePk
  113. ? <View style={styles.container}>
  114. <View style={styles.formView}>
  115. <Text style={styles.label}>Brand</Text>
  116. <TextInput
  117. style={styles.formInput}
  118. defaultValue={this.state.vehicleInfo.brand}
  119. placeholder='Add text'
  120. maxLength={25}
  121. onChangeText={text => this.form.brand = text}
  122. />
  123. </View>
  124. <View style={styles.formView}>
  125. <Text style={styles.label}>Model</Text>
  126. <TextInput
  127. style={styles.formInput}
  128. defaultValue={this.state.vehicleInfo.model}
  129. placeholder='Add text'
  130. maxLength={50}
  131. onChangeText={text => this.form.model = text}
  132. />
  133. </View>
  134. <View style={styles.formView}>
  135. <Text style={styles.label}>License Plate</Text>
  136. <TextInput
  137. style={styles.formInput}
  138. defaultValue={this.state.vehicleInfo.licensePlate}
  139. placeholder='Add text'
  140. maxLength={20}
  141. onChangeText={text => this.form.licensePlate = text}
  142. />
  143. </View>
  144. <View style={styles.formView}>
  145. <Text style={styles.label}>Choose Connecter Type</Text>
  146. <View style={styles.connectorView}>
  147. { this.state.connectorType.map((item, index) => {
  148. return (
  149. <Pressable
  150. key={index}
  151. style={[
  152. styles.ctypeView,
  153. index==this.state.connectorIndex && styles.actived
  154. ]}
  155. onPress={() => {
  156. this.selectConnector(index);
  157. }}>
  158. <Image
  159. style={styles.typeIcon}
  160. source={item.icon}/>
  161. <Text style={styles.typeName}>{item.name}</Text>
  162. { index==this.state.connectorIndex &&
  163. <Image
  164. style={styles.checkedIcon}
  165. source={require('../../images/charge/charge-item-select.png')}/>
  166. }
  167. </Pressable>
  168. )
  169. })
  170. }
  171. </View>
  172. </View>
  173. <Text style={ui.flex1}></Text>
  174. <View style={styles.buttonView}>
  175. <Button
  176. style={styles.removeButton}
  177. text='Remove'
  178. elevation={1.5}
  179. onClick={() => {
  180. this.removeVehicle();
  181. }}/>
  182. <Button
  183. text='Update'
  184. elevation={1.5}
  185. onClick={() => {
  186. this.validate();
  187. }}/>
  188. </View>
  189. </View>
  190. : <View></View>
  191. );
  192. }
  193. }
  194. const styles = VehicleStyles