Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent unnecessary fetches on the library page. #8763

Merged
merged 2 commits into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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])) {
rtibbles marked this conversation as resolved.
Show resolved Hide resolved
// 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