import React, { Component } from "react"; import BeldenLogo from "../assets/images/belden-white.png"; import DefaultPartImg from "../assets/images/part.png"; import { NavLink } from "react-router-dom"; import "../assets/stylesheets/cabledetail.scss"; class Cable { constructor(cableData) { this.partnum = cableData.partnum; this.id = cableData.id; this.brand = cableData.brand; this.position = cableData.position; this.description = cableData.description ? cableData.image : "No description available."; this.image = cableData.image ? `http://localhost${cableData.image}` : DefaultPartImg; this.dynamicProps = {}; Object.keys(cableData).forEach((key) => { if (!["partnum", "id", "brand", "position", "image"].includes(key)) { this.dynamicProps[key] = cableData[key]; } }); } } export default class CableDetailComponent extends Component { constructor(props) { super(props); this.state = { cableDetails: null, }; } componentDidMount() { const cablePos = window.location.href.split("/").pop(); this.props.socket.send( `{"type":"cable_details","call":"request","data":{"position":["${cablePos}"]}}` ); this.props.socket.addEventListener("message", this.handleMessage); } handleMessage = (event) => { try { console.log("Message from server", event.data); const message = JSON.parse(event.data); const cableDetails = new Cable(message.data.cables[0]); this.setState({ cableDetails }); } catch (error) { console.error("Error parsing message from server:", error); } }; componentWillUnmount() { this.props.socket.removeEventListener("message", this.handleMessage); } renderTables() { const { cableDetails } = this.state; if (!cableDetails) return null; return Object.keys(cableDetails.dynamicProps).map((key) => (

{key.replace(/([A-Z])/g, " $1").trim()}

{Object.entries(cableDetails.dynamicProps[key]).map( ([propKey, value]) => ( ) )}
{propKey} {Array.isArray(value) ? value.join(", ") : value}
)); } render() { const { cableDetails } = this.state; return (
Belden
⬅️ Back
{cableDetails ? (
Cable
{cableDetails.partnum}
{cableDetails.description}
Brand:{" "} {cableDetails.brand}
Position:{" "} {cableDetails.position}
) : (
)}
➤ Datasheet ✏️
➤ Show 💡
➤ Dispense 🤲
); } }