Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some frontend fixes wrt parts #53

Merged
merged 3 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions frontend/src/pages/RobotDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,19 @@ interface RobotDetailsResponse {
bom: Bom[];
}

interface ExtendedBom {
part_id: string;
quantity: number;
part_name: string;
}

const RobotDetails = () => {
const auth = useAuthentication();
const auth_api = new api(auth.api);
const { id } = useParams();
const [show, setShow] = useState(false);
const [robot, setRobot] = useState<RobotDetailsResponse | null>(null);
const [parts, setParts] = useState<ExtendedBom[]>([]);
const [imageIndex, setImageIndex] = useState(0);
const [error, setError] = useState<string | null>(null);

Expand All @@ -38,6 +45,14 @@ const RobotDetails = () => {
try {
const robotData = await auth_api.getRobotById(id);
setRobot(robotData);
const parts = robotData.bom.map(async (part) => {
return {
part_name: (await auth_api.getPartById(part.part_id)).part_name,
part_id: part.part_id,
quantity: part.quantity,
};
});
setParts(await Promise.all(parts));
} catch (err) {
if (err instanceof Error) {
setError(err.message);
Expand Down Expand Up @@ -117,14 +132,13 @@ const RobotDetails = () => {
<tr>
<th>Name</th>
<th>Quantity</th>
<th>Price</th>
</tr>
</thead>
<tbody>
{response.bom.map((part, key) => (
{parts.map((part, key) => (
<tr key={key}>
<td>
<Link to={`/part/${part.part_id}`}>{part.part_id}</Link>
<Link to={`/part/${part.part_id}`}>{part.part_name}</Link>
</td>
<td>{part.quantity}</td>
</tr>
Expand Down
34 changes: 28 additions & 6 deletions frontend/src/pages/RobotForm.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { api, Bom, Image, Robot } from "hooks/api";
import { api, Bom, Image, Part, Robot } from "hooks/api";
import { useAuthentication } from "hooks/auth";
import React, { ChangeEvent, FormEvent, useState } from "react";
import React, { ChangeEvent, FormEvent, useEffect, useState } from "react";
import { Button, Col, Form, Row } from "react-bootstrap";

const RobotForm: React.FC = () => {
Expand All @@ -11,6 +11,7 @@ const RobotForm: React.FC = () => {
const [robot_description, setDescription] = useState<string>("");
const [robot_bom, setBom] = useState<Bom[]>([]);
const [robot_images, setImages] = useState<Image[]>([]);
const [parts, setParts] = useState<Part[]>([]);

const handleImageChange = (
index: number,
Expand Down Expand Up @@ -65,7 +66,7 @@ const RobotForm: React.FC = () => {
robot_id: "",
name: robot_name,
description: robot_description,
owner: "Bob",
owner: "",
bom: robot_bom,
images: robot_images,
};
Expand All @@ -77,6 +78,18 @@ const RobotForm: React.FC = () => {
}
};

useEffect(() => {
const fetchParts = async () => {
try {
const partsQuery = await auth_api.getParts();
setParts(partsQuery);
} catch (err) {
console.error(err);
}
};
fetchParts();
}, []);

return (
<Row>
<h2>Add a New Robot</h2>
Expand Down Expand Up @@ -148,16 +161,25 @@ const RobotForm: React.FC = () => {
{robot_bom.map((bom, index) => (
<Row key={index} className="mb-3">
<Col md={12}>
Part Name:
Part:
<Form.Control
className="mb-1"
type="text"
as="select"
placeholder="Part Id: "
name="part_id"
value={bom.part_id}
onChange={(e) => handleBomChange(index, e)}
required
/>
>
<option value="" disabled>
Select a Part
</option>
{parts.map((part, index) => (
<option key={index} value={part.part_id}>
{part.part_name}
</option>
))}
</Form.Control>
Quantity:
<Form.Control
className="mb-1"
Expand Down
Loading