-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathBaseProductList.tsx
More file actions
72 lines (65 loc) · 1.94 KB
/
BaseProductList.tsx
File metadata and controls
72 lines (65 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import cn from 'classnames'
import { useCallback } from 'react'
import type { ReactNode } from 'react'
import { ProductCard } from '@/entities/product'
import type { Product, ProductId } from '@/entities/product'
import { selectIsAuthorized } from '@/entities/session'
import { AddToWishlistIcon } from '@/features/wishlist/addToWishlist'
import { useAppSelector } from '@/shared/redux'
import css from './BaseProductList.module.css'
type Props<T extends Product> = {
products: T[]
productCardBottomSlot?: (product: T) => ReactNode
productCardActionsSlot?: (productId: ProductId) => ReactNode
isFetching?: boolean
size?: 's' | 'm'
}
export function BaseProductList<T extends Product>(props: Props<T>) {
const { isFetching, products, size = 'm' } = props
const isAuthorized = useAppSelector(selectIsAuthorized)
const getActionSlot = useCallback(
(product: Product) => {
if (props.productCardActionsSlot) {
return props.productCardActionsSlot(product.id)
}
if (isAuthorized) {
return <AddToWishlistIcon productId={product.id} />
}
return null
},
[props.productCardActionsSlot, isAuthorized],
)
if (Boolean(isFetching) && products.length === 0) {
return <div className={css.root}>Fetching...</div>
}
/**
* ✅ FSD Best practice
*
* Receive product actions (add to wishlist)
* to render-prop to avoid entity cross-import
*/
return (
<div
data-fsd="widget/BaseProductList"
className={cn(
css.root,
isFetching && css.rootIsFetching,
css[`root_size_${size}`],
)}
>
{products.map(product => (
<ProductCard
size={size}
key={product.id}
product={product}
bottomContentSlot={
props.productCardBottomSlot
? props.productCardBottomSlot(product)
: null
}
actionSlot={getActionSlot(product)}
/>
))}
</div>
)
}