import followBatchTopicsURL followTopicURL getCuratedSuggestion URL ge

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import {
followBatchTopicsURL,
followTopicURL,
getCuratedSuggestionURL,
getCuratedTopicsURL,
unfollowTopicURL
} from '../config/api-utils'
import proxy from '../../../common/lib/http-common'
import axios from 'axios'
import { articlesTypes as articleTypes, curatedTypes as types } from './mutation-types'
const getDefaultFollowCuratedState = () => {
return {
curated: {
list_all: [],
search: '',
suggested: [],
selected: [],
requests: [],
loaders: {
save: false,
suggestion: false,
curated_view: true,
auto_complete: false
}
}
}
}
const state = getDefaultFollowCuratedState()
const getters = {
getFollowCurated: state => {
return state.curated
},
getFollowCuratedLoader: state => {
return state.curated.loaders
},
getCuratedTopicsList: state => {
return state.curated.list_all
}
}
const actions = {
allCuratedTopics ({ commit, getters, dispatch }) {
console.log('allCuratedTopics', getters.getDiscoverFetchStatus)
if (getters.getDiscoverFetchStatus === true) {
commit(types.SET_CURATED_VIEW_LOADER, false)
return
}
commit(types.SET_CURATED_VIEW_LOADER, true)
proxy.post(getCuratedTopicsURL, {
workspace_id: getters.getWorkspaces.activeWorkspace._id
}).then(resp => {
if (resp.data.status) {
commit(types.SET_CURATED_VIEW_LOADER, false)
commit(types.SET_CURATED_TOPICS_ALL, resp.data.list)
}
}).catch(err => {
commit(types.SET_CURATED_VIEW_LOADER, false)
})
},
searchCuratedTopic ({ commit, getters, dispatch }) {
proxy.post(getCuratedSuggestionURL, {
filters: {
search: getters.getFollowCurated.search,
match_phrase: true
},
workspace_id: getters.getWorkspaces.activeWorkspace._id
}).then(
resp => {
dispatch('removeDuplicates', resp.data.curated_suggestion)
}
)
},
async autocompleteCuratedTopic ({ commit, getters, dispatch }) {
// cancel previously sent requests
let CancelToken = axios.CancelToken
let searchRequests = getters.getFollowCurated.requests
for (let i = 0; i < searchRequests.length; i++) {
let remove = searchRequests[i]
remove()
}
commit(types.SET_SUGGESTED_LIST, [])
let cancel
// for search content suggestions
commit(types.SET_AUTOCOMPLETE_CURATED_TOPICS_LOADER, true)
let res = await axios.post(getCuratedSuggestionURL, {
filters: {
search: getters.getFollowCurated.search,
match_phrase: true
},
workspace_id: getters.getWorkspaces.activeWorkspace._id
}, { headers: { Authorization: 'Bearer ' + getters.getJWTToken } }, {
cancelToken: new CancelToken(function executor (c) {
// An executor function receives a cancel function as a parameter
cancel = c
})
}).then(
resp => {
dispatch('removeDuplicates', resp.data.curated_suggestion)
return resp
}).catch(
() => {
return null
}
)
commit(types.SET_AUTOCOMPLETE_CURATED_TOPICS_LOADER, false)
},
saveCuratedTopics ({ commit, getters, dispatch }) {
commit(types.SET_CURATED_SAVE_LOADER, true)
proxy.post(followBatchTopicsURL, {
workspace_id: getters.getWorkspaces.activeWorkspace._id,
curated_topics: getters.getFollowCurated.selected
}).then(
resp => {
commit(types.SET_CURATED_SAVE_LOADER, false)
$('#curated_topic_modal').modal('hide')
if (resp.data.status) {
dispatch('toastNotification', { message: 'Your curated topics have been saved!', type: 'success' })
commit(types.SET_SUGGESTED_LIST, [])
commit(types.SET_SELECTED_LIST, [])
dispatch('addToCuratedTopicsList', resp.data.curated_topics)
// follow them to the original list
resp.data.curated_topics.forEach((topic) => {
getters.getCuratedTopicsList.forEach(item => {
item.list.forEach(curated => {
if (curated.id === topic.id) curated.followed = true
})
})
})
}
}
).catch()
$('#curated_topic_modal').modal('hide')
},
removeCuratedTopic ({ commit, getters }) {
},
removeDuplicates ({ commit, getters }, suggestedTopics) {
let cleanedList = []
suggestedTopics.forEach(nestedElement => {
let exists = false
getters.getFollowCurated.selected.forEach(element => {
if (nestedElement.name === element.name) {
exists = true
}
})
getters.getMyCuratedTopicsList.forEach(element => {
if (nestedElement.name === element.name) {
exists = true
}
})
if (!exists) {
cleanedList.push(nestedElement)
}
})
console.log(cleanedList)
commit(types.SET_SUGGESTED_LIST, cleanedList)
},
followCuratedTopic ({ commit, getters, state, dispatch }, payload) {
console.debug('Action:followCuratedTopic', payload)
let topic = payload.topic
topic.followed = true
topic.workspace_id = getters.getWorkspaces.activeWorkspace._id
proxy.post(followTopicURL, topic).then(
resp => {
if (resp.data.status) {
if (resp.data.curated_topic) {
topic.id = resp.data.curated_topic.id
dispatch('addToCuratedTopicsList', [resp.data.curated_topic])
dispatch('trackEvent', { event: 'curated_topics_followed' })
// if there is no payload id
if (!payload.hasOwnProperty('id')) {
commit(articleTypes.SET_SEARCH_ARTICLES_TOPIC, resp.data.curated_topic)
}
// mark them to the list
if (payload.id) { // verify this section may be not using
getters.getCuratedTopicsList.forEach(item => {
item.list.forEach(curated => {
if (curated.id === payload.id) curated.followed = true
})
})
}
if (topic.name) {
getters.getCuratedTopicsList.forEach(item => {
item.list.forEach(curated => {
if (curated.name === topic.name) curated.followed = true
})
})
}
}
// dispatch('toastNotification', { message: FOLLOWED, type: 'success' })
}
}
).catch()
},
unfollowCuratedTopic ({ commit, getters, state, dispatch }, payload) {
console.debug('Action:unfollowCuratedTopic', payload)
let topic = payload.topic
let index = payload.index
topic.followed = false
commit(articleTypes.SET_SEARCH_ARTICLES_TOPIC, JSON.parse(JSON.stringify(topic)))
topic.workspace_id = getters.getWorkspaces.activeWorkspace._id
// remove the item from the list on topics curated topics
let curatedTopicsList = getters.getMyCuratedTopicsList
if (index != null) {
curatedTopicsList.splice(index, 1)
commit('SET_CURATED_TOPICS_LIST', curatedTopicsList)
} else {
let topicIndex = null
curatedTopicsList.forEach(function (item, index) {
if (item.name === topic.name) topicIndex = index
})
if (topicIndex !== null) curatedTopicsList.splice(topicIndex, 1)
commit('SET_CURATED_TOPICS_LIST', curatedTopicsList)
}
proxy.post(unfollowTopicURL, topic).then(resp => {
if (resp.data.status) {
getters.getCuratedTopicsList.forEach(item => {
item.list.forEach(curated => {
if (curated.name === topic.name) curated.followed = false
})
})
if (getters.getCoveredStoriesPosts.length > 0) {
// get the covered stories and remove it.
getters.getCoveredStoriesPosts.filter(
(item, indexItem) => {
if (item.topic._id === topic._id) {
getters.getCoveredStoriesPosts.splice(indexItem, 1)
}
}
)
if (getters.myCuratedTopics && getters.myCuratedTopics.length > 0) {
getters.myCuratedTopics.filter(
(item, itemIndex) => {
if (item.topic._id === topic._id) {
getters.myCuratedTopics.splice(itemIndex, 1)
}
}
)
}
}
}
}).catch()
}
}
const mutations = {
[types.SET_CURATED_TOPICS_ALL] (state, list) {
state.curated.list_all = list
},
[types.SET_SELECTED_LIST] (state, list) {
state.curated.selected = list
},
[types.ADD_TO_SELECTED_LIST] (state, payload) {
state.curated.selected.push(payload.topic)
state.curated.suggested.splice(payload.index, 1)
},
[types.SET_SUGGESTED_LIST] (state, list) {
state.curated.suggested = list
},
[types.ADD_TO_SUGGESTED_LIST] (state, payload) {
state.curated.suggested.push(payload.topic)
state.curated.selected.splice(payload.index, 1)
},
[types.SET_CURATED_SAVE_LOADER] (state, value) {
state.curated.loaders.save = value
},
[types.SET_CURATED_VIEW_LOADER] (state, value) {
state.curated.loaders.curated_view = value
},
[types.SET_AUTOCOMPLETE_CURATED_TOPICS_LOADER] (state, value) {
state.curated.loaders.auto_complete = value
}
}
export default {
state, getters, actions, mutations
}