Skip to content

Commit 67c9c30

Browse files
committed
fix value of isLoading inside project topics reducer so it's true when any kind of topic (private or public) is loading
1 parent 485096a commit 67c9c30

File tree

1 file changed

+41
-5
lines changed

1 file changed

+41
-5
lines changed

src/projects/reducers/projectTopics.js

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,15 @@ import {
3737
import update from 'react-addons-update'
3838
import { getLinksFromPost } from '../../helpers/draftJSHelper'
3939

40-
4140
const initialState = {
41+
// as we can load different kind of topics in parallel we have to track their loading status separately
42+
isLoadingPerTag: {
43+
MESSAGES: false,
44+
PRIMARY: false,
45+
},
46+
// `isLoading` would indicate the loading of any kind of topic as aggregation value for the values above
47+
// technically we could aggregate inside components, but as we already use `isLoading` in many places
48+
// so we do it inside the reducer for backward compatibility
4249
isLoading: false,
4350
isCreatingFeed: false,
4451
error: false,
@@ -49,6 +56,28 @@ const initialState = {
4956
}
5057
}
5158

59+
/**
60+
* Builds updated value of `isLoadingPerTag` state
61+
*
62+
* @param {Object} currentIsLoadingPerTag current `isLoadingPerTag` state
63+
* @param {String} tag topic tag
64+
* @param {Boolean} isLoading `true` if topic with such tag is loading now
65+
*
66+
* @returns {Object} update value for `isLoadingPerTag` state
67+
*/
68+
const updateIsLoadingPerTag = (currentIsLoadingPerTag, tag, isLoading) => ({
69+
...currentIsLoadingPerTag,
70+
[tag]: isLoading,
71+
})
72+
73+
/**
74+
* Calculates values for `isLoading` state value based on `isLoadingPerTag` state
75+
*
76+
* @param {Object} isLoadingPerTag `isLoadingPerTag` state
77+
*
78+
* @returns {Boolean} `true` if topic with any tag is loaded
79+
*/
80+
const getIsLoading = (isLoadingPerTag) => _.some(_.values(isLoadingPerTag))
5281

5382
export const projectTopics = function (state=initialState, action) {
5483
const payload = action.payload
@@ -63,9 +92,11 @@ export const projectTopics = function (state=initialState, action) {
6392
if (action.meta.projectId === state.projectId) {
6493
const feedUpdateQuery = {}
6594
feedUpdateQuery[action.meta.tag] = { $merge: { topics: [], totalCount: 0 } }
95+
const updatedIsLoadingPerTag = updateIsLoadingPerTag(state.isLoadingPerTag, action.meta.tag, true)
6696
return update(state, {
6797
feeds: feedUpdateQuery,
68-
isLoading: { $set: true },
98+
isLoadingPerTag: { $set: updatedIsLoadingPerTag },
99+
isLoading: { $set: getIsLoading(updatedIsLoadingPerTag) },
69100
error: { $set: false }
70101
})
71102
} else {
@@ -101,18 +132,23 @@ export const projectTopics = function (state=initialState, action) {
101132

102133
const feedUpdateQuery = {}
103134
feedUpdateQuery[action.meta.tag] = { $merge: { topics, totalCount: payload.totalCount } }
135+
const updatedIsLoadingPerTag = updateIsLoadingPerTag(state.isLoadingPerTag, action.meta.tag, false)
104136
return update(state, {
105-
isLoading: {$set: false},
137+
isLoadingPerTag: {$set: updatedIsLoadingPerTag},
138+
isLoading: {$set: getIsLoading(updatedIsLoadingPerTag)},
106139
error: {$set: false},
107140
feeds: feedUpdateQuery
108141
})
109142
}
110143
case LOAD_PROJECT_FEEDS_MEMBERS_FAILURE:
111-
case LOAD_PROJECT_FEEDS_FAILURE:
144+
case LOAD_PROJECT_FEEDS_FAILURE: {
145+
const updatedIsLoadingPerTag = updateIsLoadingPerTag(state.isLoadingPerTag, action.meta.tag, false)
112146
return Object.assign({}, initialState, {
113-
isLoading: false,
147+
isLoadingPerTag: {$set: updatedIsLoadingPerTag},
148+
isLoading: {$set: getIsLoading(updatedIsLoadingPerTag)},
114149
error: true
115150
})
151+
}
116152
case CREATE_PROJECT_FEED_PENDING:
117153
return Object.assign({}, state, {
118154
isCreatingFeed: true,

0 commit comments

Comments
 (0)