Skip to content

Commit

Permalink
Merge pull request #8763 from rtibbles/library_returns
Browse files Browse the repository at this point in the history
Prevent unnecessary fetches on the library page.
  • Loading branch information
rtibbles committed Nov 29, 2021
2 parents 5de9da3 + 2a37f6b commit 62b0a01
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 35 deletions.
2 changes: 1 addition & 1 deletion kolibri/plugins/learn/assets/src/composables/useSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { deduplicateResources } from '../utils/contentNode';
import { normalizeContentNode } from '../modules/coreLearn/utils';
import useContentNodeProgress from './useContentNodeProgress';

const searchKeys = [
export const searchKeys = [
'learning_activities',
'categories',
'learner_needs',
Expand Down
67 changes: 35 additions & 32 deletions kolibri/plugins/learn/assets/src/modules/recommended/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@ import samePageCheckGenerator from 'kolibri.utils.samePageCheckGenerator';
import ConditionalPromise from 'kolibri.lib.conditionalPromise';
import { PageNames } from '../../constants';
import useChannels from '../../composables/useChannels';
import useContentNodeProgress from '../../composables/useContentNodeProgress';
import useLearnerResources from '../../composables/useLearnerResources';
import { searchKeys } from '../../composables/useSearch';
import { _collectionState } from '../coreLearn/utils';

const { channels } = useChannels();

const { fetchContentNodeProgress } = useContentNodeProgress();

const { fetchResumableContentNodes } = useLearnerResources();

export function showLibrary(store) {
export function showLibrary(store, query) {
if (!get(channels).length) {
return;
}
Expand All @@ -24,41 +22,46 @@ export function showLibrary(store) {
store.commit('CORE_SET_PAGE_LOADING', true);
}

const promises = [
ContentNodeResource.fetchCollection({
getParams: {
parent__isnull: true,
include_coach_content:
store.getters.isAdmin || store.getters.isCoach || store.getters.isSuperuser,
},
}),
];
const promises = [];

if (store.getters.isUserLoggedIn) {
fetchContentNodeProgress({ resume: true });
promises.push(fetchResumableContentNodes());
if (!searchKeys.some(key => query[key])) {
// If not currently on a route with search terms
promises.push(
ContentNodeResource.fetchCollection({
getParams: {
parent__isnull: true,
include_coach_content:
store.getters.isAdmin || store.getters.isCoach || store.getters.isSuperuser,
},
})
);
if (store.getters.isUserLoggedIn) {
promises.push(fetchResumableContentNodes());
}
}

return ConditionalPromise.all(promises).only(
samePageCheckGenerator(store),
([channelCollection]) => {
// we want them to be in the same order as the channels list
const rootNodes = get(channels)
.map(channel => {
const node = _collectionState(channelCollection).find(n => n.channel_id === channel.id);
if (node) {
// The `channel` comes with additional data that is
// not returned from the ContentNodeResource.
// Namely thumbnail, description and tagline (so far)
node.title = channel.name || node.title;
node.thumbnail = channel.thumbnail;
node.description = channel.tagline || channel.description;
return node;
}
})
.filter(Boolean);
if (channelCollection && channelCollection.length) {
// we want them to be in the same order as the channels list
const rootNodes = get(channels)
.map(channel => {
const node = _collectionState(channelCollection).find(n => n.channel_id === channel.id);
if (node) {
// The `channel` comes with additional data that is
// not returned from the ContentNodeResource.
// Namely thumbnail, description and tagline (so far)
node.title = channel.name || node.title;
node.thumbnail = channel.thumbnail;
node.description = channel.tagline || channel.description;
return node;
}
})
.filter(Boolean);

store.commit('SET_ROOT_NODES', rootNodes);
store.commit('SET_ROOT_NODES', rootNodes);
}

store.commit('CORE_SET_PAGE_LOADING', false);
store.commit('CORE_SET_ERROR', null);
Expand Down
4 changes: 2 additions & 2 deletions kolibri/plugins/learn/assets/src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ export default [
{
name: PageNames.LIBRARY,
path: '/library',
handler: () => {
handler: to => {
if (unassignedContentGuard()) {
return unassignedContentGuard();
}
showLibrary(store);
showLibrary(store, to.query);
},
component: LibraryPage,
},
Expand Down

0 comments on commit 62b0a01

Please sign in to comment.