| 1 |
import { useEffect, useState } from "@wordpress/element"; |
| 2 |
import { jsonStringify } from "../blocks/shared/helpFn"; |
| 3 |
import { filterDndSelectValues, queryFn } from "../controls/controls"; |
| 4 |
import { useSelect } from "@wordpress/data"; |
| 5 |
|
| 6 |
const useAllPosts = (attributes) => { |
| 7 |
const { |
| 8 |
postType, |
| 9 |
multiplePostType, |
| 10 |
quickQuery, |
| 11 |
postLimit, |
| 12 |
offset, |
| 13 |
categories, |
| 14 |
filterByAuthor, |
| 15 |
filterByDate, |
| 16 |
filterByKeyword, |
| 17 |
filterByCustomFields, |
| 18 |
orderBy, |
| 19 |
orderDirection, |
| 20 |
excludePost, |
| 21 |
excludeTerm, |
| 22 |
excludeAuthor, |
| 23 |
excludeStickyPosts, |
| 24 |
excludeCurrentPosts, |
| 25 |
excludeProtectedPosts, |
| 26 |
excludeChildrenPosts, |
| 27 |
excludePostWithoutImagePosts, |
| 28 |
termId, |
| 29 |
filterProduct, |
| 30 |
multipleFilterRelation, |
| 31 |
relation, |
| 32 |
specificDate, |
| 33 |
specificMonth, |
| 34 |
specificYear, |
| 35 |
specificPeriodAfter, |
| 36 |
specificPeriodBefore, |
| 37 |
specificDateBefore, |
| 38 |
specificDateAfter, |
| 39 |
keywordSearch, |
| 40 |
currentPage, |
| 41 |
itemsPerPage, |
| 42 |
blockName, |
| 43 |
displayAdvertisement, |
| 44 |
page_id, |
| 45 |
excludeDateAfter, |
| 46 |
excludeDateBefore, |
| 47 |
liveSearchText, |
| 48 |
} = attributes; |
| 49 |
|
| 50 |
const filteredExcludePost = filterDndSelectValues(excludePost); |
| 51 |
const coreEditor = useSelect((select) => select("core/editor")); |
| 52 |
|
| 53 |
const postId = page_id ? page_id : coreEditor?.getCurrentPostId(); |
| 54 |
|
| 55 |
const dependencies = { |
| 56 |
postType, |
| 57 |
blockName, |
| 58 |
quickQuery, |
| 59 |
postLimit, |
| 60 |
offset, |
| 61 |
categories, |
| 62 |
filterByAuthor, |
| 63 |
filterByDate, |
| 64 |
filterByKeyword, |
| 65 |
filterByCustomFields, |
| 66 |
orderBy, |
| 67 |
orderDirection, |
| 68 |
excludePost: filteredExcludePost, |
| 69 |
excludeTerm, |
| 70 |
excludeAuthor, |
| 71 |
excludeStickyPosts, |
| 72 |
excludeCurrentPosts, |
| 73 |
excludeProtectedPosts, |
| 74 |
excludeChildrenPosts, |
| 75 |
excludePostWithoutImagePosts, |
| 76 |
termId, |
| 77 |
filterProduct, |
| 78 |
relation, |
| 79 |
specificDate, |
| 80 |
specificMonth, |
| 81 |
specificYear, |
| 82 |
specificPeriodAfter, |
| 83 |
specificPeriodBefore, |
| 84 |
specificDateBefore, |
| 85 |
specificDateAfter, |
| 86 |
keywordSearch, |
| 87 |
currentPage, |
| 88 |
itemsPerPage, |
| 89 |
displayAdvertisement, |
| 90 |
multiplePostType, |
| 91 |
postId, |
| 92 |
page_id, |
| 93 |
excludeDateAfter, |
| 94 |
excludeDateBefore, |
| 95 |
liveSearchText, |
| 96 |
multipleFilterRelation |
| 97 |
}; |
| 98 |
|
| 99 |
const [allData, setAllData] = useState({ allPosts: [] }); |
| 100 |
|
| 101 |
const fetchAllPosts = async () => { |
| 102 |
const data = new FormData(); |
| 103 |
data.append("nonce", sp_smart_post_block_localize.ajaxNonce); |
| 104 |
data.append("action", "sp_smart_post_block_all_post_query"); |
| 105 |
data.append( |
| 106 |
"postQueryData", |
| 107 |
jsonStringify({ |
| 108 |
...dependencies, |
| 109 |
}) |
| 110 |
); |
| 111 |
try { |
| 112 |
const { posts } = await queryFn(data); |
| 113 |
|
| 114 |
setAllData({ allPosts: posts }); |
| 115 |
} catch (error) { |
| 116 |
console.error("Error fetching all posts:", error.message); |
| 117 |
} |
| 118 |
}; |
| 119 |
|
| 120 |
useEffect(() => { |
| 121 |
fetchAllPosts(); |
| 122 |
}, [jsonStringify(dependencies)]); |
| 123 |
return allData; |
| 124 |
}; |
| 125 |
|
| 126 |
export default useAllPosts; |
| 127 |
|