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