EditVehicle.js 5.8 KB

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