Advertisement
fabiobiondi

React Pro - Real World App - Ch9. 08. Product Form: creare un side panel con transitions

Mar 23rd, 2023
919
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ======================================================
  2. // pages/cms/products/components/CMSProductForm.tsx
  3. // ======================================================
  4.  
  5. import { Product } from '@/model/product';
  6. import clsx from 'clsx';
  7.  
  8. export interface CMSProductFormProps {
  9.   activeItem: Partial<Product> | null;
  10.   onClose: () => void;
  11. }
  12.  
  13. export function CMSProductForm(
  14.   props: CMSProductFormProps
  15. ) {
  16.   return (
  17.     <div className={clsx(
  18.       'fixed bg-slate-200 z-10 text-black top-0 w-96  h-full transition-all',
  19.       {'-right-96': !props.activeItem, 'right-0': props.activeItem}
  20.     )}>
  21.  
  22.       <div className="flex justify-around h-16">
  23.         <button
  24.           className="text-white w-1/2 bg-green-500 hover:bg-green-600"
  25.           onClick={props.onClose}
  26.         >SAVE</button>
  27.         <button
  28.           className="text-white w-1/2 bg-slate-500 hover:bg-slate-600"
  29.           onClick={props.onClose}
  30.         >CLOSE</button>
  31.       </div>
  32.  
  33.       {props.activeItem?.name}
  34.  
  35.     </div>
  36.   )
  37. }
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44. // ======================================================
  45. //pages/cms/products/CMSProductsPage.tsx
  46. // ======================================================
  47. import { useEffect } from 'react';
  48. import { useProductsService } from '@/services/products';
  49. import { ServerError, Spinner } from '@/shared/';
  50. import { CMSProductForm } from './components/CMSProductForm';
  51. import { CMSProductsList } from './components/CMSProductsList';
  52.  
  53. export function CMSProductsPage() {
  54.   const { state, actions } = useProductsService();
  55.  
  56.   useEffect(() => {
  57.     actions.getProducts()
  58.   }, [])
  59.  
  60.   return (
  61.     <div>
  62.       <h1 className="title">CMS</h1>
  63.  
  64.       {state.pending && <Spinner />}
  65.       {state.error && <ServerError message={state.error} />}
  66.  
  67.       <CMSProductForm
  68.         activeItem={state.activeItem}
  69.         onClose={actions.resetActiveItem}
  70.       />
  71.  
  72.       <CMSProductsList
  73.         items={state.products}
  74.         activeItem={state.activeItem}
  75.         onEditItem={actions.setActiveItem}
  76.         onDeleteItem={actions.deleteProduct}
  77.       />
  78.  
  79.     </div>
  80.   )
  81. }
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement