SHOW:
|
|
- or go back to the newest paste.
1 | - | // pages/cms/products/CMSProductsPage.tsx |
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 | - | <div className="mt-12"> |
21 | + | <th className="text-left">PRODUCTS</th> |
22 | - | <table className="table-auto w-full hover"> |
22 | + | <th className="text-left">IMAGE</th> |
23 | - | <thead> |
23 | + | <th>COST</th> |
24 | - | <tr> |
24 | + | <th>DELETE</th> |
25 | - | <th className="text-left">PRODUCTS</th> |
25 | + | </tr> |
26 | - | <th className="text-left">IMAGE</th> |
26 | + | </thead> |
27 | - | <th>COST</th> |
27 | + | |
28 | - | <th>DELETE</th> |
28 | + | <tbody> |
29 | { | |
30 | - | </thead> |
30 | + | props.items.map(item => { |
31 | return ( | |
32 | - | <tbody> |
32 | + | <tr |
33 | - | { |
33 | + | key={item.id} |
34 | - | state.products.map(item => { |
34 | + | className={clsx( |
35 | - | return ( |
35 | + | 'cursor-pointer', |
36 | - | <tr |
36 | + | { 'bg-sky-200 text-black pointer-events-none': item.id === props.activeItem?.id } |
37 | - | key={item.id} |
37 | + | )} |
38 | - | className={clsx( |
38 | + | onClick={() => { |
39 | - | 'cursor-pointer', |
39 | + | props.onEditItem(item) |
40 | - | { 'bg-sky-200 text-black pointer-events-none': item.id === state.activeItem?.id } |
40 | + | }} |
41 | - | )} |
41 | + | > |
42 | - | onClick={() => { |
42 | + | <td>{item.name}</td> |
43 | - | actions.setActiveItem(item) |
43 | + | <td> |
44 | - | }} |
44 | + | { item.tmb && <img src={item.tmb} alt={item.name} className="h-16 rounded-xl"/>} |
45 | - | > |
45 | + | </td> |
46 | - | <td>{item.name}</td> |
46 | + | <td className="text-center">€ {item.cost}</td> |
47 | - | <td> |
47 | + | <td className="text-center"> |
48 | - | { item.tmb && <img src={item.tmb} alt={item.name} className="h-16 rounded-xl"/>} |
48 | + | <i |
49 | - | </td> |
49 | + | className="fa fa-trash" |
50 | - | <td className="text-center">€ {item.cost}</td> |
50 | + | onClick={(e) => { |
51 | - | <td className="text-center"> |
51 | + | e.stopPropagation() |
52 | - | <i |
52 | + | props.onDeleteItem(item.id) |
53 | - | className="fa fa-trash" |
53 | + | }} |
54 | - | onClick={(e) => { |
54 | + | /> |
55 | - | e.stopPropagation() |
55 | + | </td> |
56 | - | actions.deleteProduct(item.id) |
56 | + | |
57 | - | }} |
57 | + | ) |
58 | - | /> |
58 | + | }) |
59 | - | </td> |
59 | + | } |
60 | - | </tr> |
60 | + | </tbody> |
61 | - | ) |
61 | + | </table> |
62 | - | }) |
62 | + | |
63 | - | } |
63 | + | |
64 | - | </tbody> |
64 | + | |
65 | - | </table> |
65 | + | |
66 | - | </div> |
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 |