EditVehicle.js 5.7 KB

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