@@ -21,92 +21,18 @@ import { headers } from '../utils/collection-header'
2121 */
2222const Collections = ( { updateCredentials } ) => {
2323 const [ collectionsList , setCollectionsList ] = useState ( [ ] ) // All Collections
24- const [ upToDateCollections , setUpToDateCollection ] = useState ( false ) // Boolean that informs if collections have been updated.
2524 const [ needReload , setNeedReload ] = useState ( false ) // Boolean to inform that reload is requested.
26- const [ collectionInWaitMode , setCollectionInWaitMode ] = useState ( [ ] ) // Collections that are waiting for their indexation to complete .
27- const [ collectionTaskUids , setCollectionTaskUids ] = useState ( { } ) // List of collection's enqueued task uids.
25+ const [ realTimeReports , setRealTimeReports ] = useState ( false ) // List of collection's enqueued task uids .
26+ const [ refetchIndex , setRefetchIndex ] = useState ( true )
2827
29- // Trigger a task uids fetcher to find enqueued task of the indexed collections.
30- useEffect ( ( ) => {
31- findTaskUids ( )
32- } , [ ] )
28+ const refetchCollection = ( ) =>
29+ setRefetchIndex ( prevRefetchIndex => ! prevRefetchIndex )
3330
3431 // Adds a listener that informs if collections have been updated.
3532 useEffect ( ( ) => {
36- setUpToDateCollection ( false )
33+ refetchCollection ( )
3734 } , [ updateCredentials ] )
3835
39- // Adds a listener that updates collections informations on updates
40- useEffect ( ( ) => {
41- if ( ! upToDateCollections ) fetchCollections ( )
42- } , [ upToDateCollections , updateCredentials ] )
43-
44- // Trigger a watch if a collection has enqueued task uid's.
45- useEffect ( ( ) => {
46- for ( const collection in collectionTaskUids ) {
47- if ( collectionTaskUids [ collection ] . length > 0 ) {
48- watchTasks ( { collection } )
49- }
50- }
51- } , [ collectionTaskUids ] )
52-
53- /**
54- * Find all enqueued task uid's of the indexed collections.
55- * It is triggered on load.
56- */
57- const findTaskUids = async ( ) => {
58- const response = await request ( `/${ pluginId } /collection/tasks` , {
59- method : 'GET' ,
60- } )
61-
62- if ( response . error ) errorNotifications ( response )
63- setCollectionTaskUids ( response . taskUids )
64- }
65-
66- /**
67- * Watches a collection (if not already)
68- * For a maximum of 5 enqueued tasks in Meilisearch.
69- *
70- * @param {string } collection - Collection name.
71- */
72- const watchTasks = async ( { collection } ) => {
73- // If collection has pending tasks
74- const taskUids = collectionTaskUids [ collection ]
75-
76- if ( ! collectionInWaitMode . includes ( collection ) && taskUids ?. length > 0 ) {
77- addIndexedStatus
78- setCollectionInWaitMode ( prev => [ ...prev , collection ] )
79-
80- const taskUidsIdsChunk = taskUids . splice ( 0 , 1 )
81- const response = await request (
82- `/${ pluginId } /collection/${ collection } /tasks/batch` ,
83- {
84- method : 'POST' ,
85- body : { taskUids : taskUidsIdsChunk } ,
86- }
87- )
88-
89- if ( response . error ) errorNotifications ( response )
90-
91- const { tasksStatus } = response
92-
93- tasksStatus . map ( task => {
94- if ( task . status === 'failed' ) {
95- task . error . message = `Some documents could not be added: \n${ task . error . message } `
96- errorNotifications ( task . error )
97- }
98- } )
99-
100- setCollectionInWaitMode ( prev => prev . filter ( col => col !== collection ) )
101- setCollectionTaskUids ( prev => ( {
102- ...prev ,
103- [ collection ] : taskUids ,
104- } ) )
105-
106- setUpToDateCollection ( false ) // Ask for collections to be updated.
107- }
108- }
109-
11036 /**
11137 * Add a collection to Meilisearch
11238 *
@@ -122,14 +48,7 @@ const Collections = ({ updateCredentials }) => {
12248
12349 createResponseNotification ( response , `${ collection } is created!` )
12450
125- if ( ! response . error ) {
126- setCollectionTaskUids ( prev => ( {
127- ...prev ,
128- [ collection ] : response . taskUids ,
129- } ) )
130- }
131-
132- setUpToDateCollection ( false ) // Ask for collections to be updated.
51+ refetchCollection ( )
13352 }
13453
13554 /**
@@ -147,14 +66,7 @@ const Collections = ({ updateCredentials }) => {
14766
14867 createResponseNotification ( response , `${ collection } update started!` )
14968
150- if ( ! response . error ) {
151- setCollectionTaskUids ( prev => ( {
152- ...prev ,
153- [ collection ] : response . taskUids ,
154- } ) )
155- }
156-
157- setUpToDateCollection ( false ) // Ask for collections to be updated.
69+ refetchCollection ( )
15870 }
15971
16072 /**
@@ -172,7 +84,7 @@ const Collections = ({ updateCredentials }) => {
17284 `${ collection } collection is removed from Meilisearch!`
17385 )
17486
175- setUpToDateCollection ( false ) // Ask for collections to be updated.
87+ refetchCollection ( )
17688 }
17789
17890 /**
@@ -200,12 +112,9 @@ const Collections = ({ updateCredentials }) => {
200112
201113 if ( error ) errorNotifications ( res )
202114 else {
203- // Start watching collections that have pending tasks
204- collections . map ( col => {
205- if ( col . isIndexing ) {
206- watchTasks ( { collection : col . collection } )
207- }
208- } )
115+ const isIndexing = collections . find ( col => col . isIndexing === true )
116+
117+ setRealTimeReports ( isIndexing )
209118
210119 // Transform collections information to verbose string.
211120 const renderedCols = collections . map ( col => transformCollections ( col ) )
@@ -217,10 +126,26 @@ const Collections = ({ updateCredentials }) => {
217126
218127 setNeedReload ( reloading ) // A reload is required for a collection to be listened or de-listened
219128 setCollectionsList ( renderedCols ) // Store all `Strapi collections
220- setUpToDateCollection ( true ) // Collection information is up to date
221129 }
222130 }
223131
132+ // Start refreshing the collections when a collection is being indexed
133+ useEffect ( ( ) => {
134+ let interval
135+ if ( realTimeReports ) {
136+ interval = setInterval ( ( ) => {
137+ refetchCollection ( )
138+ } , 1000 )
139+ } else {
140+ clearInterval ( interval )
141+ }
142+ return ( ) => clearInterval ( interval )
143+ } , [ realTimeReports ] )
144+
145+ useEffect ( ( ) => {
146+ fetchCollections ( )
147+ } , [ refetchIndex ] )
148+
224149 return (
225150 < div className = "col-md-12" >
226151 < Wrapper >
0 commit comments