From b987a3d762f2476ca445d722f75ced5280286ed3 Mon Sep 17 00:00:00 2001 From: Kristof De Langhe Date: Thu, 6 Feb 2020 11:19:20 +0100 Subject: [PATCH] 68346: Fixed moving objects within paginated list --- .../item-edit-bitstream-bundle.component.ts | 4 +-- .../object-updates/object-updates.reducer.ts | 35 +++++++++++++++---- .../object-updates/object-updates.service.ts | 2 +- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/app/+item-page/edit-item-page/item-bitstreams/item-edit-bitstream-bundle/item-edit-bitstream-bundle.component.ts b/src/app/+item-page/edit-item-page/item-bitstreams/item-edit-bitstream-bundle/item-edit-bitstream-bundle.component.ts index baff48ce5c..906bebc6c7 100644 --- a/src/app/+item-page/edit-item-page/item-bitstreams/item-edit-bitstream-bundle/item-edit-bitstream-bundle.component.ts +++ b/src/app/+item-page/edit-item-page/item-bitstreams/item-edit-bitstream-bundle/item-edit-bitstream-bundle.component.ts @@ -96,7 +96,7 @@ export class ItemEditBitstreamBundleComponent implements OnInit { // No updates have been initialized yet for this bundle, initialize the first page this.objectUpdatesService.initializeWithCustomOrder(this.bundle.self, bitstreams, new Date(), this.batchSize, updatesPage); this.initializedPages.push(updatesPage); - } else if (this.initializedPages.indexOf(this.currentPage$.value) < 0) { + } else if (this.initializedPages.indexOf(updatesPage) < 0) { // Updates were initialized for this bundle, but not the page we're on. Add the current page to the field-update store for this bundle this.objectUpdatesService.addPageToCustomOrder(this.bundle.self, bitstreams, updatesPage); this.initializedPages.push(updatesPage); @@ -121,6 +121,6 @@ export class ItemEditBitstreamBundleComponent implements OnInit { * @param event */ drop(event: CdkDragDrop) { - this.objectUpdatesService.saveMoveFieldUpdate(this.bundle.self, event.previousIndex, event.currentIndex); + this.objectUpdatesService.saveMoveFieldUpdate(this.bundle.self, event.previousIndex, event.currentIndex, this.currentPage$.value - 1, this.currentPage$.value - 1); } } diff --git a/src/app/core/data/object-updates/object-updates.reducer.ts b/src/app/core/data/object-updates/object-updates.reducer.ts index 47756c58d3..1e1bb6cd11 100644 --- a/src/app/core/data/object-updates/object-updates.reducer.ts +++ b/src/app/core/data/object-updates/object-updates.reducer.ts @@ -169,7 +169,7 @@ function initializeFieldsUpdate(state: any, action: InitializeFieldsAction) { { customOrder: { initialOrderPages: initialOrderPages, newOrderPages: initialOrderPages, - pageSize: 9999, + pageSize: pageSize, changed: false } } ); @@ -184,11 +184,12 @@ function initializeFieldsUpdate(state: any, action: InitializeFieldsAction) { function addPageToCustomOrder(state: any, action: AddPageToCustomOrderAction) { const url: string = action.payload.url; const fields: Identifiable[] = action.payload.fields; + const fieldStates = createInitialFieldStates(fields); const order = action.payload.order; const page = action.payload.page; const pageState: ObjectUpdatesEntry = state[url] || {}; const newPageState = Object.assign({}, pageState, { - fieldStates: Object.assign({}, pageState.fieldStates, fields), + fieldStates: Object.assign({}, pageState.fieldStates, fieldStates), customOrder: Object.assign({}, pageState.customOrder, { newOrderPages: addOrderToPages(pageState.customOrder.newOrderPages, order, pageState.customOrder.pageSize, page), initialOrderPages: addOrderToPages(pageState.customOrder.initialOrderPages, order, pageState.customOrder.pageSize, page) @@ -453,16 +454,39 @@ function moveFieldUpdate(state: any, action: MoveFieldUpdateAction) { const pageState: ObjectUpdatesEntry = state[url]; const initialOrderPages = pageState.customOrder.initialOrderPages; const customOrderPages = [...pageState.customOrder.newOrderPages]; + + // Create a copy of the custom orders for the from- and to-pages + const fromPageOrder = [...customOrderPages[fromPage].order]; + const toPageOrder = [...customOrderPages[toPage].order]; if (fromPage === toPage) { if (isNotEmpty(customOrderPages[fromPage]) && isNotEmpty(customOrderPages[fromPage].order[fromIndex]) && isNotEmpty(customOrderPages[fromPage].order[toIndex])) { - moveItemInArray(customOrderPages[fromPage].order, fromIndex, toIndex); + // Move an item from one index to another within the same page + moveItemInArray(fromPageOrder, fromIndex, toIndex); + // Update the custom order for this page + customOrderPages[fromPage] = { order: fromPageOrder }; } } else { if (isNotEmpty(customOrderPages[fromPage]) && isNotEmpty(customOrderPages[toPage]) && isNotEmpty(customOrderPages[fromPage].order[fromIndex]) && isNotEmpty(customOrderPages[toPage].order[toIndex])) { - transferArrayItem(customOrderPages[fromPage].order, customOrderPages[toPage].order, fromIndex, toIndex); + // Move an item from one index of one page to an index in another page + transferArrayItem(fromPageOrder, toPageOrder, fromIndex, toIndex); + // Update the custom order for both pages + customOrderPages[fromPage] = { order: fromPageOrder }; + customOrderPages[toPage] = { order: toPageOrder }; } } + // Update the store's state with new values and return + return Object.assign({}, state, { [url]: Object.assign({}, pageState, { + customOrder: Object.assign({}, pageState.customOrder, { newOrderPages: customOrderPages, changed: checkForOrderChanges(initialOrderPages, customOrderPages) }) + })}) +} + +/** + * Compare two lists of OrderPage objects and return whether there's at least one change in the order of objects within + * @param initialOrderPages The initial list of OrderPages + * @param customOrderPages The changed list of OrderPages + */ +function checkForOrderChanges(initialOrderPages: OrderPage[], customOrderPages: OrderPage[]) { let changed = false; initialOrderPages.forEach((orderPage: OrderPage, page: number) => { if (isNotEmpty(orderPage) && isNotEmpty(orderPage.order) && isNotEmpty(customOrderPages[page]) && isNotEmpty(customOrderPages[page].order)) { @@ -477,8 +501,7 @@ function moveFieldUpdate(state: any, action: MoveFieldUpdateAction) { } } }); - - return Object.assign({}, state, { [url]: Object.assign({}, pageState, { customOrder: Object.assign({}, pageState.customOrder, { newOrderPages: customOrderPages, changed: changed }) }) }) + return changed; } /** diff --git a/src/app/core/data/object-updates/object-updates.service.ts b/src/app/core/data/object-updates/object-updates.service.ts index 6e1d8d1fc1..3d0653e147 100644 --- a/src/app/core/data/object-updates/object-updates.service.ts +++ b/src/app/core/data/object-updates/object-updates.service.ts @@ -162,7 +162,7 @@ export class ObjectUpdatesService { const objectUpdates = this.getObjectEntry(url); return objectUpdates.pipe(map((objectEntry) => { const fieldUpdates: FieldUpdates = {}; - if (hasValue(objectEntry)) { + if (hasValue(objectEntry) && hasValue(objectEntry.customOrder) && isNotEmpty(objectEntry.customOrder.newOrderPages) && page < objectEntry.customOrder.newOrderPages.length) { for (const uuid of objectEntry.customOrder.newOrderPages[page].order) { let fieldUpdate = objectEntry.fieldUpdates[uuid]; if (isEmpty(fieldUpdate)) {