import React from 'react'; import axios from 'axios'; import { ProductClient, ProductListItem } from '../../APIClient'; import { Layout } from '../shared/Layout'; import Swal from 'sweetalert2'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faEdit, faTrash } from '@fortawesome/free-solid-svg-icons'; import { useRouter } from 'next/router'; import Link from 'next/link'; import { DeleteProductProps, ProductDataProps, ProductListRowsProps, ProductState } from '../../interfaces/product'; const DeleteProductButton: React.FunctionComponent = ({ productID, onDelete }) => { const onClickDelete = async () => { const result = await Swal.fire({ title: 'Confirm delete?', text: 'Delete product?', icon: 'warning', showCancelButton: true, }) if (result.isConfirmed) { onDelete(productID); } } return ( ); } const EditProductButton: React.FunctionComponent = ({ productID }) => { const router = useRouter(); return ( ); } const ProductListItemRows: React.FunctionComponent = ({ products, onDelete }) => { let rows = products.map(x => { return ( {x.productID} {x.name} {x.price} ); }) return ( {rows} ) } class Product extends React.Component<{},ProductState> { constructor(props) { super(props); this.state = { product: [], } } async componentDidMount() { this.getProducts(); } async getProducts() { try { const client = new ProductClient(); let data = await client.getAll(); console.log(data); this.setState({ product: data }); } catch (error) { this.setState({ product: [], }); } } async deleteProduct(productId) { try { const client = new ProductClient(); await client.delete(productId); // Remove the product in the state by filter()ing it let product = this.state.product.filter(x => x.productID != productId); this.setState({ product }) Swal.fire({ title: "Deleted!", text: "Product is deleted in database", }) } catch (error) { alert(error); } } render() { return (

Products

ID Name Price Edit Delete
Create
); } } export default function ProductPage() { return ( ) }