| 1 |
import React from 'react' |
| 2 |
import { useMemo } from '@wordpress/element'; |
| 3 |
import { buildQueryString } from '@wordpress/url'; |
| 4 |
import { decodeEntities } from '@wordpress/html-entities'; |
| 5 |
|
| 6 |
import { useFetch } from '../../hooks'; |
| 7 |
import ListIcon from './ListIcon'; |
| 8 |
import NestedCategories from '../NestedCategories'; |
| 9 |
|
| 10 |
import { docIcon } from '../../blocks/sidebar/icons/Icons'; |
| 11 |
|
| 12 |
|
| 13 |
const DocList = ({ className = 'betterdocs-articles-list', attributes, categories, ...props }) => { |
| 14 |
const { postsPerPage, postsOrderBy, postsOrder, listIcon, enableNestedSubcategory, listIconImageUrl='' } = attributes; |
| 15 |
|
| 16 |
const docsByCatIDURL = useMemo(() => { |
| 17 |
let query = { |
| 18 |
per_page: postsPerPage ? postsPerPage : 0, |
| 19 |
orderby: postsOrderBy, |
| 20 |
order: postsOrder, |
| 21 |
doc_category: props.id |
| 22 |
}; |
| 23 |
|
| 24 |
const baseURL = query.orderby === "betterdocs_order" ? '/betterdocs/v1/order_docs' : '/wp/v2/docs'; |
| 25 |
|
| 26 |
return `${baseURL}?${buildQueryString(query)}`; |
| 27 |
}, [categories, postsPerPage, postsOrderBy, postsOrder, props.id]); |
| 28 |
|
| 29 |
const { data: docs, isLoading: isDocsLoading, error: docsError } = useFetch(docsByCatIDURL); |
| 30 |
return ( |
| 31 |
<ul className={className}> |
| 32 |
{ |
| 33 |
!isDocsLoading && |
| 34 |
docs.length > 0 && docs.map(doc => ( |
| 35 |
<li key={doc.id}> |
| 36 |
{props?.showDocIcon && docIcon()} |
| 37 |
{listIcon?.length == 0 && listIconImageUrl?.length == 0 && betterDocsBlocksHelper?.docsIcon?.length > 0 && <img src={betterDocsBlocksHelper?.docsIcon}/>} |
| 38 |
{(listIcon?.length > 0 && listIconImageUrl?.length == 0) && <ListIcon icon={listIcon} />} |
| 39 |
{listIconImageUrl?.length > 0 && <img src={listIconImageUrl}/>} |
| 40 |
<a href={void 0}> |
| 41 |
{decodeEntities(doc.title.rendered)} |
| 42 |
</a> |
| 43 |
</li> |
| 44 |
)) |
| 45 |
} |
| 46 |
|
| 47 |
{ |
| 48 |
Boolean(enableNestedSubcategory) && |
| 49 |
<NestedCategories |
| 50 |
{...props} |
| 51 |
attributes={attributes} |
| 52 |
/> |
| 53 |
} |
| 54 |
</ul> |
| 55 |
) |
| 56 |
} |
| 57 |
|
| 58 |
export default DocList |
| 59 |
|