| 1 |
import { gridFourLargeItemPosition, skipItemsForGridFourTopSection } from "../../controls/constants"; |
| 2 |
import { Fragment, useEffect, useState } from "@wordpress/element"; |
| 3 |
import { breakpoint } from "../../controls/controls"; |
| 4 |
import TemplateOne from "../shared/templates/templateCards/templateOne"; |
| 5 |
|
| 6 |
const LoopContent = ({ |
| 7 |
posts, |
| 8 |
attributes, |
| 9 |
bottomStyle = false, |
| 10 |
Masonry = false, |
| 11 |
ResponsiveMasonry = Fragment, |
| 12 |
containerRef, |
| 13 |
}) => { |
| 14 |
const { postGridLayout, contentVerticalPosition, gridFourColumns, masonryGap } = attributes; |
| 15 |
|
| 16 |
const deviceType = breakpoint(); |
| 17 |
|
| 18 |
const skipPostForLargeLayouts = "Mobile" !== deviceType ? skipItemsForGridFourTopSection[postGridLayout] : 0; |
| 19 |
|
| 20 |
const largeItemPosition = gridFourLargeItemPosition[postGridLayout]; |
| 21 |
|
| 22 |
const masonryGapCss = `${masonryGap?.device?.Desktop}${masonryGap?.unit?.Desktop}`; |
| 23 |
|
| 24 |
const columnsBreakPoints = Masonry |
| 25 |
? { |
| 26 |
columnsCountBreakPoints: { |
| 27 |
350: gridFourColumns?.device?.Mobile, |
| 28 |
750: gridFourColumns?.device?.Tablet, |
| 29 |
900: gridFourColumns?.device?.Desktop, |
| 30 |
}, |
| 31 |
} |
| 32 |
: {}; |
| 33 |
|
| 34 |
const gutter = Masonry |
| 35 |
? { |
| 36 |
gutter: masonryGapCss, |
| 37 |
} |
| 38 |
: {}; |
| 39 |
|
| 40 |
const [selectedPostId, setSelectedPostId] = useState(null); |
| 41 |
|
| 42 |
useEffect(() => { |
| 43 |
const handleClickOutside = (event) => { |
| 44 |
if (containerRef.current && !containerRef.current.contains(event.target)) { |
| 45 |
setSelectedPostId(null); |
| 46 |
} |
| 47 |
}; |
| 48 |
|
| 49 |
document.addEventListener("mousedown", handleClickOutside); |
| 50 |
return () => document.removeEventListener("mousedown", handleClickOutside); |
| 51 |
}, []); |
| 52 |
|
| 53 |
return ( |
| 54 |
<> |
| 55 |
{Masonry && ( |
| 56 |
<ResponsiveMasonry {...columnsBreakPoints}> |
| 57 |
<Masonry {...gutter}> |
| 58 |
{posts |
| 59 |
?.slice( |
| 60 |
bottomStyle ? skipPostForLargeLayouts : 0, |
| 61 |
bottomStyle ? posts?.length : skipPostForLargeLayouts |
| 62 |
) |
| 63 |
?.map((post, i) => ( |
| 64 |
<TemplateOne |
| 65 |
key={post?.post_id} |
| 66 |
index={i} |
| 67 |
data={post} |
| 68 |
posts={posts} |
| 69 |
contentPosition={contentVerticalPosition} |
| 70 |
attributes={attributes} |
| 71 |
largeItemIndex={bottomStyle ? "" : i === largeItemPosition ? "large" : "no"} |
| 72 |
isSelected={selectedPostId === post?.post_id} |
| 73 |
onSelect={() => setSelectedPostId(post?.post_id)} |
| 74 |
/> |
| 75 |
))} |
| 76 |
</Masonry> |
| 77 |
</ResponsiveMasonry> |
| 78 |
)} |
| 79 |
{!Masonry && |
| 80 |
posts |
| 81 |
?.slice( |
| 82 |
bottomStyle ? skipPostForLargeLayouts : 0, |
| 83 |
bottomStyle ? posts?.length : skipPostForLargeLayouts |
| 84 |
) |
| 85 |
?.map((post, i) => ( |
| 86 |
<TemplateOne |
| 87 |
key={post?.post_id} |
| 88 |
index={i} |
| 89 |
data={post} |
| 90 |
posts={posts} |
| 91 |
contentPosition={contentVerticalPosition} |
| 92 |
attributes={attributes} |
| 93 |
largeItemIndex={bottomStyle ? "" : i === largeItemPosition ? "large" : "no"} |
| 94 |
isSelected={selectedPostId === post?.post_id} |
| 95 |
onSelect={() => setSelectedPostId(post?.post_id)} |
| 96 |
/> |
| 97 |
))} |
| 98 |
</> |
| 99 |
); |
| 100 |
}; |
| 101 |
|
| 102 |
export default LoopContent; |
| 103 |
|