Small refactor to get the last used collection.

This commit is contained in:
Franco Montenegro 2022-03-08 13:56:49 -03:00 committed by jessopb
parent c843991378
commit 66bdd3fc87
2 changed files with 16 additions and 32 deletions

View file

@ -3,6 +3,7 @@ import { selectClaimForUri, selectClaimIsMine } from 'redux/selectors/claims';
import { doCollectionEdit, doFetchItemsInCollection } from 'redux/actions/collections'; import { doCollectionEdit, doFetchItemsInCollection } from 'redux/actions/collections';
import { doPrepareEdit } from 'redux/actions/publish'; import { doPrepareEdit } from 'redux/actions/publish';
import { import {
makeSelectCollectionForId,
makeSelectCollectionForIdHasClaimUrl, makeSelectCollectionForIdHasClaimUrl,
makeSelectCollectionIsMine, makeSelectCollectionIsMine,
makeSelectEditedCollectionForId, makeSelectEditedCollectionForId,
@ -44,7 +45,8 @@ const select = (state, props) => {
const shuffleList = selectListShuffle(state); const shuffleList = selectListShuffle(state);
const shuffle = shuffleList && shuffleList.collectionId === collectionId && shuffleList.newUrls; const shuffle = shuffleList && shuffleList.collectionId === collectionId && shuffleList.newUrls;
const playNextUri = shuffle && shuffle[0]; const playNextUri = shuffle && shuffle[0];
const lastUsedCollection = selectLastUsedCollection(state); const lastUsedCollectionId = selectLastUsedCollection(state);
const lastUsedCollection = makeSelectCollectionForId(lastUsedCollectionId)(state);
return { return {
claim, claim,
@ -73,12 +75,13 @@ const select = (state, props) => {
resolvedList: makeSelectUrlsForCollectionId(collectionId)(state), resolvedList: makeSelectUrlsForCollectionId(collectionId)(state),
playNextUri, playNextUri,
lastUsedCollection, lastUsedCollection,
hasClaimInLastUsedCollection: hasClaimInLastUsedCollection: makeSelectCollectionForIdHasClaimUrl(
lastUsedCollection && makeSelectCollectionForIdHasClaimUrl(lastUsedCollection.id, contentPermanentUri)(state), lastUsedCollectionId,
contentPermanentUri
)(state),
lastUsedCollectionIsNotBuiltin: lastUsedCollectionIsNotBuiltin:
lastUsedCollection && lastUsedCollectionId !== COLLECTIONS_CONSTS.WATCH_LATER_ID &&
lastUsedCollection.id !== COLLECTIONS_CONSTS.WATCH_LATER_ID && lastUsedCollectionId !== COLLECTIONS_CONSTS.FAVORITES_ID,
lastUsedCollection.id !== COLLECTIONS_CONSTS.FAVORITES_ID,
}; };
}; };

View file

@ -54,7 +54,7 @@ const collectionsReducer = handleActions(
return { return {
...state, ...state,
unpublished: newLists, unpublished: newLists,
lastUsedCollection: newList, lastUsedCollection: params.id,
}; };
}, },
@ -64,7 +64,7 @@ const collectionsReducer = handleActions(
const { edited: editList, unpublished: unpublishedList, pending: pendingList } = state; const { edited: editList, unpublished: unpublishedList, pending: pendingList } = state;
const newEditList = Object.assign({}, editList); const newEditList = Object.assign({}, editList);
const newUnpublishedList = Object.assign({}, unpublishedList); const newUnpublishedList = Object.assign({}, unpublishedList);
const isDeletingLastUsedCollection = lastUsedCollection && lastUsedCollection.id === id; const isDeletingLastUsedCollection = lastUsedCollection === id;
const newPendingList = Object.assign({}, pendingList); const newPendingList = Object.assign({}, pendingList);
@ -118,7 +118,7 @@ const collectionsReducer = handleActions(
edited: newEditList, edited: newEditList,
unpublished: newUnpublishedList, unpublished: newUnpublishedList,
pending: newPendingList, pending: newPendingList,
lastUsedCollection: newPendingList[claimId], lastUsedCollection: claimId,
}; };
}, },
@ -130,7 +130,7 @@ const collectionsReducer = handleActions(
return { return {
...state, ...state,
[collectionKey]: { ...lists, [id]: collection }, [collectionKey]: { ...lists, [id]: collection },
lastUsedCollection: collection, lastUsedCollection: id,
}; };
} }
@ -139,14 +139,14 @@ const collectionsReducer = handleActions(
return { return {
...state, ...state,
edited: { ...lists, [id]: collection }, edited: { ...lists, [id]: collection },
lastUsedCollection: collection, lastUsedCollection: id,
}; };
} }
const { unpublished: lists } = state; const { unpublished: lists } = state;
return { return {
...state, ...state,
unpublished: { ...lists, [id]: collection }, unpublished: { ...lists, [id]: collection },
lastUsedCollection: collection, lastUsedCollection: id,
}; };
}, },
@ -181,7 +181,7 @@ const collectionsReducer = handleActions(
}, },
[ACTIONS.COLLECTION_ITEMS_RESOLVE_COMPLETED]: (state, action) => { [ACTIONS.COLLECTION_ITEMS_RESOLVE_COMPLETED]: (state, action) => {
const { resolvedCollections, failedCollectionIds } = action.data; const { resolvedCollections, failedCollectionIds } = action.data;
const { pending, edited, isResolvingCollectionById, resolved, lastUsedCollection } = state; const { pending, edited, isResolvingCollectionById, resolved } = state;
const newPending = Object.assign({}, pending); const newPending = Object.assign({}, pending);
const newEdited = Object.assign({}, edited); const newEdited = Object.assign({}, edited);
const newResolved = Object.assign({}, resolved, resolvedCollections); const newResolved = Object.assign({}, resolved, resolvedCollections);
@ -208,31 +208,12 @@ const collectionsReducer = handleActions(
}); });
} }
const newAllCollections = [
...Object.values(newPending),
...Object.values(newResolved),
...Object.values(newEdited),
];
let newLastUsedCollection = lastUsedCollection;
// If a collection is being published or got published,
// its id will get updated which means, we have to sync
// the last used collection.
if (lastUsedCollection) {
newLastUsedCollection = newAllCollections.find((collection) => {
// $FlowFixMe
return collection.name === lastUsedCollection.name;
});
}
return Object.assign({}, state, { return Object.assign({}, state, {
...state, ...state,
pending: newPending, pending: newPending,
resolved: newResolved, resolved: newResolved,
edited: newEdited, edited: newEdited,
isResolvingCollectionById: newResolving, isResolvingCollectionById: newResolving,
lastUsedCollection: newLastUsedCollection,
}); });
}, },
[ACTIONS.COLLECTION_ITEMS_RESOLVE_FAILED]: (state, action) => { [ACTIONS.COLLECTION_ITEMS_RESOLVE_FAILED]: (state, action) => {