| 1 |
import { useEffect, useState } from "@wordpress/element"; |
| 2 |
import { breakpoint, inArray } 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 = false, |
| 11 |
gridRef, |
| 12 |
}) => { |
| 13 |
const { postGridLayout, contentVerticalPosition, gridFiveColumns, masonryGap } = attributes; |
| 14 |
|
| 15 |
const deviceType = breakpoint(); |
| 16 |
|
| 17 |
const dynamicPosts = |
| 18 |
"Mobile" !== deviceType |
| 19 |
? inArray(["grid-five-layout-one", "grid-five-layout-two", "grid-five-layout-three"], postGridLayout) |
| 20 |
? 3 |
| 21 |
: 4 |
| 22 |
: 0; |
| 23 |
|
| 24 |
const masonryGapCss = `${masonryGap?.device?.Desktop}${masonryGap?.unit?.Desktop}`; |
| 25 |
|
| 26 |
const columnsBreakPoints = Masonry |
| 27 |
? { |
| 28 |
columnsCountBreakPoints: { |
| 29 |
350: gridFiveColumns?.device?.Mobile, |
| 30 |
750: gridFiveColumns?.device?.Tablet, |
| 31 |
900: gridFiveColumns?.device?.Desktop, |
| 32 |
}, |
| 33 |
style: { marginTop: masonryGapCss }, |
| 34 |
} |
| 35 |
: {}; |
| 36 |
|
| 37 |
const gutter = Masonry |
| 38 |
? { |
| 39 |
gutter: masonryGapCss, |
| 40 |
} |
| 41 |
: {}; |
| 42 |
|
| 43 |
const [selectedPostId, setSelectedPostId] = useState(null); |
| 44 |
|
| 45 |
useEffect(() => { |
| 46 |
const handleClickOutside = (event) => { |
| 47 |
if (gridRef.current && !gridRef.current.contains(event.target)) { |
| 48 |
setSelectedPostId(null); |
| 49 |
} |
| 50 |
}; |
| 51 |
|
| 52 |
document.addEventListener("mousedown", handleClickOutside); |
| 53 |
return () => document.removeEventListener("mousedown", handleClickOutside); |
| 54 |
}, []); |
| 55 |
|
| 56 |
return ( |
| 57 |
<> |
| 58 |
{Masonry && ( |
| 59 |
<ResponsiveMasonry {...columnsBreakPoints}> |
| 60 |
<Masonry {...gutter}> |
| 61 |
{posts |
| 62 |
?.slice(bottomStyle ? dynamicPosts : 0, bottomStyle ? posts?.length : dynamicPosts) |
| 63 |
?.map((post) => ( |
| 64 |
<TemplateOne |
| 65 |
key={post?.post_id} |
| 66 |
data={post} |
| 67 |
posts={posts} |
| 68 |
attributes={attributes} |
| 69 |
horizontalAlign={bottomStyle ? "" : false} |
| 70 |
contentPosition={bottomStyle ? "" : contentVerticalPosition} |
| 71 |
isSelected={selectedPostId === post?.post_id} |
| 72 |
onSelect={() => setSelectedPostId(post?.post_id)} |
| 73 |
/> |
| 74 |
))} |
| 75 |
</Masonry> |
| 76 |
</ResponsiveMasonry> |
| 77 |
)} |
| 78 |
{!Masonry && |
| 79 |
posts |
| 80 |
?.slice(bottomStyle ? dynamicPosts : 0, bottomStyle ? posts?.length : dynamicPosts) |
| 81 |
?.map((post) => ( |
| 82 |
<TemplateOne |
| 83 |
key={post?.post_id} |
| 84 |
data={post} |
| 85 |
posts={posts} |
| 86 |
attributes={attributes} |
| 87 |
horizontalAlign={bottomStyle ? "" : false} |
| 88 |
contentPosition={bottomStyle ? "" : contentVerticalPosition} |
| 89 |
isSelected={selectedPostId === post?.post_id} |
| 90 |
onSelect={() => setSelectedPostId(post?.post_id)} |
| 91 |
/> |
| 92 |
))} |
| 93 |
</> |
| 94 |
); |
| 95 |
}; |
| 96 |
|
| 97 |
export default LoopContent; |
| 98 |
|