1
- import { useEffect , useMemo } from 'react'
1
+ import { useEffect , useMemo , useState } from 'react'
2
2
import { useParams } from 'react-router'
3
3
import { APIResponseHandler } from '../APIResponseHandler'
4
4
import PluginTagSelect from './PluginTagSelect'
5
+ import PluginList from './PluginList'
6
+ import { FilterChips } from '../FilterChips'
5
7
import { SearchBar , useAsync } from '../../../Common'
6
8
import { getAvailablePluginTags , getPluginStoreData } from './service'
7
- import { PluginListParamsType , PluginListContainerProps , PluginListProps } from './types'
9
+ import {
10
+ PluginListParamsType ,
11
+ PluginListContainerProps ,
12
+ PluginListProps ,
13
+ PluginListFiltersType ,
14
+ PluginListItemType ,
15
+ } from './types'
8
16
import { ReactComponent as ICCross } from '../../../Assets/Icon/ic-cross.svg'
9
17
import { ReactComponent as ICVisibility } from '../../../Assets/Icon/ic-visibility-on.svg'
10
- import PluginList from './PluginList '
18
+ import { DEFAULT_PLUGIN_LIST_FILTERS } from './constants '
11
19
12
20
const PluginListContainer = ( {
13
- pluginList,
14
- handlePluginListUpdate,
15
- filters,
16
- handleUpdateFilters,
17
- isSelectable,
18
21
availableTags,
19
22
handleUpdateAvailableTags,
20
23
pluginDataStore,
21
24
handlePluginDataStoreUpdate,
22
- totalCount,
23
- handleUpdateTotalCount,
24
- persistFilters,
25
- selectedPluginsMap,
26
25
handlePluginSelection,
26
+ // Selected plugins Section
27
+ isSelectable,
28
+ selectedPluginsMap,
29
+ // Plugin list Section
30
+ persistFilters,
31
+ parentPluginList,
32
+ handleParentPluginListUpdate,
33
+ parentTotalCount,
34
+ handleParentTotalCount,
35
+ parentFilters,
36
+ handleUpdateParentFilters,
27
37
} : Readonly < PluginListContainerProps > ) => {
28
- const { searchKey, selectedTags, showSelectedPlugins } = filters
29
38
const { appId } = useParams < PluginListParamsType > ( )
30
39
40
+ const [ pluginList , setPluginList ] = useState < PluginListItemType [ ] > ( parentPluginList || [ ] )
41
+ const [ totalCount , setTotalCount ] = useState < number > ( parentTotalCount || 0 )
42
+ const [ filters , setFilters ] = useState < PluginListFiltersType > (
43
+ parentFilters || structuredClone ( DEFAULT_PLUGIN_LIST_FILTERS ) ,
44
+ )
45
+
46
+ const handlePluginListUpdate = ( updatedPluginList : PluginListItemType [ ] ) => {
47
+ setPluginList ( updatedPluginList )
48
+ handleParentPluginListUpdate ?.( updatedPluginList )
49
+ }
50
+
51
+ const handleUpdateTotalCount = ( updatedTotalCount : number ) => {
52
+ setTotalCount ( updatedTotalCount )
53
+ handleParentTotalCount ?.( updatedTotalCount )
54
+ }
55
+
56
+ const handleUpdateFilters = ( updatedFilters : PluginListFiltersType ) => {
57
+ setFilters ( updatedFilters )
58
+ handleUpdateParentFilters ?.( updatedFilters )
59
+ }
60
+
61
+ const { searchKey, selectedTags, showSelectedPlugins } = filters || { }
62
+
31
63
const pluginDataDependency = persistFilters ? [ pluginList ] : [ searchKey , appId , selectedTags ]
32
64
// TODO: Add abortController as well
33
65
const [ isLoadingPluginData , pluginData , pluginDataError , reloadPluginData ] = useAsync (
@@ -36,7 +68,7 @@ const PluginListContainer = ({
36
68
searchKey,
37
69
selectedTags,
38
70
offset : 0 ,
39
- appId : appId ? + appId : undefined ,
71
+ appId : appId ? + appId : null ,
40
72
} ) ,
41
73
pluginDataDependency ,
42
74
persistFilters ? ! pluginList . length : true ,
@@ -99,16 +131,6 @@ const PluginListContainer = ({
99
131
} )
100
132
}
101
133
102
- useEffect (
103
- // TODO: Test this persistFilters value on unmount
104
- ( ) => ( ) => {
105
- if ( ! persistFilters ) {
106
- handleClearFilters ( )
107
- }
108
- } ,
109
- [ ] ,
110
- )
111
-
112
134
const handleSearch = ( searchText : string ) => {
113
135
handleUpdateFilters ( {
114
136
...filters ,
@@ -145,6 +167,13 @@ const PluginListContainer = ({
145
167
} )
146
168
}
147
169
170
+ const handleRemoveSelectedTag = ( filterConfig : Pick < PluginListFiltersType , 'selectedTags' > ) => {
171
+ handleUpdateFilters ( {
172
+ ...filters ,
173
+ selectedTags : filterConfig . selectedTags ,
174
+ } )
175
+ }
176
+
148
177
// TODO: Be remember to turn showSelectedPluginFilter as false if count becomes 0
149
178
const showSelectedPluginFilter = useMemo (
150
179
( ) => isSelectable && selectedPluginsMap && Object . keys ( selectedPluginsMap ) . length > 0 ,
@@ -191,6 +220,18 @@ const PluginListContainer = ({
191
220
) }
192
221
</ div >
193
222
223
+ { ! ! selectedTags . length && (
224
+ < FilterChips < Pick < PluginListFiltersType , 'selectedTags' > >
225
+ filterConfig = { {
226
+ selectedTags,
227
+ } }
228
+ onRemoveFilter = { handleRemoveSelectedTag }
229
+ clearFilters = { handleClearFilters }
230
+ className = "p-0 w-100 pt-4"
231
+ clearButtonClassName = "dc__no-background-imp dc__no-border-imp dc__tab-focus"
232
+ />
233
+ ) }
234
+
194
235
< APIResponseHandler
195
236
isLoading = { isLoadingPluginData }
196
237
error = { pluginDataError }
0 commit comments