11import React , { PropsWithChildren , useEffect , useMemo , useState } from 'react'
22import { doRequest } from '../../requests/request'
33import { showSimpleError } from '../../utils/notification'
4- import { ITopic } from '../../requests/responses/topic'
4+ import { ITopic , TopicState } from '../../requests/responses/topic'
55import { ITopicsContext , ITopicsFilters , TopicsContext } from './context'
66import { PaginationResponse } from '../../requests/responses/pagination'
77
88interface ITopicsProviderProps {
9- includeClosedTopics ?: boolean
109 limit : number
1110 hideIfEmpty ?: boolean
1211 researchSpecific ?: boolean
1312 initialFilters ?: Partial < ITopicsFilters >
13+ states ?: TopicState [ ]
1414}
1515
1616const TopicsProvider = ( props : PropsWithChildren < ITopicsProviderProps > ) => {
1717 const {
1818 children,
19- includeClosedTopics = false ,
2019 limit,
2120 hideIfEmpty = false ,
2221 researchSpecific = true ,
2322 initialFilters,
23+ states = [ ] ,
2424 } = props
2525
2626 const [ topics , setTopics ] = useState < PaginationResponse < ITopic > > ( )
2727 const [ page , setPage ] = useState ( 0 )
2828 const [ filters , setFilters ] = useState < ITopicsFilters > ( {
29- includeClosed : includeClosedTopics ,
29+ states : states ,
3030 researchSpecific : researchSpecific ,
3131 ...initialFilters ,
3232 } )
3333
3434 const [ isLoading , setIsLoading ] = useState ( false )
3535
36- useEffect ( ( ) => {
36+ const fetchTopics = async ( ) => {
3737 setIsLoading ( true )
3838
3939 return doRequest < PaginationResponse < ITopic > > (
@@ -45,7 +45,7 @@ const TopicsProvider = (props: PropsWithChildren<ITopicsProviderProps>) => {
4545 page,
4646 limit,
4747 type : filters . types ?. join ( ',' ) || '' ,
48- includeClosed : filters . includeClosed ? 'true' : 'false ',
48+ states : filters . states ?. join ( ',' ) || ' ',
4949 onlyOwnResearchGroup : filters . researchSpecific ? 'true' : 'false' ,
5050 search : filters . search ?? '' ,
5151 researchGroupIds : filters . researchGroupIds ?. join ( ',' ) || '' ,
@@ -70,12 +70,16 @@ const TopicsProvider = (props: PropsWithChildren<ITopicsProviderProps>) => {
7070 setTopics ( res . data )
7171 } ,
7272 )
73+ }
74+
75+ useEffect ( ( ) => {
76+ fetchTopics ( )
7377 } , [ filters , page , limit ] )
7478
7579 useEffect ( ( ) => {
7680 setFilters ( ( prev ) => ( {
7781 ...prev ,
78- includeClosed : includeClosedTopics ,
82+ states : states ,
7983 researchSpecific : researchSpecific ,
8084 ...initialFilters ,
8185 } ) )
@@ -99,10 +103,18 @@ const TopicsProvider = (props: PropsWithChildren<ITopicsProviderProps>) => {
99103
100104 const index = prev . content . findIndex ( ( x ) => x . topicId === newTopic . topicId )
101105
106+ let newFetchRequired = false
107+
102108 if ( index >= 0 ) {
109+ newFetchRequired = newTopic . state !== prev . content [ index ] . state
103110 prev . content [ index ] = newTopic
104111 }
105112
113+ if ( newFetchRequired ) {
114+ // If state changed, refetch to update based on filters
115+ fetchTopics ( )
116+ }
117+
106118 return { ...prev }
107119 } )
108120 } ,
@@ -112,9 +124,14 @@ const TopicsProvider = (props: PropsWithChildren<ITopicsProviderProps>) => {
112124 return undefined
113125 }
114126
115- prev . content = [ newTopic , ...prev . content ] . slice ( - limit )
116- prev . totalElements += 1
117- prev . totalPages = Math . ceil ( prev . totalElements / limit )
127+ const states = filters . states ?? [ TopicState . OPEN . toString ( ) ]
128+ const newHasState = states . includes ( newTopic . state )
129+
130+ if ( newHasState ) {
131+ prev . content = [ newTopic , ...prev . content ] . slice ( - limit )
132+ prev . totalElements += 1
133+ prev . totalPages = Math . ceil ( prev . totalElements / limit )
134+ }
118135
119136 return { ...prev }
120137 } )
0 commit comments