| 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 { Body, NotFound, SingleCategory } from "../../../components"; |
| 10 |
import { |
| 11 |
usePreviewDeviceType, |
| 12 |
useDuplicateBlockIdFix, |
| 13 |
useStyleSheets, |
| 14 |
useFetch, |
| 15 |
} from "../../../hooks"; |
| 16 |
import cssProperties from "./cssProperties"; |
| 17 |
import { FAQList } from "../../../components/partials/"; |
| 18 |
import { htmlToElem } from "../../../helpers/helper"; |
| 19 |
import FAQTabLayout from "../../../components/partials/FAQTabLayout"; |
| 20 |
|
| 21 |
const BLOCK_PREFIX = "betterdocs-faq"; |
| 22 |
const Edit = (props) => { |
| 23 |
const { attributes, setAttributes, isSelected, clientId } = props; |
| 24 |
const { |
| 25 |
blockId, |
| 26 |
faqLayout, |
| 27 |
faqSectionText, |
| 28 |
includeFaqGroup, |
| 29 |
excludeFaqGroup, |
| 30 |
faqGroupTitleColor, |
| 31 |
faqGroupTitleHoverColor, |
| 32 |
faqListColor, |
| 33 |
faqContentColor, |
| 34 |
faqIconColor, |
| 35 |
faqGroupTitleTypography, |
| 36 |
faqTaxonomy, |
| 37 |
handbookPerPage, |
| 38 |
orderBy, |
| 39 |
order, |
| 40 |
enableNestedSubfaq, |
| 41 |
layoutMode, |
| 42 |
layout, |
| 43 |
} = attributes; |
| 44 |
``; |
| 45 |
usePreviewDeviceType(setAttributes); |
| 46 |
useDuplicateBlockIdFix(BLOCK_PREFIX, blockId, clientId, setAttributes); |
| 47 |
const { properties, htmlAttributes, styleTag } = useStyleSheets({ |
| 48 |
properties: cssProperties({ blockId, faqLayout }), |
| 49 |
setAttributes, |
| 50 |
attributes, |
| 51 |
}); |
| 52 |
|
| 53 |
const isProductAssignments = faqTaxonomy === 'product_assignments'; |
| 54 |
|
| 55 |
// The REST slug used to fetch terms for the editor preview. |
| 56 |
// product_assignments has no taxonomy of its own, so preview using |
| 57 |
// betterdocs_product_faq_category (same terms the frontend resolver draws from). |
| 58 |
const previewTaxonomySlug = isProductAssignments |
| 59 |
? 'betterdocs_product_faq_category' |
| 60 |
: (faqTaxonomy || 'betterdocs_faq_category'); |
| 61 |
|
| 62 |
const faqApiURL = useMemo(() => { |
| 63 |
let query = { |
| 64 |
per_page: handbookPerPage ? handbookPerPage : 9, |
| 65 |
orderby: orderBy, |
| 66 |
order: order, |
| 67 |
hide_empty: 1, |
| 68 |
}; |
| 69 |
|
| 70 |
// Include/exclude only applies in taxonomy modes, not product_assignments. |
| 71 |
if (!isProductAssignments) { |
| 72 |
let include = |
| 73 |
includeFaqGroup && includeFaqGroup !== "[]" |
| 74 |
? JSON.parse(includeFaqGroup).map((faq) => faq.value) |
| 75 |
: []; |
| 76 |
|
| 77 |
let exclude = |
| 78 |
excludeFaqGroup && excludeFaqGroup !== "[]" |
| 79 |
? JSON.parse(excludeFaqGroup).map((faq) => faq.value) |
| 80 |
: []; |
| 81 |
|
| 82 |
include = include.filter((item) => !exclude.includes(item)); |
| 83 |
|
| 84 |
if (include.length > 0) { |
| 85 |
query["include"] = include; |
| 86 |
} |
| 87 |
if (exclude.length > 0) { |
| 88 |
query["exclude"] = exclude; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
return `/wp/v2/${previewTaxonomySlug}/?${buildQueryString(query)}`; |
| 93 |
}, [isProductAssignments, previewTaxonomySlug, includeFaqGroup, excludeFaqGroup, handbookPerPage, orderBy, order]); |
| 94 |
|
| 95 |
const { |
| 96 |
data: faqs, |
| 97 |
isLoading: isFaqLoading, |
| 98 |
error: faqError, |
| 99 |
} = useFetch(faqApiURL); |
| 100 |
|
| 101 |
const blockProps = useBlockProps({ |
| 102 |
className: `bd-guten-block-main-parent-wrapper`, |
| 103 |
}); |
| 104 |
|
| 105 |
let leyout = "layout-modern"; |
| 106 |
if (faqLayout === "layout-2") { |
| 107 |
leyout = "layout-classic"; |
| 108 |
} |
| 109 |
|
| 110 |
return ( |
| 111 |
<> |
| 112 |
{isSelected && ( |
| 113 |
<Inspector |
| 114 |
attributes={attributes} |
| 115 |
setAttributes={setAttributes} |
| 116 |
/> |
| 117 |
)} |
| 118 |
<div {...blockProps}> |
| 119 |
{styleTag()} |
| 120 |
{ |
| 121 |
<div |
| 122 |
className={`${safeHTML( |
| 123 |
blockId, |
| 124 |
)} betterdocs-faq-wrapper ${leyout} betterdocs-faq-${faqLayout} betterdocs-shortcode`} |
| 125 |
> |
| 126 |
<h2 |
| 127 |
className={`betterdocs-faq-${faqLayout} betterdocs-faq-section-title`} |
| 128 |
> |
| 129 |
{faqSectionText}{" "} |
| 130 |
</h2> |
| 131 |
|
| 132 |
<div className="betterdocs-faq-inner-wrapper"> |
| 133 |
{ |
| 134 |
isFaqLoading && <Spinner /> |
| 135 |
} |
| 136 |
{ |
| 137 |
!isFaqLoading && faqs?.length == 0 && ( |
| 138 |
<NotFound /> |
| 139 |
) |
| 140 |
} |
| 141 |
{ |
| 142 |
( !isFaqLoading && faqLayout != 'layout-4') && |
| 143 |
faqs?.length > 0 && |
| 144 |
faqs.map((faq) => ( |
| 145 |
<> |
| 146 |
<div className="betterdocs-faq-title"> |
| 147 |
<h2>{htmlToElem(faq.name)}</h2> |
| 148 |
</div> |
| 149 |
<FAQList |
| 150 |
key={faq.id} |
| 151 |
widgetType={"batterdocs-faq"} |
| 152 |
attributes={attributes} |
| 153 |
faqs={faqs} |
| 154 |
{...faq} |
| 155 |
/> |
| 156 |
</> |
| 157 |
)) |
| 158 |
} |
| 159 |
{ |
| 160 |
(faqLayout == 'layout-4' && !isFaqLoading) && |
| 161 |
<FAQTabLayout faqCategories={faqs} attributes={attributes}/> |
| 162 |
} |
| 163 |
</div> |
| 164 |
</div> |
| 165 |
} |
| 166 |
</div> |
| 167 |
</> |
| 168 |
); |
| 169 |
}; |
| 170 |
|
| 171 |
export default Edit; |
| 172 |
|