Skip to content

Commit a40d8ca

Browse files
committed
Fix Shopify pagination selection persistence
1 parent 94cd29f commit a40d8ca

File tree

4 files changed

+54
-9
lines changed

4 files changed

+54
-9
lines changed

src/Umbraco.Cms.Integrations.Commerce.Shopify/Client/src/modal/shopify-products-modal.element.ts

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ export default class ShopifyProductsModalElement extends UmbModalBaseElement<Sho
9898
},
9999
];
100100

101+
private _selectedItems: Array<string | null> = [];
102+
private _selectedProducts: Array<ProductDtoModel> = [];
103+
101104
constructor() {
102105
super();
103106

@@ -215,7 +218,9 @@ export default class ShopifyProductsModalElement extends UmbModalBaseElement<Sho
215218
}
216219

217220
async #loadSelectionItems() {
218-
this._selection = this.data!.selectedItemIdList;
221+
this._selection = this._selectedItems.length > 0
222+
? this._selectedItems
223+
: this.data!.selectedItemIdList;
219224
this._addUpToItems = (this.data?.config?.maxItems ? this.data?.config?.maxItems : 0) - (this.data?.config?.minItems ? this.data?.config?.minItems : 0);
220225
}
221226

@@ -227,27 +232,64 @@ export default class ShopifyProductsModalElement extends UmbModalBaseElement<Sho
227232
this.#onEventRun(event);
228233
}
229234

230-
#onEventRun(event: UmbTableSelectedEvent | UmbTableDeselectedEvent){
235+
#onEventRun(event: UmbTableSelectedEvent | UmbTableDeselectedEvent) {
236+
231237
event.stopPropagation();
232238
const table = event.target as UmbTableElement;
233239
const selection = table.selection;
234240
const items = table.items;
235-
this.#collectionContext?.selection.setSelection(selection);
241+
242+
this.saveSelectedItems(items, selection);
243+
244+
this.#collectionContext?.selection.setSelection(selection);
236245

237246
this.#getSelectedProduct(selection, items);
238247
this._numberOfSelection = selection.length;
239248
}
240249

250+
private saveSelectedItems(items: UmbTableItem[], selection: string[]) {
251+
// remove current table view items from the selected array to cover the deselect action.
252+
this._selectedItems = this._selectedItems.filter(obj => {
253+
if (!items.some(item => item.id == obj)) {
254+
return obj;
255+
}
256+
});
257+
selection.forEach(obj => {
258+
if (this._selectedItems.indexOf(obj) == -1) {
259+
this._selectedItems.push(obj);
260+
}
261+
});
262+
}
263+
241264
#getSelectedProduct(selectedRows: Array<string>, allRows: Array<UmbTableItem>){
242265
let lst: Array<UmbTableItem[]> = [];
243266
selectedRows.forEach(selectedRow => {
244267
const selectedProduct = allRows.filter(r => r.id == selectedRow);
245-
lst.push(selectedProduct);
268+
if (selectedProduct && selectedProduct.length > 0) {
269+
lst.push(selectedProduct);
270+
}
246271
});
247272

248273
let lstData = lst.map(l => l[0].data);
249274
let lstId = lst.map(l => l[0].id);
250275
this._modalSelectedProducts = this.#mapToDto(lstData, lstId);
276+
277+
this.saveSelectedProducts(allRows);
278+
}
279+
280+
private saveSelectedProducts(allRows: Array<UmbTableItem>) {
281+
// clear items of current table view
282+
this._selectedProducts = this._selectedProducts.filter(obj => {
283+
if (!allRows.some(row => row.id == obj.id.toString())) {
284+
return obj;
285+
}
286+
});
287+
288+
this._modalSelectedProducts.forEach(obj => {
289+
if (!this._selectedProducts.some(product => product.id == obj.id)) {
290+
this._selectedProducts.push(obj);
291+
}
292+
});
251293
}
252294

253295
#mapToDto(lstData: UmbTableItemData[][], lstId: string[]){
@@ -280,7 +322,7 @@ export default class ShopifyProductsModalElement extends UmbModalBaseElement<Sho
280322
this._rejectModal();
281323
}
282324

283-
this.value = {productList: this._modalSelectedProducts};
325+
this.value = { productList: this._selectedProducts };
284326
this._submitModal();
285327
}
286328
}

src/Umbraco.Cms.Integrations.Commerce.Shopify/Client/src/property-editor/shopify-product-picker-property-editor.element.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export class ShopifyProductPickerPropertyEditor extends UmbLitElement implements
123123

124124
deleteProduct(id: number) {
125125
var index = this.products.map(p => p.id).indexOf(id);
126-
this.products.splice(index);
126+
this.products = this.products.filter(product => product.id != id);
127127
this.value = JSON.stringify(this.products.map(product => product.id));
128128
this.dispatchEvent(new CustomEvent('property-value-change'));
129129
}

src/Umbraco.Cms.Integrations.Commerce.Shopify/Services/ShopifyService.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,11 @@ public async Task<ResponseDto<ProductsListDto>> GetResults(string pageInfo)
144144
};
145145

146146
var pageInfoDetails = response.GetPageInfo();
147-
responseDto.PreviousPageInfo = pageInfoDetails.Item1;
148-
responseDto.NextPageInfo = pageInfoDetails.Item2;
147+
if (pageInfoDetails != null)
148+
{
149+
responseDto.PreviousPageInfo = pageInfoDetails.Item1;
150+
responseDto.NextPageInfo = pageInfoDetails.Item2;
151+
}
149152

150153
return responseDto;
151154
}

src/Umbraco.Cms.Integrations.Commerce.Shopify/Umbraco.Cms.Integrations.Commerce.Shopify.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<PackageIconUrl></PackageIconUrl>
1717
<PackageProjectUrl>https://github.com/umbraco/Umbraco.Cms.Integrations/tree/main-v14/src/Umbraco.Cms.Integrations.Commerce.Shopify</PackageProjectUrl>
1818
<RepositoryUrl>https://github.com/umbraco/Umbraco.Cms.Integrations</RepositoryUrl>
19-
<Version>2.0.0</Version>
19+
<Version>2.0.1</Version>
2020
<Authors>Umbraco HQ</Authors>
2121
<Company>Umbraco</Company>
2222
<PackageIcon>shopify.png</PackageIcon>

0 commit comments

Comments
 (0)