diff --git a/src/components/Contexts/VehicleContext.jsx b/src/components/Contexts/VehicleContext.jsx index c71860f..3744469 100644 --- a/src/components/Contexts/VehicleContext.jsx +++ b/src/components/Contexts/VehicleContext.jsx @@ -331,6 +331,27 @@ export const VehicleProvider = ({ children }) => { } }; + const addFlashpackVersion = async (model, year, flashpack, carFlashpackVersions, token) => { + try { + setBusy(true); + + const data = { + "car_model": model, + "car_year": year, + "flashpack": flashpack, + "car_flashpack_versions": carFlashpackVersions, + } + + const result = await api.addFlashpackVersion(data, token) + if (result.error) + throw new Error(`Add flashpack version error. ${result.message}`); + + return result; + } finally { + setBusy(false) + } + } + const deleteFlashpackVersion = async (model, year, flashpack, token) => { let result; try { @@ -388,6 +409,7 @@ export const VehicleProvider = ({ children }) => { totalFlashpackECUMappings, flashpackECUMappings, getFlashpackECUMappings, + addFlashpackVersion, deleteFlashpackVersion, }} > diff --git a/src/components/Flashpack/Add/index.jsx b/src/components/Flashpack/Add/index.jsx index e69de29..69d527a 100644 --- a/src/components/Flashpack/Add/index.jsx +++ b/src/components/Flashpack/Add/index.jsx @@ -0,0 +1,215 @@ +import { + Button, + TextField +} from "@material-ui/core"; +import React, { useEffect, useState } from "react"; +import { Redirect } from "react-router"; +import { logger } from "../../../services/monitoring"; +import { useStatusContext } from "../../Contexts/StatusContext"; +import { useVehicleContext, VehicleProvider } from "../../Contexts/VehicleContext"; +import { useUserContext } from "../../Contexts/UserContext"; +import useStyles from "../../useStyles"; + +const MainForm = () => { + const { + token: { + idToken: { jwtToken: token }, + }, + } = useUserContext(); + const classes = useStyles(); + const [redirect, setRedirect] = useState(null); + const { setMessage, setTitle, setSitePath } = useStatusContext(); + const [carModel, setCarModel] = useState(""); + const [carYear, setCarYear] = useState(0); + const [flashpack, setFlashpack] = useState(""); + const [mappingInputs, setMappingInputs] = useState([{ ecuName: "", ecuVersion: "" }]); + const { + addFlashpackVersion, + busy, + } = useVehicleContext(); + + useEffect(() => { + setTitle(`Add Flashpack Version`); + setSitePath([ + { + label: `Add Flashpack Version`, + link: `/tools/flashpack/add`, + }, + ]); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []); + + const onCarModelChange = (event) => { + setCarModel(event.target.value); + } + + const onCarYearChange = (event) => { + setCarYear(event.target.value); + } + + const onFlashpackChange = (event) => { + setFlashpack(event.target.value); + } + + const onSubmit = async (event) => { + try { + event.preventDefault(); + + const carFlashpackVersions = [] + for (let i = 0; i < mappingInputs.length; i++) { + carFlashpackVersions.push({ + "car_model": carModel, + "car_year": parseInt(carYear), + "flashpack": flashpack, + "car_ecu_name": mappingInputs[i].ecuName, + "car_ecu_version": mappingInputs[i].ecuVersion, + }) + } + + const result = await addFlashpackVersion(carModel, parseInt(carYear), flashpack, carFlashpackVersions, token); + if (!result || result.error) return; + + setMessage(`Added ${carYear} ${carModel} ${flashpack}`); + setRedirect(`/tools/flashpacks`); + } catch (e) { + setMessage(e.message); + logger.warn(e.stack); + } + }; + + if (redirect && redirect.length > 0) { + return ; + } + + // code for handling the dynamic input fields adapted from + // https://blog.stackademic.com/how-to-dynamically-add-input-fields-on-button-click-in-reactjs-ddf8d8fe495b + + const handleAddMappingInput = () => { + setMappingInputs([...mappingInputs, { ecuName: "", ecuVersion: "", }]); + }; + + const handleChange = (event, index) => { + let { name, value } = event.target; + let onChangeValue = [...mappingInputs]; + onChangeValue[index][name] = value; + setMappingInputs(onChangeValue); + }; + + const handleDeleteMappingInput = (index) => { + const newArray = [...mappingInputs]; + newArray.splice(index, 1); + setMappingInputs(newArray); + }; + + return ( +
+
+
+ + + +
+ {mappingInputs.map((item, index) => ( +
+ handleChange(event, index)} + type="text" + /> + handleChange(event, index)} + type="text" + /> + {mappingInputs.length > 1 && ( + + )} + {index === mappingInputs.length - 1 && ( + + )} +
+ ))} +
{JSON.stringify(mappingInputs)}
+
+
+ +
+
+ ); +}; + +const FlashpackAdd = () => ( + + + +); + +export default FlashpackAdd; \ No newline at end of file diff --git a/src/services/vehiclesAPI.js b/src/services/vehiclesAPI.js index 6280d14..1ad68ec 100644 --- a/src/services/vehiclesAPI.js +++ b/src/services/vehiclesAPI.js @@ -283,8 +283,8 @@ const vehiclesAPI = { .catch(errorHandler) }, - addFlashpackECUMapping: async (data, token) => { - return fetch(`${API_ENDPOINT}/flashpack_ecu_mapping`, { + addFlashpackVersion: async (data, token) => { + return fetch(`${API_ENDPOINT}/flashpack_version`, { method: "POST", headers: Object.assign( { "Content-Type": "application/json" },