Skip to content

Commit

Permalink
Prevent editing collection while all items aren't resolved (#3061)
Browse files Browse the repository at this point in the history
Co-authored-by: miko <[email protected]>
  • Loading branch information
keikari and miko authored Feb 20, 2024
1 parent b3e18b8 commit 6c1b5e3
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions ui/redux/actions/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ import {
selectClaimForId,
makeSelectMetadataItemForUri,
selectHasClaimForId,
selectResolvingIds,
selectResolvingUris,
} from 'redux/selectors/claims';
import {
selectCollectionForId,
selectResolvedCollectionForId,
selectCollectionHasItemsResolvedForId,
selectHasPrivateCollectionForId,
selectIsCollectionPrivateForId,
selectUrlsForCollectionId,
Expand Down Expand Up @@ -243,7 +246,7 @@ export const doToggleCollectionSavedForId = (collectionId: string) => (dispatch:
dispatch({ type: ACTIONS.COLLECTION_TOGGLE_SAVE, data: collectionId });
};

const doFetchCollectionItems = (items: Array<any>, pageSize?: number) => async (dispatch: Dispatch) => {
const doFetchCollectionItems = (items: Array<any>, pageSize?: number) => async (dispatch: Dispatch, getState: GetState) => {
const sortResults = (resultItems: Array<Claim>) => {
const newItems: Array<Claim> = [];

Expand All @@ -269,6 +272,7 @@ const doFetchCollectionItems = (items: Array<any>, pageSize?: number) => async (
};

try {
const state = getState();
const batchSize = pageSize || FETCH_BATCH_SIZE;
const uriBatches: Array<Promise<any>> = [];
const idBatches: Array<Promise<any>> = [];
Expand Down Expand Up @@ -303,7 +307,17 @@ const doFetchCollectionItems = (items: Array<any>, pageSize?: number) => async (
const resultItems = sortResults(mergeBatches(itemsInBatches.filter(Boolean)));

// The resolve calls will NOT return items when they still are in a previous call's 'Processing' state.
const itemsWereFetching = resultItems.length !== items.length;
let itemsWereFetching = resultItems.length !== items.length;

// Related to above. Collection with deleted items would never get "resolved: true" status.
// Which is needed to avoid issues when editing list before all items are resolved. (Not resolved items get removed.)
if (itemsWereFetching) {
const resolvingIds = selectResolvingIds(state);
const resolvingUris = selectResolvingUris(state);
if (resolvingIds.length === 0 && resolvingUris.length === 0) {
itemsWereFetching = false;
}
}

if (resultItems && !itemsWereFetching) {
return resultItems;
Expand Down Expand Up @@ -522,13 +536,15 @@ export const doCollectionEdit =

const { uris, remove, replace, order, type, isPreview } = params;

let collectionUrls = selectUrlsForCollectionId(state, collectionId);
if (collectionUrls === undefined) {
await dispatch(doFetchItemsInCollection({ collectionId }));
state = getState();
collectionUrls = selectUrlsForCollectionId(state, collectionId);
await dispatch(doFetchItemsInCollection({ collectionId }));
state = getState();
const hasItemsResolved = selectCollectionHasItemsResolvedForId(state, collectionId);
if (!hasItemsResolved) {
return dispatch(doToast({ message: __('Failed to resolve collection items. Please try again.'), isError: true }));
}

const collectionUrls = selectUrlsForCollectionId(state, collectionId);

const currentUrls = collectionUrls ? collectionUrls.concat() : [];
const currentUrlsSet = new Set(currentUrls);
let newItems = currentUrls;
Expand Down

0 comments on commit 6c1b5e3

Please sign in to comment.