| 1 |
import React from "react"; |
| 2 |
import { useBlockProps } from "@wordpress/block-editor"; |
| 3 |
import { buildQueryString } from "@wordpress/url"; |
| 4 |
import { Spinner } from "@wordpress/components"; |
| 5 |
import { useMemo, useEffect } from "@wordpress/element"; |
| 6 |
import { safeHTML } from '@wordpress/dom'; |
| 7 |
|
| 8 |
import Inspector from "./inspector"; |
| 9 |
import { NotFound, SingleCategory } from "../../../components"; |
| 10 |
import { |
| 11 |
usePreviewDeviceType, |
| 12 |
useDuplicateBlockIdFix, |
| 13 |
useStyleSheets, |
| 14 |
useFetch, |
| 15 |
} from "../../../hooks"; |
| 16 |
import cssProperties from "./cssProperties"; |
| 17 |
import masonryGrider from "./helper"; |
| 18 |
|
| 19 |
const BLOCK_PREFIX = "betterdocs-categorygrid"; |
| 20 |
const Edit = ({ attributes, setAttributes, isSelected, clientId }) => { |
| 21 |
const { |
| 22 |
blockId, |
| 23 |
selectKB, |
| 24 |
includeCategories, |
| 25 |
excludeCategories, |
| 26 |
gridPerPage, |
| 27 |
orderBy, |
| 28 |
order, |
| 29 |
enableNestedSubcategory, |
| 30 |
layoutMode, |
| 31 |
layout, |
| 32 |
categoryTitleLink, |
| 33 |
} = attributes; |
| 34 |
usePreviewDeviceType(setAttributes); |
| 35 |
useDuplicateBlockIdFix(BLOCK_PREFIX, blockId, clientId, setAttributes); |
| 36 |
const { properties, htmlAttributes, styleTag } = useStyleSheets({ |
| 37 |
properties: cssProperties({ blockId, layoutMode, layout }), |
| 38 |
setAttributes, |
| 39 |
attributes, |
| 40 |
}); |
| 41 |
|
| 42 |
const categoryApiURL = useMemo(() => { |
| 43 |
let query = { |
| 44 |
per_page: gridPerPage ? gridPerPage : 9, |
| 45 |
orderby: orderBy, |
| 46 |
order: order, |
| 47 |
hide_empty: 1, |
| 48 |
}; |
| 49 |
|
| 50 |
let knowledge_base = []; |
| 51 |
if (selectKB && selectKB !== "[]") { |
| 52 |
// Parse the JSON string to convert it into an object |
| 53 |
const kbObject = JSON.parse(selectKB); |
| 54 |
|
| 55 |
// If the parsed value is an object and has a 'value' property, extract it |
| 56 |
if (typeof kbObject === "object" && kbObject.value !== undefined) { |
| 57 |
knowledge_base.push(kbObject.value); |
| 58 |
} else if (Array.isArray(kbObject)) { |
| 59 |
knowledge_base = kbObject.map((kb) => kb.value); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
let include = |
| 64 |
includeCategories && includeCategories !== "[]" |
| 65 |
? JSON.parse(includeCategories).map( |
| 66 |
(category) => category.value, |
| 67 |
) |
| 68 |
: []; |
| 69 |
|
| 70 |
let exclude = |
| 71 |
excludeCategories && excludeCategories !== "[]" |
| 72 |
? JSON.parse(excludeCategories).map( |
| 73 |
(category) => category.value, |
| 74 |
) |
| 75 |
: []; |
| 76 |
|
| 77 |
include = include.filter((item) => !exclude.includes(item)); |
| 78 |
|
| 79 |
if (include.length > 0) { |
| 80 |
query["include"] = include; |
| 81 |
} |
| 82 |
if (exclude.length > 0) { |
| 83 |
query["exclude"] = exclude; |
| 84 |
} |
| 85 |
|
| 86 |
if (enableNestedSubcategory) { |
| 87 |
query["parent"] = 0; |
| 88 |
query["nested_subcategory"] = true; |
| 89 |
} |
| 90 |
|
| 91 |
if (knowledge_base.length > 0) { |
| 92 |
query["filter[knowledgebase]"] = knowledge_base.join(","); |
| 93 |
} |
| 94 |
|
| 95 |
return `/wp/v2/doc_category/?${buildQueryString(query)}`; |
| 96 |
}, [ |
| 97 |
selectKB, |
| 98 |
includeCategories, |
| 99 |
excludeCategories, |
| 100 |
gridPerPage, |
| 101 |
orderBy, |
| 102 |
order, |
| 103 |
enableNestedSubcategory, |
| 104 |
]); |
| 105 |
|
| 106 |
const { |
| 107 |
data: categories, |
| 108 |
isLoading: isCategoryLoading, |
| 109 |
error: categoryError, |
| 110 |
} = useFetch(categoryApiURL); |
| 111 |
|
| 112 |
const blockProps = useBlockProps({ |
| 113 |
className: `bd-guten-block-main-parent-wrapper`, |
| 114 |
}); |
| 115 |
|
| 116 |
useEffect(() => { |
| 117 |
masonryGrider(); |
| 118 |
}, [layoutMode, categories]); |
| 119 |
|
| 120 |
const clearMasonryGrider = setTimeout(() => { |
| 121 |
masonryGrider(); |
| 122 |
clearTimeout(clearMasonryGrider); |
| 123 |
}, 0); |
| 124 |
|
| 125 |
useEffect(() => { |
| 126 |
if( layout == 'default' ) { |
| 127 |
setAttributes({prefix:''}); |
| 128 |
setAttributes({suffix:''}); |
| 129 |
setAttributes({suffixSingular: ''}); |
| 130 |
} |
| 131 |
}, [layout]); |
| 132 |
return ( |
| 133 |
<> |
| 134 |
{isSelected && ( |
| 135 |
<Inspector |
| 136 |
attributes={attributes} |
| 137 |
setAttributes={setAttributes} |
| 138 |
/> |
| 139 |
)} |
| 140 |
<div {...blockProps}> |
| 141 |
{styleTag()} |
| 142 |
<div |
| 143 |
className={`betterdocs-category-grid-wrapper betterdocs-blocks ${safeHTML( |
| 144 |
blockId, |
| 145 |
)} `} |
| 146 |
> |
| 147 |
<div |
| 148 |
className={`betterdocs-category-grid-inner-wrapper layout-flex ${layoutMode} ${layout} ${properties?.col?.classNames}`} |
| 149 |
{...htmlAttributes} |
| 150 |
> |
| 151 |
{ |
| 152 |
/** |
| 153 |
* If item is not loaded yet |
| 154 |
*/ |
| 155 |
isCategoryLoading && <Spinner /> |
| 156 |
} |
| 157 |
{ |
| 158 |
/** |
| 159 |
* If no item is found. |
| 160 |
*/ |
| 161 |
!isCategoryLoading && categories?.length == 0 && ( |
| 162 |
<NotFound /> |
| 163 |
) |
| 164 |
} |
| 165 |
{ |
| 166 |
/** |
| 167 |
* If items are loaded. |
| 168 |
*/ |
| 169 |
!isCategoryLoading && |
| 170 |
categories?.length > 0 && |
| 171 |
categories.map((category) => ( |
| 172 |
<SingleCategory |
| 173 |
key={category.id} |
| 174 |
widgetType="category-grid" |
| 175 |
attributes={attributes} |
| 176 |
categories={categories} |
| 177 |
titleLink={categoryTitleLink} |
| 178 |
{...category} |
| 179 |
/> |
| 180 |
)) |
| 181 |
} |
| 182 |
</div> |
| 183 |
</div> |
| 184 |
</div> |
| 185 |
</> |
| 186 |
); |
| 187 |
}; |
| 188 |
|
| 189 |
export default Edit; |
| 190 |
|