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