VehicleDetail.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /**
  2. * 添加/编辑 Vehicle页面
  3. * @邠心vbe on 2024/01/30
  4. */
  5. import React, { Component } from 'react';
  6. import { View, Text, StyleSheet, TextInput, Image, Pressable, ScrollView } from 'react-native';
  7. import Dialog from '../../components/Dialog';
  8. import apiUser from '../../api/apiUser';
  9. import TextView from '../../components/TextView';
  10. import Dropdown from '../../components/Dropdown';
  11. import { TypeImageList } from '../chargeV2/Charging';
  12. import ImageCropPicker from 'react-native-image-crop-picker';
  13. import { UploadThemes } from '../../components/ThemesConfig';
  14. import apiUpload from '../../api/apiUpload';
  15. import utils from '../../utils/utils';
  16. const options = {
  17. width: 300,
  18. height: 300,
  19. cropping: true,
  20. multiple: false,
  21. mediaType: 'photo',
  22. writeTempFile: false,
  23. compressImageQuality: 0.8,
  24. compressImageMaxWidth: 720,
  25. compressImageMaxHeight: 1280,
  26. ...UploadThemes
  27. }
  28. export default class VehicleDetail extends Component {
  29. constructor(props) {
  30. super(props);
  31. this.state = {
  32. isEdit: false,
  33. vehicleInfo: {
  34. brand: "",
  35. model: "",
  36. connectorType: ""
  37. },
  38. brandOptions: [],
  39. modelOptions: [],
  40. typeOptions: []
  41. };
  42. }
  43. componentDidMount() {
  44. this.getBrandOptions();
  45. const params = this.props.route.params;
  46. if (params.id) {
  47. this.setState({
  48. isEdit: true
  49. })
  50. Dialog.showProgressDialog();
  51. this.getVehicleData(params.id);
  52. }
  53. }
  54. getBrandOptions() {
  55. apiUser.getVehicleBrandList().then(res => {
  56. if (res.data) {
  57. this.setState({
  58. brandOptions: res.data
  59. })
  60. }
  61. }).catch((err) => {
  62. Dialog.dismissLoading();
  63. toastShort(err)
  64. });
  65. const types = []
  66. TypeImageList.forEach((item, index) => {
  67. types.push(item.name);
  68. })
  69. this.setState({
  70. typeOptions: types
  71. })
  72. }
  73. getModelOptions() {
  74. if (this.state.vehicleInfo.brand) {
  75. apiUser.getVehicleModelByBrand({
  76. brand: this.state.vehicleInfo.brand
  77. }).then(res => {
  78. if (res.data) {
  79. this.setState({
  80. modelOptions: res.data
  81. })
  82. }
  83. }).catch((err) => {
  84. toastShort(err)
  85. this.setState({
  86. modelOptions: []
  87. })
  88. });
  89. } else {
  90. this.setState({
  91. modelOptions: []
  92. })
  93. }
  94. }
  95. getVehicleData(id) {
  96. apiUser.getVehicleById({
  97. vehiclePk: id
  98. }).then(res => {
  99. Dialog.dismissLoading();
  100. if (res.data) {
  101. const info = res.data;
  102. this.setState({
  103. vehicleInfo: info
  104. }, () => {
  105. this.getModelOptions();
  106. });
  107. }
  108. }).catch((err) => {
  109. Dialog.dismissLoading();
  110. toastShort(err)
  111. });
  112. }
  113. changeForm(key, value) {
  114. const form = {
  115. ...this.state.vehicleInfo
  116. }
  117. form[key] = value;
  118. this.setState({
  119. vehicleInfo: form
  120. })
  121. }
  122. uploadImage() {
  123. ImageCropPicker.openPicker({
  124. ...options,
  125. cropperToolbarTitle: $t('common.cropperTitle')
  126. }).then(image => {
  127. if (image.path) {
  128. apiUpload.uploadImage(image.path, image.mime, 'PDVL').then(res => {
  129. if (res.success && res.data.picturePath) {
  130. this.changeForm("vehiclePhoto", res.data.picturePath)
  131. toastShort($t('common.uploadSuccess'));
  132. } else {
  133. toastShort($t('common.uploadFailed'));
  134. }
  135. }).catch(err => {
  136. toastShort(err);
  137. });
  138. }
  139. }).catch(err1 => {
  140. //console.log(err1);
  141. });
  142. }
  143. validate() {
  144. if (!this.state.vehicleInfo.brand) {
  145. toastShort($t('profile.msgSelectBrand'));
  146. return;
  147. }
  148. if (!this.state.vehicleInfo.vehicleModelId) {
  149. toastShort($t('profile.msgSelectModel'));
  150. return;
  151. }
  152. if (!this.state.vehicleInfo.licensePlate) {
  153. toastShort($t('profile.msgInputPlate'));
  154. return;
  155. }
  156. /*if (!this.state.vehicleInfo.batteryCapacity) {
  157. toastShort($t('profile.msgInputBatCap'));
  158. return;
  159. }
  160. if (!this.state.vehicleInfo.connectorType) {
  161. toastShort($t('profile.msgSelectType'));
  162. return;
  163. }
  164. if (!this.state.vehicleInfo.vehiclePhoto) {
  165. toastShort($t('profile.msgVehiclePhoto'));
  166. return;
  167. }*/
  168. if (this.state.isEdit) {
  169. this.updateVehicle(this.state.vehicleInfo)
  170. } else {
  171. this.addVehicle(this.state.vehicleInfo)
  172. }
  173. }
  174. addVehicle(params) {
  175. Dialog.showProgressDialog();
  176. apiUser.addVehicle(params).then(res => {
  177. Dialog.dismissLoading();
  178. toastShort($t('common.addSuccess'));
  179. goBack();
  180. }).catch((err) => {
  181. Dialog.dismissLoading();
  182. toastShort(err);
  183. });
  184. }
  185. updateVehicle(params) {
  186. Dialog.showProgressDialog();
  187. apiUser.updateVehicle(params).then(res => {
  188. Dialog.dismissLoading();
  189. toastShort($t('common.updateSuccess'));
  190. goBack();
  191. }).catch((err) => {
  192. Dialog.dismissLoading();
  193. toastShort(err);
  194. });
  195. }
  196. render() {
  197. return (
  198. <ScrollView
  199. style={styles.container}
  200. contentContainerStyle={$padding(16)}>
  201. <View style={ui.flexc}>
  202. <MaterialCommunityIcons
  203. name="form-dropdown"
  204. size={20}
  205. color={textPrimary}
  206. style={ui.bold}
  207. />
  208. <TextView style={styles.titleText}>{$t('profile.vehicleBrand')}</TextView>
  209. </View>
  210. <Dropdown
  211. style={styles.formDropdown}
  212. list={this.state.brandOptions}
  213. placeholder={$t('profile.selectBrand')}
  214. title={$t('profile.vehicleBrand')}
  215. value={this.state.vehicleInfo.brand}
  216. autoSelect={false}
  217. onChange={value => {
  218. this.changeForm("brand", value);
  219. this.getModelOptions();
  220. }}
  221. />
  222. <Dropdown
  223. style={styles.formDropdown}
  224. list={this.state.modelOptions}
  225. placeholder={$t('profile.selectModel')}
  226. title={$t('profile.vehicleModel')}
  227. value={this.state.vehicleInfo.vehicleModelId}
  228. valueKey={"vehicleModelId"}
  229. nameKey={"model"}
  230. autoSelect={true}
  231. onChange={value => this.changeForm("vehicleModelId", value)}
  232. />
  233. <TextInput
  234. style={styles.formInput}
  235. defaultValue={this.state.vehicleInfo.licensePlate}
  236. placeholder={$t('profile.enterLicensePlate')}
  237. placeholderTextColor={textPlacehoder}
  238. maxLength={20}
  239. onChangeText={text => this.changeForm("licensePlate", text)}
  240. />
  241. <View style={styles.formInputRow}>
  242. <TextInput
  243. style={styles.itemInput}
  244. defaultValue={this.state.vehicleInfo.batteryCapacity}
  245. placeholder={$t('profile.enterBatteryCapacity')}
  246. placeholderTextColor={textPlacehoder}
  247. maxLength={20}
  248. keyboardType='decimal-pad'
  249. onChangeText={text => this.changeForm("batteryCapacity", text)}
  250. />
  251. <TextView style={styles.titleText}>kWh</TextView>
  252. </View>
  253. <View style={ui.flexc}>
  254. <MaterialCommunityIcons
  255. name="form-dropdown"
  256. size={20}
  257. color={textPrimary}
  258. style={ui.bold}
  259. />
  260. <TextView style={styles.titleText}>{$t('profile.connecterType')}</TextView>
  261. </View>
  262. <Dropdown
  263. style={styles.formDropdown}
  264. list={this.state.typeOptions}
  265. placeholder={$t('profile.selectConnecterType')}
  266. title={$t('profile.connecterType')}
  267. value={this.state.vehicleInfo.connectorType}
  268. onChange={value => this.changeForm("connectorType", value)}
  269. autoSelect={false}
  270. />
  271. <View style={ui.flexc}>
  272. <MaterialCommunityIcons
  273. name="tray-arrow-up"
  274. size={20}
  275. color={textPrimary}
  276. style={ui.bold}
  277. />
  278. <TextView style={styles.titleText}>{$t('profile.plsUploadImageCar')}</TextView>
  279. </View>
  280. <View style={styles.uploadGroup}>
  281. { this.state.vehicleInfo.vehiclePhoto
  282. ? <Pressable
  283. onPress={() => this.uploadImage()}>
  284. <Image
  285. style={styles.uploadImage}
  286. source={{uri: utils.getImageUrl(this.state.vehicleInfo.vehiclePhoto)}}
  287. />
  288. </Pressable>
  289. : <Pressable
  290. style={styles.uploadView}
  291. onPress={() => this.uploadImage()}>
  292. <MaterialCommunityIcons
  293. name="tray-arrow-up"
  294. size={32}
  295. color={textCancel}
  296. style={ui.bold}
  297. />
  298. <TextView style={styles.uploadTitle}>{$t('profile.selectUpload')}</TextView>
  299. <TextView style={styles.uploadDesc}>{$t('profile.max4mbPhoto')}</TextView>
  300. </Pressable>
  301. }
  302. </View>
  303. <View style={styles.buttonView}>
  304. <Button
  305. text={$t('common.save')}
  306. elevation={1.5}
  307. onClick={() => {
  308. this.validate();
  309. }}/>
  310. </View>
  311. </ScrollView>
  312. );
  313. }
  314. }
  315. const styles = StyleSheet.create({
  316. container: {
  317. flex: 1
  318. },
  319. titleText: {
  320. padding: 8,
  321. fontSize: 14,
  322. color: textPrimary,
  323. fontWeight: 'bold'
  324. },
  325. formInput: {
  326. ...$margin(8, 0),
  327. padding: 12,
  328. borderWidth: 1,
  329. borderRadius: 4,
  330. borderStyle: 'solid',
  331. borderColor: "#EAEAEA",
  332. },
  333. formDropdown: {
  334. ...$margin(8, 0),
  335. padding: 12,
  336. borderWidth: 1,
  337. borderRadius: 4,
  338. borderStyle: 'solid',
  339. borderColor: "#EAEAEA",
  340. alignItems: 'center',
  341. flexDirection: 'row'
  342. },
  343. formInputRow: {
  344. ...$margin(8, 0),
  345. padding: 8,
  346. borderWidth: 1,
  347. borderRadius: 4,
  348. borderStyle: 'solid',
  349. borderColor: "#EAEAEA",
  350. alignItems: 'center',
  351. flexDirection: 'row'
  352. },
  353. itemInput: {
  354. flex: 1,
  355. padding: 4
  356. },
  357. uploadView: {
  358. width: 160,
  359. height: 160,
  360. ...$margin(8, 0),
  361. padding: 8,
  362. borderWidth: 1,
  363. borderRadius: 4,
  364. borderStyle: 'solid',
  365. borderColor: "#EAEAEA",
  366. alignItems: 'center',
  367. justifyContent: 'center'
  368. },
  369. uploadImage: {
  370. width: 160,
  371. height: 160,
  372. ...$margin(8, 0)
  373. },
  374. uploadTitle: {
  375. fontSize: 14,
  376. color: textPrimary,
  377. paddingTop: 8
  378. },
  379. uploadDesc: {
  380. fontSize: 12,
  381. color: textCancel,
  382. paddingTop: 2
  383. },
  384. buttonView: {
  385. marginTop: 32,
  386. marginBottom: 8
  387. }
  388. })