| 1 |
import { useEffect } from "@wordpress/element"; |
| 2 |
import { useSelect } from "@wordpress/data"; |
| 3 |
import {InnerBlocks, store as blockEditorStore } from "@wordpress/block-editor"; |
| 4 |
import { useAddChildBlock, jsonStringify } from "./helpFn"; |
| 5 |
import useApiData from "../../hooks/useApiData"; |
| 6 |
import preloader from "../../../public/assets/img/preloader.svg"; |
| 7 |
import { useSelectChildBlock } from "../../hooks/usePaginationSelect"; |
| 8 |
import { googleFonts } from "../../controls/controls"; |
| 9 |
import useBlockLocation from "../../hooks/useBlockLocation"; |
| 10 |
|
| 11 |
export const EditorWrapper = ({ children, setPosts, setAttributes, blockType = "", customClass = "" }) => { |
| 12 |
const { attributes } = children?.props; |
| 13 |
|
| 14 |
const { posts, postsStatus, postCount, queryData } = useApiData(attributes); |
| 15 |
|
| 16 |
const { |
| 17 |
uniqueId, |
| 18 |
additionalCssClass, |
| 19 |
equalHeightEnable, |
| 20 |
noResultFoundResult, |
| 21 |
blockName, |
| 22 |
paginationEnable, |
| 23 |
paginationPosition, |
| 24 |
preloaderEnable, |
| 25 |
liveFilterEnable, |
| 26 |
clientId, |
| 27 |
postLimit, |
| 28 |
titleTypography, |
| 29 |
largeItemTitleTypography, |
| 30 |
catTabCategoryTypography, |
| 31 |
metaTypography, |
| 32 |
excerptTypography, |
| 33 |
readMoreButtonTypography, |
| 34 |
paginationTypography, |
| 35 |
headingTypography, |
| 36 |
tickerTitleTypography, |
| 37 |
tickerDateTypography, |
| 38 |
paginationTypeParent, |
| 39 |
postBadgesTypography, |
| 40 |
} = attributes; |
| 41 |
|
| 42 |
const liveFilterRelation = useSelect((select) => { |
| 43 |
|
| 44 |
const parentId = select(blockEditorStore).getBlockRootClientId(clientId); |
| 45 |
|
| 46 |
if (!parentId) return null; |
| 47 |
|
| 48 |
const parentBlock = select(blockEditorStore).getBlock(parentId); |
| 49 |
|
| 50 |
return parentBlock?.attributes?.multipleFilterRelation || null; |
| 51 |
|
| 52 |
}, []); |
| 53 |
|
| 54 |
if (clientId) { |
| 55 |
// Select Pagination Child Blocks |
| 56 |
useSelectChildBlock(clientId, paginationEnable); |
| 57 |
useAddChildBlock(liveFilterEnable, paginationEnable, clientId); |
| 58 |
|
| 59 |
const _blockLocator = useBlockLocation(clientId); |
| 60 |
|
| 61 |
useEffect(() => { |
| 62 |
// Remove null or empty values. |
| 63 |
const cleanLocation = Object.fromEntries( |
| 64 |
Object.entries(_blockLocator).filter(([, value]) => value !== null && value !== "") |
| 65 |
); |
| 66 |
|
| 67 |
setAttributes({ |
| 68 |
blockLocation: JSON.stringify(cleanLocation), |
| 69 |
multipleFilterRelation: liveFilterRelation |
| 70 |
}); |
| 71 |
|
| 72 |
}, [_blockLocator, liveFilterRelation]); |
| 73 |
} |
| 74 |
|
| 75 |
const googleFontLists = [ |
| 76 |
titleTypography, |
| 77 |
largeItemTitleTypography, |
| 78 |
catTabCategoryTypography, |
| 79 |
metaTypography, |
| 80 |
excerptTypography, |
| 81 |
readMoreButtonTypography, |
| 82 |
paginationTypography, |
| 83 |
headingTypography, |
| 84 |
tickerTitleTypography, |
| 85 |
tickerDateTypography, |
| 86 |
postBadgesTypography, |
| 87 |
]; |
| 88 |
|
| 89 |
useEffect(() => { |
| 90 |
setAttributes({ |
| 91 |
fontLists: jsonStringify(googleFonts(googleFontLists, "frontend")), |
| 92 |
fontListsEditPage: googleFonts(googleFontLists), |
| 93 |
}); |
| 94 |
}, googleFontLists); |
| 95 |
|
| 96 |
useEffect(() => { |
| 97 |
setPosts(posts); |
| 98 |
}, [posts]); |
| 99 |
|
| 100 |
// improvement: update queryData on save rather saving on every render |
| 101 |
useEffect(() => { |
| 102 |
if ( attributes?.postQuery !== JSON.stringify( queryData ) ) { |
| 103 |
setAttributes({ |
| 104 |
postQuery: jsonStringify(queryData), |
| 105 |
}); |
| 106 |
} |
| 107 |
}, [queryData]); |
| 108 |
|
| 109 |
if (posts?.length < 1) { |
| 110 |
return postsStatus !== false && preloaderEnable ? ( |
| 111 |
<div className="sp-smart-post-preloader"> |
| 112 |
<img src={preloader} alt="smart post preloader" /> |
| 113 |
</div> |
| 114 |
) : ( |
| 115 |
<h4>{noResultFoundResult ? noResultFoundResult : "No post found"}</h4> |
| 116 |
); |
| 117 |
} |
| 118 |
return ( |
| 119 |
<div |
| 120 |
id={uniqueId} |
| 121 |
className={`sp-smart-post-wrapper ${additionalCssClass} sp-smart-${blockName}-editor-page sp-smart-${blockName} ${ |
| 122 |
equalHeightEnable ? "sp-smart-post-show-equal-height" : "" |
| 123 |
} ${customClass}`} |
| 124 |
> |
| 125 |
{uniqueId && ( |
| 126 |
<> |
| 127 |
<div |
| 128 |
className={`sp-smart-post-block-with-pagination pagination-${paginationTypeParent === "navigation" ? paginationPosition : "bottom"}`} |
| 129 |
> |
| 130 |
{children} |
| 131 |
{blockType !== "builder-block" && paginationEnable && postCount > postLimit && ( |
| 132 |
<InnerBlocks |
| 133 |
allowedBlocks={["sp-smart-post-show/pagination"]} |
| 134 |
template={[["sp-smart-post-show/pagination"]]} |
| 135 |
templateLock="all" |
| 136 |
__experimentalLayout={{ |
| 137 |
type: "default", |
| 138 |
}} |
| 139 |
/> |
| 140 |
)} |
| 141 |
</div> |
| 142 |
</> |
| 143 |
)} |
| 144 |
</div> |
| 145 |
); |
| 146 |
}; |
| 147 |
|