1
- import React , { memo , useEffect , useState } from 'react' ;
1
+ import React , { memo , useEffect , useMemo , useState } from 'react' ;
2
2
import { useDispatch , useSelector } from 'react-redux' ;
3
3
4
4
import { GlobalState } from 'mattermost-redux/types/store' ;
@@ -21,27 +21,26 @@ import {getSubscribeModalState, getWebsocketEventState} from 'selectors';
21
21
22
22
import usePluginApi from 'hooks/usePluginApi' ;
23
23
import useApiRequestCompletionState from 'hooks/useApiRequestCompletionState' ;
24
-
25
- import { getCurrentChannelSubscriptions } from 'utils/filterData' ;
24
+ import usePreviousState from 'hooks/usePreviousState' ;
26
25
27
26
const ProjectDetails = memo ( ( projectDetails : ProjectDetails ) => {
28
27
const { projectName, organizationName} = projectDetails ;
29
28
30
- // Hooks
31
- const dispatch = useDispatch ( ) ;
32
- const { makeApiRequestWithCompletionStatus, makeApiRequest, getApiState, state} = usePluginApi ( ) ;
33
-
34
29
// State variables
30
+ const [ showAllSubscriptions , setShowAllSubscriptions ] = useState ( false ) ;
35
31
const [ showProjectConfirmationModal , setShowProjectConfirmationModal ] = useState ( false ) ;
36
32
const [ showSubscriptionConfirmationModal , setShowSubscriptionConfirmationModal ] = useState ( false ) ;
37
33
const [ subscriptionToBeDeleted , setSubscriptionToBeDeleted ] = useState < SubscriptionPayload > ( ) ;
38
- const [ showAllSubscriptions , setShowAllSubscriptions ] = useState ( false ) ;
39
- const [ subscriptionList , setSubscriptionList ] = useState < SubscriptionDetails [ ] > ( [ ] ) ;
40
34
const { currentChannelId} = useSelector ( ( reduxState : GlobalState ) => reduxState . entities . channels ) ;
35
+ const subscriptionListApiParams = useMemo < FetchSubscriptionList > ( ( ) => ( {
36
+ project : projectName ,
37
+ channel_id : showAllSubscriptions ? '' : currentChannelId ,
38
+ } ) , [ projectName , currentChannelId , showAllSubscriptions ] ) ;
41
39
42
- const project : FetchSubscriptionList = { project : projectName } ;
43
- const { data, isLoading} = getApiState ( plugin_constants . pluginApiServiceConfigs . getSubscriptionList . apiServiceName , project ) ;
44
- const subscriptionData = data as SubscriptionDetails [ ] ;
40
+ // Hooks
41
+ const previousState = usePreviousState ( { currentChannelId} ) ;
42
+ const dispatch = useDispatch ( ) ;
43
+ const { makeApiRequestWithCompletionStatus, makeApiRequest, getApiState, state} = usePluginApi ( ) ;
45
44
46
45
const handleResetProjectDetails = ( ) => {
47
46
dispatch ( resetProjectDetails ( ) ) ;
@@ -86,7 +85,7 @@ const ProjectDetails = memo((projectDetails: ProjectDetails) => {
86
85
// Fetch subscription list
87
86
const fetchSubscriptionList = ( ) => makeApiRequest (
88
87
plugin_constants . pluginApiServiceConfigs . getSubscriptionList . apiServiceName ,
89
- project ,
88
+ subscriptionListApiParams ,
90
89
) ;
91
90
92
91
// Handles deletion of a subscription and fetching the modified subscription list
@@ -105,31 +104,27 @@ const ProjectDetails = memo((projectDetails: ProjectDetails) => {
105
104
106
105
// Reset the state when the component is unmounted
107
106
useEffect ( ( ) => {
108
- if ( ! getWebsocketEventState ( state ) . isSubscriptionDeleted ) {
109
- fetchSubscriptionList ( ) ;
110
- }
111
-
112
107
return ( ) => {
113
108
handleResetProjectDetails ( ) ;
114
109
} ;
115
110
} , [ ] ) ;
116
111
117
112
useEffect ( ( ) => {
118
- if ( subscriptionData ) {
119
- if ( showAllSubscriptions ) {
120
- setSubscriptionList ( subscriptionData ) ;
121
- } else {
122
- setSubscriptionList ( getCurrentChannelSubscriptions ( subscriptionData , currentChannelId ) ) ;
123
- }
113
+ /**
114
+ * Prevent calling API to fetch subscription list twice on switching channel
115
+ *
116
+ * If the current channel is changed and "showAllSubscriptions" was true on the last channel then
117
+ * this useEffect runs twice because "subscriptionListApiParams" is modified twice
118
+ * once when "currentChannelId" is updated and other time when "showAllSubscriptions" is set to false
119
+ */
120
+ if ( showAllSubscriptions && previousState ?. currentChannelId !== currentChannelId ) {
121
+ return ;
124
122
}
125
- } , [ subscriptionData , showAllSubscriptions ] ) ;
123
+ fetchSubscriptionList ( ) ;
124
+ } , [ subscriptionListApiParams ] ) ;
126
125
127
- // Update subscription list on switching channels
128
126
useEffect ( ( ) => {
129
- if ( subscriptionData ) {
130
- setShowAllSubscriptions ( false ) ;
131
- setSubscriptionList ( getCurrentChannelSubscriptions ( subscriptionData , currentChannelId ) ) ;
132
- }
127
+ setShowAllSubscriptions ( false ) ;
133
128
} , [ currentChannelId ] ) ;
134
129
135
130
// Fetch the subscription list when new subscription is created
@@ -140,6 +135,9 @@ const ProjectDetails = memo((projectDetails: ProjectDetails) => {
140
135
}
141
136
} , [ getSubscribeModalState ( state ) . isCreated ] ) ;
142
137
138
+ const { data, isLoading} = getApiState ( plugin_constants . pluginApiServiceConfigs . getSubscriptionList . apiServiceName , subscriptionListApiParams ) ;
139
+ const subscriptionList = data as SubscriptionDetails [ ] ;
140
+
143
141
const { isLoading : isUnlinkProjectLoading } = getApiState ( plugin_constants . pluginApiServiceConfigs . unlinkProject . apiServiceName , projectDetails ) ;
144
142
const { isLoading : isDeleteSubscriptionLoading } = getApiState ( plugin_constants . pluginApiServiceConfigs . deleteSubscription . apiServiceName , subscriptionToBeDeleted ) ;
145
143
0 commit comments