| 1 |
import { useEffect, useState } from "@wordpress/element"; |
| 2 |
import { jsonStringify } from "../blocks/shared/helpFn"; |
| 3 |
import { filterDndSelectValues, filteredTaxonomiesValues, queryFn } from "../controls/controls"; |
| 4 |
import { useSelect } from "@wordpress/data"; |
| 5 |
|
| 6 |
const useApiData = (attributes) => { |
| 7 |
const { |
| 8 |
postType, |
| 9 |
quickQuery, |
| 10 |
postLimit, |
| 11 |
offset, |
| 12 |
taxonomies, |
| 13 |
categories, |
| 14 |
filterByAuthor, |
| 15 |
filterByDate, |
| 16 |
filterByKeyword, |
| 17 |
filterByCustomFields, |
| 18 |
orderBy, |
| 19 |
orderDirection, |
| 20 |
includeOnlyPost, |
| 21 |
excludePost, |
| 22 |
excludeTerm, |
| 23 |
excludeAuthor, |
| 24 |
excludeStickyPosts, |
| 25 |
excludeCurrentPosts, |
| 26 |
excludeProtectedPosts, |
| 27 |
excludeChildrenPosts, |
| 28 |
excludePostWithoutImagePosts, |
| 29 |
termId, |
| 30 |
filterProduct, |
| 31 |
multipleFilterRelation, |
| 32 |
relation, |
| 33 |
specificDate, |
| 34 |
specificMonth, |
| 35 |
specificYear, |
| 36 |
specificPeriodAfter, |
| 37 |
specificPeriodBefore, |
| 38 |
specificDateBefore, |
| 39 |
specificDateAfter, |
| 40 |
keywordSearch, |
| 41 |
currentPage, |
| 42 |
blockName, |
| 43 |
displayAdvertisement, |
| 44 |
multiplePostType, |
| 45 |
page_id, |
| 46 |
excludeDateAfter, |
| 47 |
excludeDateBefore, |
| 48 |
customFieldRelation, |
| 49 |
activeSeoPlugin, |
| 50 |
seoMetaShow, |
| 51 |
metaDateCustomDateFormat, |
| 52 |
metaDateFormat, |
| 53 |
paginationEnable, |
| 54 |
imageLazyLoad, |
| 55 |
imageSrcset, |
| 56 |
imageSize, |
| 57 |
catTabCategoryType, |
| 58 |
} = attributes; |
| 59 |
const pageId = useSelect((select) => select("core/editor")?.getCurrentPostId()); |
| 60 |
const postId = page_id ? page_id : pageId; |
| 61 |
|
| 62 |
const [allData, setAllData] = useState({ |
| 63 |
posts: [], |
| 64 |
postCount: {}, |
| 65 |
postsStatus: true, |
| 66 |
}); |
| 67 |
const filteredTaxonomies = filteredTaxonomiesValues(taxonomies); |
| 68 |
const filteredIncludedPost = filterDndSelectValues(includeOnlyPost); |
| 69 |
const filteredExcludePost = filterDndSelectValues(excludePost); |
| 70 |
|
| 71 |
const dependencies = { |
| 72 |
postType, |
| 73 |
blockName, |
| 74 |
quickQuery, |
| 75 |
postLimit, |
| 76 |
categories, |
| 77 |
filterByAuthor, |
| 78 |
filterByDate, |
| 79 |
filterByKeyword, |
| 80 |
filterByCustomFields, |
| 81 |
orderBy, |
| 82 |
orderDirection, |
| 83 |
excludePost: filteredExcludePost, |
| 84 |
excludeTerm, |
| 85 |
excludeAuthor, |
| 86 |
excludeStickyPosts, |
| 87 |
excludeCurrentPosts, |
| 88 |
excludeProtectedPosts, |
| 89 |
excludeChildrenPosts, |
| 90 |
excludePostWithoutImagePosts, |
| 91 |
termId, |
| 92 |
filterProduct, |
| 93 |
multipleFilterRelation, |
| 94 |
relation, |
| 95 |
specificDate, |
| 96 |
specificMonth, |
| 97 |
specificYear, |
| 98 |
specificPeriodAfter, |
| 99 |
specificPeriodBefore, |
| 100 |
specificDateBefore, |
| 101 |
specificDateAfter, |
| 102 |
keywordSearch, |
| 103 |
currentPage, |
| 104 |
displayAdvertisement, |
| 105 |
multiplePostType, |
| 106 |
postId, |
| 107 |
page_id, |
| 108 |
excludeDateAfter, |
| 109 |
excludeDateBefore, |
| 110 |
customFieldRelation, |
| 111 |
activeSeoPlugin, |
| 112 |
seoMetaShow, |
| 113 |
metaDateCustomDateFormat, |
| 114 |
metaDateFormat, |
| 115 |
imageLazyLoad, |
| 116 |
imageSrcset, |
| 117 |
offset, |
| 118 |
imageSize, |
| 119 |
catTabCategoryType, |
| 120 |
}; |
| 121 |
|
| 122 |
const queryData = { |
| 123 |
taxonomies: filteredTaxonomies, |
| 124 |
includeOnlyPost: filteredIncludedPost, |
| 125 |
paginationEnable, |
| 126 |
catTabCategoryType, |
| 127 |
// imageSize, |
| 128 |
...dependencies, |
| 129 |
}; |
| 130 |
|
| 131 |
const fetchPosts = async () => { |
| 132 |
try { |
| 133 |
const data = new FormData(); |
| 134 |
data.append("action", "sp_smart_post_block_post_query"); |
| 135 |
data.append("nonce", sp_smart_post_block_localize.ajaxNonce); |
| 136 |
data.append("queryData", jsonStringify(queryData)); |
| 137 |
const postQueryData = await queryFn(data); |
| 138 |
const { posts, post_count, posts_status } = postQueryData; |
| 139 |
|
| 140 |
setAllData({ |
| 141 |
posts, |
| 142 |
postCount: post_count, |
| 143 |
postsStatus: posts_status, |
| 144 |
}); |
| 145 |
} catch (error) { |
| 146 |
console.error("Error fetching posts:", error.message); |
| 147 |
} |
| 148 |
}; |
| 149 |
|
| 150 |
useEffect(() => { |
| 151 |
fetchPosts(); |
| 152 |
}, [ |
| 153 |
jsonStringify({ |
| 154 |
taxonomies, |
| 155 |
includeOnlyPost, |
| 156 |
...dependencies, |
| 157 |
}), |
| 158 |
]); |
| 159 |
return { ...allData, queryData }; |
| 160 |
}; |
| 161 |
|
| 162 |
export default useApiData; |
| 163 |
|