diff --git a/apps/audius-client/packages/common/src/store/saved-collections/selectors.ts b/apps/audius-client/packages/common/src/store/saved-collections/selectors.ts index ca23464d38d..a037de04808 100644 --- a/apps/audius-client/packages/common/src/store/saved-collections/selectors.ts +++ b/apps/audius-client/packages/common/src/store/saved-collections/selectors.ts @@ -10,6 +10,7 @@ import { CommonState } from '../commonStore' import { CollectionType } from './types' const getAccountCollections = (state: CommonState) => state.account.collections + export const getSavedCollectionsState = ( state: CommonState, type: CollectionType diff --git a/apps/audius-client/packages/mobile/src/store/offline-downloads/sagas/requestDownloadAllFavoritesSaga.ts b/apps/audius-client/packages/mobile/src/store/offline-downloads/sagas/requestDownloadAllFavoritesSaga.ts index adbda3e94f9..5481950a766 100644 --- a/apps/audius-client/packages/mobile/src/store/offline-downloads/sagas/requestDownloadAllFavoritesSaga.ts +++ b/apps/audius-client/packages/mobile/src/store/offline-downloads/sagas/requestDownloadAllFavoritesSaga.ts @@ -3,6 +3,7 @@ import { getContext, savedPageSelectors } from '@audius/common' +import { fetchAllAccountCollections } from 'common/store/saved-collections/sagas' import moment from 'moment' import { takeEvery, select, call, put } from 'typed-redux-saga' @@ -78,6 +79,9 @@ function* downloadAllFavorites() { } // Add favorited collections and their tracks + // AccountCollections don't include track lists, so retrieve all the collections + // first + yield* call(fetchAllAccountCollections) const favoritedCollections = yield* select(getAccountCollections) for (const favoritedCollection of favoritedCollections) { diff --git a/apps/audius-client/packages/web/src/common/store/saved-collections/sagas.ts b/apps/audius-client/packages/web/src/common/store/saved-collections/sagas.ts index f55e064ddeb..6c0a3b0fb78 100644 --- a/apps/audius-client/packages/web/src/common/store/saved-collections/sagas.ts +++ b/apps/audius-client/packages/web/src/common/store/saved-collections/sagas.ts @@ -1,12 +1,23 @@ -import { savedCollectionsActions, waitForRead } from '@audius/common' -import { call, put, takeEvery } from 'typed-redux-saga' +import { + CollectionType, + ID, + savedCollectionsActions, + savedCollectionsSelectors, + waitForRead +} from '@audius/common' +import { all, call, select, put, takeEvery } from 'typed-redux-saga' import { retrieveCollections } from '../cache/collections/utils' const { fetchCollections, fetchCollectionsSucceeded } = savedCollectionsActions +const { getAccountAlbums, getAccountPlaylists } = savedCollectionsSelectors -function* fetchCollectionsAsync(action: ReturnType) { - const { type, ids } = action.payload +type FetchCollectionsConfig = { + type: CollectionType + ids: ID[] +} + +function* fetchCollectionsAsync({ ids, type }: FetchCollectionsConfig) { yield waitForRead() yield* call(retrieveCollections, ids) @@ -18,8 +29,35 @@ function* fetchCollectionsAsync(action: ReturnType) { ) } +/** Will create and wait on parallel effects to fetch full details for all saved albums and + * playlists. Note: Only use this if you really need full details (such as track + * lists) for all collections, as it may potentially fetch a lot of data. + */ +export function* fetchAllAccountCollections() { + yield waitForRead() + + const { data: playlists } = yield* select(getAccountPlaylists) + const { data: albums } = yield* select(getAccountAlbums) + + yield* all([ + call(fetchCollectionsAsync, { + ids: albums.map(({ id }) => id), + type: 'albums' + }), + call(fetchCollectionsAsync, { + ids: playlists.map(({ id }) => id), + type: 'playlists' + }) + ]) +} + function* watchFetchCollections() { - yield takeEvery(fetchCollections.type, fetchCollectionsAsync) + yield* takeEvery( + fetchCollections.type, + function* (action: ReturnType) { + yield* fetchCollectionsAsync(action.payload) + } + ) } export default function sagas() {