Skip to content

Commit

Permalink
#minor User can now manually upload maintenance information for a veh…
Browse files Browse the repository at this point in the history
…icle

Documentation updated
  • Loading branch information
Delaney H committed Apr 7, 2024
1 parent b7eeafe commit 7f707ba
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 62 deletions.
93 changes: 63 additions & 30 deletions backend/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,36 +193,69 @@ app.get('/api/get-vehicle-history', async (req, res) => {
res.send(getHistory);
});

/***
* This route is used to add maintenance history to the DATABASE
* @param configId - The config_id of the vehicle
* @param type - The type of maintenance
* @param date - The date of the maintenance
* @param maintenance - The maintenance performed
* @param cost - The cost of the maintenance
* @param email - The email of the user
*/
app.post('/api/add-maintenance-history', async (req, res) => {
const garage = DATABASE.collection("user_vehicle_info");
const configId = req.query.configId;
const maintenance = {
type: req.body.type,
date: req.body.date,
maintenance: req.body.maintenance,
cost: parseInt(req.body.cost)
};
const add = await garage.updateOne(
{ email: EMAIL, config_id: parseInt(configId) },
{ $push: { completed_maintenance: maintenance } }
);
return res.json(add.modifiedCount);
});

/***
* This route is used to update maintenance history in the DATABASE
* @param configId - The config_id of the vehicle
* @param old_type - The old type of maintenance
* @param old_date - The old date of the maintenance
* @param old_maintenance - The old maintenance performed
* @param old_cost - The old cost of the maintenance
* @param new_type - The new type of maintenance
* @param new_date - The new date of the maintenance
* @param new_maintenance - The new maintenance performed
* @param new_cost - The new cost of the maintenance
* @param email - The email of the user
*/
app.post('/api/update-maintenance-history', async (req, res) => {
try {
const garage = DATABASE.collection("user_vehicle_info");

const {
old_type, old_date, old_maintenance, old_cost,
new_type, new_date, new_maintenance, new_cost
} = req.body;
const update = await garage.updateOne(
{
email: EMAIL,
"completed_maintenance.type": old_type,
"completed_maintenance.date": old_date,
"completed_maintenance.maintenance": old_maintenance,
"completed_maintenance.cost": parseInt(old_cost)
},
{
$set: {
"completed_maintenance.$.type": new_type,
"completed_maintenance.$.date": new_date,
"completed_maintenance.$.maintenance": new_maintenance,
"completed_maintenance.$.cost": parseInt(new_cost)
}
const garage = DATABASE.collection("user_vehicle_info");
const configId = req.query.configId;
const {
old_type, old_date, old_maintenance, old_cost,
new_type, new_date, new_maintenance, new_cost
} = req.body;
const update = await garage.updateOne(
{
email: EMAIL,
config_id: configId,
"completed_maintenance.type": old_type,
"completed_maintenance.date": old_date,
"completed_maintenance.maintenance": old_maintenance,
"completed_maintenance.cost": parseInt(old_cost)
},
{
$set: {
"completed_maintenance.$.type": new_type,
"completed_maintenance.$.date": new_date,
"completed_maintenance.$.maintenance": new_maintenance,
"completed_maintenance.$.cost": parseInt(new_cost)
}
);
return res.json(update.modifiedCount);
} catch (error) {
return res.status(500).json({message: error.message});
}

});
return res.json(update.modifiedCount);
});

app.delete('/api/delete-maintenance-history', async (req, res) => {
Expand Down Expand Up @@ -432,8 +465,8 @@ app.post('/api/add-vehicle', async (req, res) => {
const config_id = req.body.config_id;
console.log("ADD: Config_id: "+config_id);

const database = client.db("vehicleDB");
const garage = database.collection("user_garage");
//const database = client.db("vehicleDB");
const garage = DATABASE.collection("user_garage");

try{
const exist = await garage.findOne({email});
Expand Down
110 changes: 100 additions & 10 deletions frontend/src/pages/maintenance-history/ManualVehicleHistory.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,108 @@
import Button from "react-bootstrap/Button";
import {Link} from "react-router-dom";
import {Link, useLocation} from "react-router-dom";
import { useNavigate} from "react-router-dom";
import Modal from "react-bootstrap/Modal";
import {Alert, Col, Form, InputGroup, Row} from "react-bootstrap";
import {useRef, useState} from "react";

function ManualVehicleHistory() {
let navigate = useNavigate();
function ManualVehicleHistory(props) {

const [refreshData, setRefreshData] = useState(false);
const [show, setShow] = useState(false);
const handleClose = () => setShow(false);
const handleShow = () => setShow(true);

const typeText = useRef();
const dateText = useRef();
const maintenanceText = useRef();
const costText = useRef();

const submit = (e) => {
e.preventDefault();
const myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
const raw = JSON.stringify({
"type": typeText.current.value,
"date": dateText.current.value,
"maintenance": maintenanceText.current.value,
"cost": parseInt(costText.current.value)
});
const reqOptions = {
method: 'POST',
headers: myHeaders,
body: raw,
redirect: 'follow'
};

fetch("/api/add-maintenance-history?configId="+props.configId, reqOptions)
.then((res) => res.json())
.then((result) => {
console.log(result);
setRefreshData(!refreshData);
})
.catch((error) => console.error(error));
handleClose();
}

return (
<div>
<h1>Manual Vehicle History</h1>
<p>Uh oh! This page isn't finished yet!</p>
<Button onClick={() => navigate(-1)}>
Go Back
</Button>
</div>
<>
<Button onClick={handleShow}>Manual</Button>
<Modal show={show} onHide={handleClose}>
<Modal.Dialog>
<Modal.Header closeButton>
<Modal.Title>Manual Maintenance History</Modal.Title>
</Modal.Header>
<Modal.Body>
<Form>
<Form.Group>
<Row>
<Col>
<InputGroup hasValidation>
<InputGroup.Text>Type</InputGroup.Text>
<Form.Select type="as" ref={typeText} required>
<option value="Maintenance">Maintenance</option>
<option value="Repair">Repair</option>
<option value="Inspection">Inspection</option>
<option value="Replacement">Replacement</option>
<option value="Other">Other</option>
</Form.Select>
</InputGroup>
</Col>
<Col>
<InputGroup hasValidation>
<InputGroup.Text>Date</InputGroup.Text>
<Form.Control type="date" defaultValue={"yyyy-mm-dd"}
ref={dateText} required/>
</InputGroup>
</Col>
</Row>
<Row>
<Col>
<InputGroup hasValidation>
<InputGroup.Text>Service</InputGroup.Text>
<Form.Control type="text" placeholder={""}
ref={maintenanceText} required/>
</InputGroup>
</Col>
<Col>
<InputGroup hasValidation>
<InputGroup.Text>Cost</InputGroup.Text>
<Form.Control type="number" placeholder={"0.00"}
ref={costText} required/>
</InputGroup>
</Col>
</Row>

</Form.Group>
</Form>
</Modal.Body>
<Modal.Footer className={'mx-auto'}>
<Button variant="secondary" onClick={handleClose}>Close</Button>
<Button variant="success" onClick={submit}>Upload</Button>
</Modal.Footer>
</Modal.Dialog>
</Modal>
</>
)
}

Expand Down
14 changes: 7 additions & 7 deletions frontend/src/pages/maintenance-history/UploadNavbar.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import {Link} from "react-router-dom";
import Button from "react-bootstrap/Button";
import Manipulator from "bootstrap/js/src/dom/manipulator";
import ManualVehicleHistory from "./ManualVehicleHistory";

function UploadNavbar() {
function UploadNavbar(props) {
return (
<div className="container">
<div className="row">
<div className="col-md-1 order-md-1"/>
<div className="col-md-10 order-md-2">
<div className="d-flex justify-content-center">
<Link to={"/garage/vehicle-history/upload"} className="me-3">
<Button variant="primary">Upload</Button>
</Link>

<Link to={"/garage/vehicle-history/manual"} className="ml-3">
<Button variant="primary">Manual</Button>
<Link to={"/garage/vehicle-history/upload"} className="me-3"
onClick={(e) => e.preventDefault()}>
<Button variant="primary" disabled>Upload</Button>
</Link>
<ManualVehicleHistory configId={props.configId}/>
</div>
</div>
<div className="col-md-1 order-md-3"/>
Expand Down
30 changes: 16 additions & 14 deletions frontend/src/pages/vehicle/VehicleHistory.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {BrowserRouter, Routes, Route, useParams, useLocation} from "react-router-dom";
import {Routes, Route, useLocation} from "react-router-dom";
import VehicleNavbar from "./VehicleNavbar";
import UploadNavbar from "../maintenance-history/UploadNavbar";
import {useEffect, useRef, useState} from "react";
Expand Down Expand Up @@ -40,7 +40,7 @@ function VehicleHistory(props) {

return (
<div className="container">
<VehicleNavbar selected={"history"}/>
<VehicleNavbar selected={"history"} configId={configId}/>
<Routes>
<Route path="/garage/vehicle-info/:vehicle/*"/>
<Route path="/garage/vehicle-history/:vehicle/*"/>
Expand All @@ -54,14 +54,10 @@ function VehicleHistory(props) {
<div className="col-md-10 mt-4">
<Row>
<Col>
<h1>Vehicle History</h1>
<h1>Maintenance History</h1>
</Col>
<Col>
<UploadNavbar/>
<Routes>
<Route path="/garage/vehicle-history/upload"/>
<Route path="/garage/vehicle-history/manual"/>
</Routes>
<UploadNavbar configId={configId}/>
</Col>
<Col/>
</Row>
Expand Down Expand Up @@ -172,14 +168,20 @@ function UpdateMaintenanceHistory(props) {
<Col>
<InputGroup hasValidation>
<InputGroup.Text>Type</InputGroup.Text>
<Form.Control type="text" placeholder={props.maintenanceInfo.type}
ref={typeText} required/>
<Form.Select type="as" defaultValue={props.maintenanceInfo.type}
ref={typeText} required>
<option value="Maintenance">Maintenance</option>
<option value="Repair">Repair</option>
<option value="Inspection">Inspection</option>
<option value="Replacement">Replacement</option>
<option value="Other">Other</option>
</Form.Select>
</InputGroup>
</Col>
<Col>
<InputGroup hasValidation>
<InputGroup.Text>Date</InputGroup.Text>
<Form.Control type="text" placeholder={props.maintenanceInfo.date}
<Form.Control type="date" placeholder={props.maintenanceInfo.date}
ref={dateText} required/>
</InputGroup>
</Col>
Expand All @@ -195,7 +197,7 @@ function UpdateMaintenanceHistory(props) {
<Col>
<InputGroup hasValidation>
<InputGroup.Text>Cost</InputGroup.Text>
<Form.Control type="text" placeholder={props.maintenanceInfo.cost}
<Form.Control type="number" placeholder={props.maintenanceInfo.cost}
ref={costText} required/>
</InputGroup>
</Col>
Expand All @@ -204,9 +206,9 @@ function UpdateMaintenanceHistory(props) {
</Form.Group>
</Form>
</Modal.Body>
<Modal.Footer>
<Button variant="primary" onClick={submit}>Save changes</Button>
<Modal.Footer className={'mx-auto'}>
<Button variant="secondary" onClick={handleClose}>Close</Button>
<Button variant="success" onClick={submit}>Save changes</Button>
</Modal.Footer>
</Modal.Dialog>
</Modal>
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/pages/vehicle/VehicleInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function VehicleInfo() {

return (
<div className="container">
<VehicleNavbar selected={"info"}/>
<VehicleNavbar selected={"info"} configId={configId}/>
<Routes>
<Route path="/garage/vehicle-info/:vehicle/*" />
<Route path="/garage/vehicle-history/:vehicle/*" />
Expand Down

0 comments on commit 7f707ba

Please sign in to comment.