Advertisement
fabiobiondi

React Pro - Real World App - Ch9. 07. Product List Component

Mar 23rd, 2023
946
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ======================================================
  2. // pages/cms/products/components/CMSProductsList.tsx
  3. // ======================================================
  4.  
  5. import clsx from 'clsx';
  6. import { Product } from '@/model/product';
  7.  
  8. interface CMSProductsListProps {
  9.   items: Product[];
  10.   activeItem: Partial<Product> | null;
  11.   onEditItem: (product: Partial<Product>) => void;
  12.   onDeleteItem: (id: string) => void;
  13. }
  14.  
  15. export function CMSProductsList(props: CMSProductsListProps) {
  16.   return (
  17.     <div className="mt-12">
  18.       <table className="table-auto w-full hover">
  19.         <thead>
  20.         <tr>
  21.           <th className="text-left">PRODUCTS</th>
  22.           <th className="text-left">IMAGE</th>
  23.           <th>COST</th>
  24.           <th>DELETE</th>
  25.         </tr>
  26.         </thead>
  27.  
  28.         <tbody>
  29.         {
  30.           props.items.map(item => {
  31.             return (
  32.               <tr
  33.                 key={item.id}
  34.                 className={clsx(
  35.                   'cursor-pointer',
  36.                   { 'bg-sky-200 text-black pointer-events-none': item.id === props.activeItem?.id }
  37.                 )}
  38.                 onClick={() => {
  39.                   props.onEditItem(item)
  40.                 }}
  41.               >
  42.                 <td>{item.name}</td>
  43.                 <td>
  44.                   { item.tmb && <img src={item.tmb} alt={item.name} className="h-16 rounded-xl"/>}
  45.                 </td>
  46.                 <td className="text-center">{item.cost}</td>
  47.                 <td className="text-center">
  48.                   <i
  49.                     className="fa fa-trash"
  50.                     onClick={(e) => {
  51.                       e.stopPropagation()
  52.                       props.onDeleteItem(item.id)
  53.                     }}
  54.                   />
  55.                 </td>
  56.               </tr>
  57.             )
  58.           })
  59.         }
  60.         </tbody>
  61.       </table>
  62.     </div>
  63.   )
  64. }
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71. // ======================================================
  72. //pages/cms/products/CMSProductsPage.tsx
  73. // ======================================================
  74. import { useEffect } from 'react';
  75. import { useProductsService } from '@/services/products';
  76. import { ServerError, Spinner } from '@/shared/';
  77. import { CMSProductsList } from './components/CMSProductsList';
  78.  
  79. export function CMSProductsPage() {
  80.   const { state, actions } = useProductsService();
  81.  
  82.   useEffect(() => {
  83.     actions.getProducts()
  84.   }, [])
  85.  
  86.   return (
  87.     <div>
  88.       <h1 className="title">CMS</h1>
  89.  
  90.       {state.pending && <Spinner />}
  91.       {state.error && <ServerError message={state.error} />}
  92.  
  93.       <CMSProductsList
  94.         items={state.products}
  95.         activeItem={state.activeItem}
  96.         onEditItem={actions.setActiveItem}
  97.         onDeleteItem={actions.deleteProduct}
  98.       />
  99.  
  100.       <pre>{JSON.stringify(state.activeItem, null, 2)}</pre>
  101.     </div>
  102.   )
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement