Skip to content

Commit

Permalink
Improve collection edit performance (#3185)
Browse files Browse the repository at this point in the history
* Collection edit: Fetch collection items only if not fetched before

* Get rid of some dispatches on collection edit

---------

Co-authored-by: miko <sauce47@posteo.net>
  • Loading branch information
keikari and miko authored Nov 21, 2024
1 parent 6d556f5 commit cfdca7a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
15 changes: 6 additions & 9 deletions ui/redux/actions/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -566,9 +566,12 @@ export const doCollectionEdit =
const isPrivateVersion = selectHasPrivateCollectionForId(state, collectionId);
const { uris, remove, replace, order, type, isPreview } = params;

await dispatch(doFetchItemsInCollection({ collectionId }));
state = getState();
const hasItemsResolved = selectCollectionHasItemsResolvedForId(state, collectionId);
let hasItemsResolved = selectCollectionHasItemsResolvedForId(state, collectionId);
if (!hasItemsResolved && !isPrivateVersion) {
await dispatch(doFetchItemsInCollection({ collectionId }));
state = getState();
hasItemsResolved = selectCollectionHasItemsResolvedForId(state, collectionId);
}
if (!hasItemsResolved && !isPrivateVersion) {
return dispatch(doToast({ message: __('Failed to resolve collection items. Please try again.'), isError: true }));
}
Expand Down Expand Up @@ -605,8 +608,6 @@ export const doCollectionEdit =
currentUrls.splice(order.to, 0, movedItem);
}

await dispatch(doRemoveFromUpdatedCollectionsForCollectionId(collectionId));

const isQueue = collectionId === COLS.QUEUE_ID;
const title = params.title || params.name;

Expand All @@ -632,10 +633,6 @@ export const doCollectionEdit =
},
},
});
// Needs to be run after collection_edit is dispatched, or saving changes doesn't work from edit page
if (!isPreview) {
dispatch(doRemoveFromUnsavedChangesCollectionsForCollectionId(collectionId));
}
success();
});
};
Expand Down
12 changes: 11 additions & 1 deletion ui/redux/reducers/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,21 @@ const collectionsReducer = handleActions(
const newCollection = Object.assign({}, collection);
newCollection.updatedAt = getCurrentTimeInSec();

return {
const newState = {
...state,
[collectionKey]: { ...currentCollections, [id]: newCollection },
lastUsedCollection: id,
};

// Remove un-wanted versions of the list
[COLS.KEYS.UPDATED, COLS.KEYS.UNSAVED_CHANGES].forEach((key) => {
if (collectionKey !== key) {
const { [id]: _, ...remainingCollections } = state[key];
newState[key] = remainingCollections;
}
});

return newState;
},

[ACTIONS.USER_STATE_POPULATE]: (state, action) => {
Expand Down

0 comments on commit cfdca7a

Please sign in to comment.