@@ -5,11 +5,12 @@ import { __, sprintf } from '@wordpress/i18n';
55import QuickEditModal from './QuickEditModal' ;
66import { useSortable } from '@dnd-kit/sortable' ;
77import extractedTitle from '../../utils/extractedTitle' ;
8- import { Fragment , useState } from '@wordpress/element' ;
8+ import { Fragment , useState , useEffect } from '@wordpress/element' ;
99import { useSelect } from '@wordpress/data' ;
1010import docStore from '../../data/docs' ;
1111import ArticleChildrens from './ArticleChildrens' ;
1212import AddArticleModal from '../AddArticleModal' ;
13+ import DraggableDocs from '../DraggableDocs' ;
1314
1415const SectionArticles = ( { article, articles, isAdmin, section, sections, searchValue, setShowArticles, isAllowComments } ) => {
1516 const { attributes, listeners, setNodeRef, transform, transition, isDragging } =
@@ -33,13 +34,53 @@ const SectionArticles = ( { article, articles, isAdmin, section, sections, searc
3334
3435 const articleChildrens = useSelect (
3536 ( select ) => select ( docStore ) . getArticleChildrens ( parseInt ( article ?. id ) ) ,
37+ [ article ?. id ]
38+ ) ;
39+
40+ const [ childrenItems , setChildrenItems ] = useState ( articleChildrens || [ ] ) ;
41+ const [ needSortingStatusLocal , setNeedSortingStatusLocal ] = useState ( false ) ;
42+
43+ const sortableStatus = useSelect (
44+ ( select ) => select ( docStore ) . getSortingStatus ( ) ,
3645 [ ]
3746 ) ;
3847
39- const filteredArticleChildrens = articleChildrens ?. filter (
40- ( children ) => children ?. title ?. rendered ?. toLowerCase ( ) . includes ( searchValue . toLowerCase ( ) )
48+ const needSortableStatus = useSelect (
49+ ( select ) => select ( docStore ) . getNeedSortingStatus ( ) ,
50+ [ ]
4151 ) ;
4252
53+ // Update children items when articleChildren changes
54+ useEffect ( ( ) => {
55+ if ( articleChildrens ) {
56+ setChildrenItems ( articleChildrens ) ;
57+ }
58+ } , [ articleChildrens , article ?. id ] ) ;
59+
60+ // Sync local sorting status with global status
61+ useEffect ( ( ) => {
62+ setNeedSortingStatusLocal ( needSortableStatus ) ;
63+ } , [ needSortableStatus ] ) ;
64+
65+ // Handle sorting when needed
66+ useEffect ( ( ) => {
67+ if ( needSortingStatusLocal && childrenItems . length > 0 ) {
68+ const { dispatch } = wp . data ;
69+ dispatch ( docStore )
70+ . updateSortingStatus ( { sortable_status : sortableStatus , documentations : childrenItems } )
71+ . then ( ( result ) => setNeedSortingStatusLocal ( result ?. sorting ) )
72+ . catch ( ( error ) => {
73+ console . error ( 'Failed to update sorting status:' , error ) ;
74+ } ) ;
75+ }
76+ } , [ needSortingStatusLocal , childrenItems , sortableStatus ] ) ;
77+
78+ const filteredArticleChildrens = searchValue
79+ ? childrenItems ?. filter (
80+ ( children ) => children ?. title ?. rendered ?. toLowerCase ( ) . includes ( searchValue . toLowerCase ( ) )
81+ )
82+ : childrenItems ;
83+
4384 const isAdminRestrictedArticle = wp ?. hooks ?. applyFilters (
4485 'wedocs_check_is_admin_restricted_article' ,
4586 false ,
@@ -55,13 +96,17 @@ const SectionArticles = ( { article, articles, isAdmin, section, sections, searc
5596 { ...attributes }
5697 ref = { setNodeRef }
5798 >
58- < div className = { `pr-3.5 py-0.5 cursor-grab` } >
99+ < div
100+ className = { `pr-3.5 py-0.5 cursor-grab hover:bg-gray-100 rounded transition-colors` }
101+ { ...listeners }
102+ style = { { touchAction : 'none' } }
103+ >
59104 < svg
60105 width = "20"
61106 height = "21"
62107 fill = "none"
63- { ...listeners }
64108 xmlns = "http://www.w3.org/2000/svg"
109+ className = "pointer-events-none"
65110 >
66111 < path
67112 fillRule = "evenodd"
@@ -133,7 +178,7 @@ const SectionArticles = ( { article, articles, isAdmin, section, sections, searc
133178 className = 'tooltip cursor-pointer flex items-center justify-center w-3.5 h-3.5'
134179 data-tip = { __ ( 'Create' , 'wedocs' ) }
135180 >
136- < span className = "flex items-center dashicons dashicons-plus-alt2 hidden group-hover:inline-flex text-2xl font-medium text-[#d1d5db] hover:text-indigo-700" > </ span >
181+ < span className = "items-center dashicons dashicons-plus-alt2 hidden group-hover:inline-flex text-2xl font-medium text-[#d1d5db] hover:text-indigo-700" > </ span >
137182 </ div >
138183 </ AddArticleModal >
139184
@@ -232,7 +277,7 @@ const SectionArticles = ( { article, articles, isAdmin, section, sections, searc
232277 </ div >
233278 </ div >
234279 < div className = "flex items-center gap-5 flex-shrink-0 mt-4 sm:mt-0 sm:ml-5" >
235- < div className = "flex items-center gap-1 " >
280+ < div className = "flex items-center gap-3 " >
236281 { /* Render admin restriction icon. */ }
237282 { wp ?. hooks ?. applyFilters (
238283 'wedocs_article_privacy_action' ,
@@ -297,21 +342,48 @@ const SectionArticles = ( { article, articles, isAdmin, section, sections, searc
297342 </ div >
298343 </ div >
299344
345+ { /* Level 3 children - no DraggableDocs wrapper (nested context doesn't work) */ }
300346 { ! isDragging && ( filteredArticleChildrens ?. length > 0 ) && (
301347 < div
302- style = { style }
303348 className = { `my-1 article-children pl-4 sm:pl-16 bg-white` }
304349 >
305- { filteredArticleChildrens ?. map ( ( childrenDoc ) => (
306- < ArticleChildrens
307- section = { article }
308- sections = { articles }
309- key = { childrenDoc . id }
310- article = { childrenDoc }
311- setShowArticles = { setShowArticles }
312- isAllowComments = { isAllowComments }
313- />
314- ) ) }
350+ { filteredArticleChildrens ?. map ( ( childrenDoc ) => {
351+ // Allow pro version to override the article children component
352+ const ChildComponent = wp ?. hooks ?. applyFilters (
353+ 'wedocs_article_children_component' ,
354+ ArticleChildrens ,
355+ {
356+ section : article ,
357+ sections : articles ,
358+ article : childrenDoc ,
359+ setShowArticles : setShowArticles ,
360+ isAllowComments : isAllowComments ,
361+ searchValue : searchValue ,
362+ DocActions : DocActions ,
363+ QuickEditModal : QuickEditModal ,
364+ AddArticleModal : AddArticleModal ,
365+ DraggableDocs : DraggableDocs ,
366+ }
367+ ) ;
368+
369+ return (
370+ < ChildComponent
371+ section = { article }
372+ sections = { articles }
373+ key = { childrenDoc . id }
374+ article = { childrenDoc }
375+ setShowArticles = { setShowArticles }
376+ isAllowComments = { isAllowComments }
377+ searchValue = { searchValue }
378+ DocActions = { DocActions }
379+ QuickEditModal = { QuickEditModal }
380+ AddArticleModal = { AddArticleModal }
381+ DraggableDocs = { DraggableDocs }
382+ depth = { 2 }
383+ maxDepth = { 7 }
384+ />
385+ ) ;
386+ } ) }
315387 </ div >
316388 ) }
317389 </ Fragment >
0 commit comments