| 1 |
import { Fragment, useEffect, useState } from "@wordpress/element"; |
| 2 |
import TemplateOne from "../shared/templates/templateCards/templateOne"; |
| 3 |
import { breakpoint, inArray } from "../../controls/controls"; |
| 4 |
|
| 5 |
const LoopContent = ({ |
| 6 |
posts, |
| 7 |
attributes, |
| 8 |
bottomStyle = false, |
| 9 |
layoutStyle = "grid-six-layout-four", |
| 10 |
containerVAlign = false, |
| 11 |
horizontalAlign = false, |
| 12 |
Masonry = false, |
| 13 |
ResponsiveMasonry = Fragment, |
| 14 |
containerRef, |
| 15 |
}) => { |
| 16 |
const { postGridLayout, contentVerticalPosition, gridSixColumns, masonryGap } = attributes; |
| 17 |
|
| 18 |
const deviceType = breakpoint(); |
| 19 |
|
| 20 |
const largeItemsNumber = |
| 21 |
"Mobile" !== deviceType ? (inArray(["grid-six-layout-one", "grid-six-layout-two"], postGridLayout) ? 1 : 2) : 0; |
| 22 |
|
| 23 |
const masonryGapCss = `${masonryGap?.device?.Desktop}${masonryGap?.unit?.Desktop}`; |
| 24 |
|
| 25 |
const columnsBreakPoints = Masonry |
| 26 |
? { |
| 27 |
columnsCountBreakPoints: { |
| 28 |
350: gridSixColumns?.device?.Mobile, |
| 29 |
750: gridSixColumns?.device?.Tablet, |
| 30 |
900: gridSixColumns?.device?.Desktop, |
| 31 |
}, |
| 32 |
} |
| 33 |
: {}; |
| 34 |
|
| 35 |
const gutter = Masonry |
| 36 |
? { |
| 37 |
gutter: masonryGapCss, |
| 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 |
const targetPostId = parseInt(event.target.closest(".sp-smart-post-card-image")?.dataset?.post_id); |
| 48 |
if (targetPostId && targetPostId !== selectedPostId) { |
| 49 |
setSelectedPostId(null); |
| 50 |
} |
| 51 |
}; |
| 52 |
|
| 53 |
document.addEventListener("mousedown", handleClickOutside); |
| 54 |
return () => document.removeEventListener("mousedown", handleClickOutside); |
| 55 |
}, [selectedPostId]); |
| 56 |
|
| 57 |
return ( |
| 58 |
<> |
| 59 |
{Masonry && ( |
| 60 |
<ResponsiveMasonry {...columnsBreakPoints}> |
| 61 |
<Masonry {...gutter}> |
| 62 |
{posts |
| 63 |
?.slice(bottomStyle ? largeItemsNumber : 0, bottomStyle ? posts?.length : largeItemsNumber) |
| 64 |
?.map( |
| 65 |
(post) => ( |
| 66 |
// postGridLayout === layoutStyle ? ( |
| 67 |
<TemplateOne |
| 68 |
key={post?.post_id} |
| 69 |
data={post} |
| 70 |
posts={posts} |
| 71 |
containerVAlign={containerVAlign} |
| 72 |
horizontalAlign={horizontalAlign} |
| 73 |
attributes={attributes} |
| 74 |
contentPosition={contentVerticalPosition} |
| 75 |
isSelected={selectedPostId === post?.post_id} |
| 76 |
onSelect={() => setSelectedPostId(post?.post_id)} |
| 77 |
/> |
| 78 |
) |
| 79 |
// ) : ( |
| 80 |
// <TemplateOne |
| 81 |
// key={ post?.post_id } |
| 82 |
// data={ post } |
| 83 |
// posts={ posts } |
| 84 |
// attributes={ |
| 85 |
// attributes |
| 86 |
// } |
| 87 |
// contentPosition={ |
| 88 |
// contentVerticalPosition |
| 89 |
// } |
| 90 |
// isSelected={ selectedPostId === post?.post_id } |
| 91 |
// onSelect={ () => setSelectedPostId( post?.post_id ) } |
| 92 |
// /> |
| 93 |
// ) |
| 94 |
)} |
| 95 |
</Masonry> |
| 96 |
</ResponsiveMasonry> |
| 97 |
)} |
| 98 |
{!Masonry && |
| 99 |
posts?.slice(bottomStyle ? largeItemsNumber : 0, bottomStyle ? posts?.length : largeItemsNumber)?.map( |
| 100 |
(post) => ( |
| 101 |
// postGridLayout === layoutStyle ? ( |
| 102 |
<TemplateOne |
| 103 |
key={post?.post_id} |
| 104 |
data={post} |
| 105 |
posts={posts} |
| 106 |
containerVAlign={containerVAlign} |
| 107 |
horizontalAlign={horizontalAlign} |
| 108 |
attributes={attributes} |
| 109 |
contentPosition={contentVerticalPosition} |
| 110 |
isSelected={selectedPostId === post?.post_id} |
| 111 |
onSelect={() => setSelectedPostId(post?.post_id)} |
| 112 |
/> |
| 113 |
) |
| 114 |
// ) : ( |
| 115 |
// <TemplateOne |
| 116 |
// key={ post?.post_id } |
| 117 |
// data={ post } |
| 118 |
// posts={ posts } |
| 119 |
// attributes={ attributes } |
| 120 |
// contentPosition={ contentVerticalPosition } |
| 121 |
// isSelected={ selectedPostId === post?.post_id } |
| 122 |
// onSelect={ () => setSelectedPostId( post?.post_id ) } |
| 123 |
// /> |
| 124 |
// ) |
| 125 |
)} |
| 126 |
</> |
| 127 |
); |
| 128 |
}; |
| 129 |
|
| 130 |
export default LoopContent; |
| 131 |
|