import * as React from 'react' import { RouteComponentProps } from "react-router"; import { Product } from './types'; export type ProductRouteComponentProps = {} export class ProductRouteComponent extends React.Component, ProductComponentState> { render() { let p_id = this.props.match.params.id p_id == "" ? undefined : isNaN(p_id) ? undefined : +this.props.product_id return } } export type ProductComponentProps = { product_id?: number product?: Product add_product?: () => void } export type ProductComponentState = { product?: { kind: "loaded", product: Product } | { kind: "loading" } | { kind: "error-or-not-found" } } export class ProductComponent extends React.Component { constructor(props: ProductComponentProps) { super(props) this.state = {} } getProduct() { {/* TODO 11: complete the implementation of the type below (1 pt) */} fetch(`/Cart/GetProduct/${this.props.product_id}`, { method: "GET", credentials: "include" }) .then(async (response) => { try { if (!response.ok) { this.setState(s => ({ ...s, product: { kind: "error-or-not-found"} })); } let responseData = await response.json(); this.setState(s => ({ ...s, product: { kind: "loaded", product: responseData }})); } catch (error) { this.setState(s => ({ ...s, product: { kind: "error-or-not-found"} })); } }) } componentWillMount() { if (this.props.product != undefined) { this.setState(s => ({ ...s, product: { kind: "loaded", product: this.props.product } })) return } else if (this.props.product_id) { this.setState(s => ({ ...s, product: { kind: "loading" } }), () => { this.getProduct() }) } else { this.setState(s => ({ ...s, product: { kind: "error-or-not-found" } })) } } public render() { if (this.state.product.kind != "loaded") { return

{this.state.product.kind}

} return
{/* TODO 12: complete the implementation of the type below (1 pt) NOTE: when rendering a product you should be able to add it to the cart (hint button) */}

Product

Id:{this.state.product.product.Id}
Name:{this.state.product.product.Name}
Price:{this.state.product.product.Price}
{this.props.add_product ? : null}
} }