Advertisement
fabiobiondi

React Pro - Real World App - Ch9. 05. Popolare la tabella, cancellare e selezionare elementi e stopP

Mar 23rd, 2023
993
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // pages/cms/products/CMSProductsPage.tsx
  2. import { useProductsService } from '@/services/products';
  3. import { ServerError, Spinner } from '@/shared/';
  4. import { useEffect } from 'react';
  5.  
  6. export function CMSProductsPage() {
  7.   const { state, actions } = useProductsService();
  8.  
  9.   useEffect(() => {
  10.     actions.getProducts()
  11.   }, [])
  12.  
  13.   return (
  14.     <div>
  15.       <h1 className="title">CMS</h1>
  16.  
  17.       {state.pending && <Spinner />}
  18.       {state.error && <ServerError message={state.error} />}
  19.  
  20.       <div className="mt-12">
  21.         <table className="table-auto w-full hover">
  22.           <thead>
  23.               <tr>
  24.                 <th className="text-left">PRODUCTS</th>
  25.                 <th className="text-left">IMAGE</th>
  26.                 <th>COST</th>
  27.                 <th>DELETE</th>
  28.               </tr>
  29.           </thead>
  30.  
  31.           <tbody>
  32.           {
  33.             state.products.map(item => {
  34.               return (
  35.                 <tr
  36.                   key={item.id}
  37.                   onClick={() => {
  38.                     actions.setActiveItem(item)
  39.                   }}
  40.                 >
  41.                   <td>{item.name}</td>
  42.                   <td>
  43.                     { item.tmb && <img src={item.tmb} alt={item.name} className="h-16 rounded-xl"/>}
  44.                   </td>
  45.                   <td className="text-center">{item.cost}</td>
  46.                   <td className="text-center">
  47.                     <i
  48.                       className="fa fa-trash"
  49.                       onClick={(e) => {
  50.                         e.stopPropagation()
  51.                         actions.deleteProduct(item.id)
  52.                       }}
  53.                     />
  54.                   </td>
  55.                 </tr>
  56.               )
  57.             })
  58.           }
  59.           </tbody>
  60.         </table>
  61.       </div>
  62.  
  63.       <pre>{JSON.stringify(state.activeItem, null, 2)}</pre>
  64.     </div>
  65.   )
  66. }
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement