| 1 |
import TemplateOne from "../shared/templates/templateCards/templateOne"; |
| 2 |
|
| 3 |
import { availableLargeContent } from "../../controls/constants"; |
| 4 |
import { Fragment, useEffect, useState } from "@wordpress/element"; |
| 5 |
|
| 6 |
const LoopContent = ({ |
| 7 |
posts, |
| 8 |
attributes, |
| 9 |
largeItemIndex = false, |
| 10 |
containerRef, |
| 11 |
}) => { |
| 12 |
const { postGridLayout } = attributes; |
| 13 |
|
| 14 |
const isAvailableLargeItem = availableLargeContent.includes(postGridLayout); |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
const [selectedPostId, setSelectedPostId] = useState(null); |
| 19 |
|
| 20 |
useEffect(() => { |
| 21 |
const handleClickOutside = (event) => { |
| 22 |
if (containerRef.current && !containerRef.current.contains(event.target)) { |
| 23 |
setSelectedPostId(null); |
| 24 |
} |
| 25 |
}; |
| 26 |
|
| 27 |
document.addEventListener("mousedown", handleClickOutside); |
| 28 |
return () => document.removeEventListener("mousedown", handleClickOutside); |
| 29 |
}, []); |
| 30 |
|
| 31 |
return ( |
| 32 |
<> |
| 33 |
{posts?.map((post, i) => ( |
| 34 |
<Fragment key={i}> |
| 35 |
<TemplateOne |
| 36 |
key={post?.post_id} |
| 37 |
index={i} |
| 38 |
data={post} |
| 39 |
posts={posts} |
| 40 |
largeItem={isAvailableLargeItem} |
| 41 |
largeItemIndex={largeItemIndex} |
| 42 |
attributes={attributes} |
| 43 |
isSelected={selectedPostId === post?.post_id} |
| 44 |
onSelect={() => setSelectedPostId(post?.post_id)} |
| 45 |
/> |
| 46 |
</Fragment> |
| 47 |
))} |
| 48 |
</> |
| 49 |
); |
| 50 |
}; |
| 51 |
|
| 52 |
export default LoopContent; |
| 53 |
|