| 1 |
import { Fragment, useEffect, useState } from "@wordpress/element"; |
| 2 |
import { skipItemsForGridThreeTopSection } from "../../controls/constants"; |
| 3 |
import { useDeviceType } 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, contentAreaHeight, contentVerticalPosition, gridThreeColumns, masonryGap } = attributes; |
| 15 |
|
| 16 |
const deviceType = useDeviceType(); |
| 17 |
|
| 18 |
const skipPostForLargeLayouts = "Mobile" !== deviceType ? skipItemsForGridThreeTopSection[postGridLayout] : 0; |
| 19 |
|
| 20 |
const masonryGapCss = `${masonryGap?.device?.Desktop}${masonryGap?.unit?.Desktop}`; |
| 21 |
|
| 22 |
const columnsBreakPoints = Masonry |
| 23 |
? { |
| 24 |
columnsCountBreakPoints: { |
| 25 |
350: gridThreeColumns?.device?.Mobile, |
| 26 |
750: gridThreeColumns?.device?.Tablet, |
| 27 |
900: gridThreeColumns?.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 |
const targetPostId = parseInt(event.target.closest(".sp-smart-post-card-image")?.dataset?.post_id); |
| 45 |
if (targetPostId && targetPostId !== selectedPostId) { |
| 46 |
setSelectedPostId(null); |
| 47 |
} |
| 48 |
}; |
| 49 |
|
| 50 |
document.addEventListener("mousedown", handleClickOutside); |
| 51 |
return () => document.removeEventListener("mousedown", handleClickOutside); |
| 52 |
}, [selectedPostId]); |
| 53 |
|
| 54 |
return ( |
| 55 |
<> |
| 56 |
{Masonry && ( |
| 57 |
<ResponsiveMasonry {...columnsBreakPoints}> |
| 58 |
<Masonry {...gutter}> |
| 59 |
{posts |
| 60 |
?.slice( |
| 61 |
bottomStyle ? skipPostForLargeLayouts : 0, |
| 62 |
bottomStyle ? posts?.length : skipPostForLargeLayouts |
| 63 |
) |
| 64 |
?.map((post, i) => ( |
| 65 |
<TemplateOne |
| 66 |
key={post?.post_id} |
| 67 |
data={post} |
| 68 |
posts={posts} |
| 69 |
containerVAlign={false} |
| 70 |
horizontalAlign={false} |
| 71 |
contentPosition={contentVerticalPosition} |
| 72 |
attributes={attributes} |
| 73 |
contentHeight={`${ |
| 74 |
contentAreaHeight?.device?.[deviceType] + contentAreaHeight?.unit?.[deviceType] |
| 75 |
}`} |
| 76 |
layoutName={postGridLayout} |
| 77 |
isSelected={selectedPostId === post?.post_id} |
| 78 |
onSelect={() => setSelectedPostId(post?.post_id)} |
| 79 |
/> |
| 80 |
))} |
| 81 |
</Masonry> |
| 82 |
</ResponsiveMasonry> |
| 83 |
)} |
| 84 |
{!Masonry && |
| 85 |
posts |
| 86 |
?.slice( |
| 87 |
bottomStyle ? skipPostForLargeLayouts : 0, |
| 88 |
bottomStyle ? posts?.length : skipPostForLargeLayouts |
| 89 |
) |
| 90 |
?.map((post, i) => ( |
| 91 |
<TemplateOne |
| 92 |
key={post?.post_id} |
| 93 |
data={post} |
| 94 |
posts={posts} |
| 95 |
containerVAlign={false} |
| 96 |
horizontalAlign={false} |
| 97 |
contentPosition={contentVerticalPosition} |
| 98 |
attributes={attributes} |
| 99 |
contentHeight={`${ |
| 100 |
contentAreaHeight?.device?.[deviceType] + contentAreaHeight?.unit?.[deviceType] |
| 101 |
}`} |
| 102 |
layoutName={postGridLayout} |
| 103 |
isSelected={selectedPostId === post?.post_id} |
| 104 |
onSelect={() => setSelectedPostId(post?.post_id)} |
| 105 |
/> |
| 106 |
))} |
| 107 |
</> |
| 108 |
); |
| 109 |
}; |
| 110 |
|
| 111 |
export default LoopContent; |
| 112 |
|