working, just need to prettify
This commit is contained in:
@@ -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) => {
|
const deleteFlashpackVersion = async (model, year, flashpack, token) => {
|
||||||
let result;
|
let result;
|
||||||
try {
|
try {
|
||||||
@@ -388,6 +409,7 @@ export const VehicleProvider = ({ children }) => {
|
|||||||
totalFlashpackECUMappings,
|
totalFlashpackECUMappings,
|
||||||
flashpackECUMappings,
|
flashpackECUMappings,
|
||||||
getFlashpackECUMappings,
|
getFlashpackECUMappings,
|
||||||
|
addFlashpackVersion,
|
||||||
deleteFlashpackVersion,
|
deleteFlashpackVersion,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -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 <Redirect to={redirect} />;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 (
|
||||||
|
<div className={classes.paper}>
|
||||||
|
<form className={classes.form} noValidate action="{onSubmit}">
|
||||||
|
<div>
|
||||||
|
<TextField
|
||||||
|
id="carModel"
|
||||||
|
name="carModel"
|
||||||
|
label="Model"
|
||||||
|
variant="outlined"
|
||||||
|
margin="normal"
|
||||||
|
inputProps={{
|
||||||
|
maxLength: "255",
|
||||||
|
}}
|
||||||
|
required
|
||||||
|
fullWidth
|
||||||
|
value={carModel}
|
||||||
|
onChange={onCarModelChange}
|
||||||
|
type="text"
|
||||||
|
/>
|
||||||
|
<TextField
|
||||||
|
id="carYear"
|
||||||
|
name="carYear"
|
||||||
|
label="Year"
|
||||||
|
variant="outlined"
|
||||||
|
margin="normal"
|
||||||
|
inputProps={{
|
||||||
|
maxLength: "255",
|
||||||
|
}}
|
||||||
|
required
|
||||||
|
fullWidth
|
||||||
|
value={carYear}
|
||||||
|
onChange={onCarYearChange}
|
||||||
|
type="number"
|
||||||
|
/>
|
||||||
|
<TextField
|
||||||
|
id="flashpack"
|
||||||
|
name="flashpack"
|
||||||
|
label="Flashpack Number"
|
||||||
|
variant="outlined"
|
||||||
|
margin="normal"
|
||||||
|
inputProps={{
|
||||||
|
maxLength: "255",
|
||||||
|
}}
|
||||||
|
required
|
||||||
|
fullWidth
|
||||||
|
value={flashpack}
|
||||||
|
onChange={onFlashpackChange}
|
||||||
|
type="text"
|
||||||
|
/>
|
||||||
|
<div className="container">
|
||||||
|
{mappingInputs.map((item, index) => (
|
||||||
|
<div className="input_container" key={index}>
|
||||||
|
<TextField
|
||||||
|
id="ecuName"
|
||||||
|
name="ecuName"
|
||||||
|
label="ECU Name"
|
||||||
|
variant="outlined"
|
||||||
|
margin="normal"
|
||||||
|
inputProps={{
|
||||||
|
maxLength: "255",
|
||||||
|
}}
|
||||||
|
value={item.ecuName}
|
||||||
|
onChange={(event) => handleChange(event, index)}
|
||||||
|
type="text"
|
||||||
|
/>
|
||||||
|
<TextField
|
||||||
|
id="ecuVersion"
|
||||||
|
name="ecuVersion"
|
||||||
|
label="ECU Version"
|
||||||
|
variant="outlined"
|
||||||
|
margin="normal"
|
||||||
|
inputProps={{
|
||||||
|
maxLength: "255",
|
||||||
|
}}
|
||||||
|
value={item.ecuVersion}
|
||||||
|
onChange={(event) => handleChange(event, index)}
|
||||||
|
type="text"
|
||||||
|
/>
|
||||||
|
{mappingInputs.length > 1 && (
|
||||||
|
<button onClick={() => handleDeleteMappingInput(index)}>Delete</button>
|
||||||
|
)}
|
||||||
|
{index === mappingInputs.length - 1 && (
|
||||||
|
<button onClick={() => handleAddMappingInput()}>Add</button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
<div className="body"> {JSON.stringify(mappingInputs)} </div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<Button
|
||||||
|
type="submit"
|
||||||
|
disabled={busy}
|
||||||
|
fullWidth
|
||||||
|
variant="contained"
|
||||||
|
color="primary"
|
||||||
|
className={classes.submit}
|
||||||
|
onClick={onSubmit}
|
||||||
|
>
|
||||||
|
{busy ? "Submitting..." : "Submit"}
|
||||||
|
</Button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const FlashpackAdd = () => (
|
||||||
|
<VehicleProvider>
|
||||||
|
<MainForm />
|
||||||
|
</VehicleProvider>
|
||||||
|
);
|
||||||
|
|
||||||
|
export default FlashpackAdd;
|
||||||
@@ -283,8 +283,8 @@ const vehiclesAPI = {
|
|||||||
.catch(errorHandler)
|
.catch(errorHandler)
|
||||||
},
|
},
|
||||||
|
|
||||||
addFlashpackECUMapping: async (data, token) => {
|
addFlashpackVersion: async (data, token) => {
|
||||||
return fetch(`${API_ENDPOINT}/flashpack_ecu_mapping`, {
|
return fetch(`${API_ENDPOINT}/flashpack_version`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: Object.assign(
|
headers: Object.assign(
|
||||||
{ "Content-Type": "application/json" },
|
{ "Content-Type": "application/json" },
|
||||||
|
|||||||
Reference in New Issue
Block a user