diff --git a/frontend/src/pages/RobotDetails.tsx b/frontend/src/pages/RobotDetails.tsx index 3b5a8a81..aebdf3c3 100644 --- a/frontend/src/pages/RobotDetails.tsx +++ b/frontend/src/pages/RobotDetails.tsx @@ -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(null); + const [parts, setParts] = useState([]); const [imageIndex, setImageIndex] = useState(0); const [error, setError] = useState(null); @@ -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); @@ -117,14 +132,13 @@ const RobotDetails = () => { Name Quantity - Price - {response.bom.map((part, key) => ( + {parts.map((part, key) => ( - {part.part_id} + {part.part_name} {part.quantity} diff --git a/frontend/src/pages/RobotForm.tsx b/frontend/src/pages/RobotForm.tsx index 616ab77a..1deaf800 100644 --- a/frontend/src/pages/RobotForm.tsx +++ b/frontend/src/pages/RobotForm.tsx @@ -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 = () => { @@ -11,6 +11,7 @@ const RobotForm: React.FC = () => { const [robot_description, setDescription] = useState(""); const [robot_bom, setBom] = useState([]); const [robot_images, setImages] = useState([]); + const [parts, setParts] = useState([]); const handleImageChange = ( index: number, @@ -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, }; @@ -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 (

Add a New Robot

@@ -148,16 +161,25 @@ const RobotForm: React.FC = () => { {robot_bom.map((bom, index) => ( - Part Name: + Part: handleBomChange(index, e)} required - /> + > + + {parts.map((part, index) => ( + + ))} + Quantity: