Skip to content

Commit bb0d1df

Browse files
committed
Add products pagination
1 parent 4a31afc commit bb0d1df

File tree

4 files changed

+89
-14
lines changed

4 files changed

+89
-14
lines changed

src/Umbraco.Cms.Integrations.Commerce.Shopify/Api/Management/Controllers/GetListController.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ public GetListController(IOptions<ShopifySettings> shopifySettings, IShopifyServ
2121
public async Task<IActionResult> GetList(string? pageInfo)
2222
{
2323
var result = await ShopifyService.GetResults(pageInfo);
24-
result.Skip = 0;
25-
result.Take = 5;
2624
return Ok(result);
2725
}
2826
}

src/Umbraco.Cms.Integrations.Commerce.Shopify/Client/src/context/shopify.context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export class ShopifyContext extends UmbControllerBase{
4040
return await this.#repository.revokeAccessToken();
4141
}
4242

43-
async getList(pageInfo: string){
43+
async getList(pageInfo?: string){
4444
return await this.#repository.getList(pageInfo);
4545
}
4646

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

Lines changed: 87 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import { UmbModalBaseElement } from "@umbraco-cms/backoffice/modal";
22
import { SHOPIFY_CONTEXT_TOKEN } from "../context/shopify.context.js";
33
import { UMB_NOTIFICATION_CONTEXT } from "@umbraco-cms/backoffice/notification";
4-
import { html, state, customElement, css } from "@umbraco-cms/backoffice/external/lit";
4+
import { html, state, customElement, css, nothing } from "@umbraco-cms/backoffice/external/lit";
55
import type { EditorSettingsModel, ProductDtoModel } from "../../generated";
66
import type { ShopifyProductPickerModalData, ShopifyProductPickerModalValue } from "./shopify.modal-token.js";
77
import type { ShopifyServiceStatus } from "../models/shopify-service.model.js";
88
import type { UmbTableColumn, UmbTableConfig, UmbTableItem, UmbTableSelectedEvent, UmbTableElement, UmbTableDeselectedEvent, UmbTableItemData } from '@umbraco-cms/backoffice/components';
99
import type { ShopifyCollectionModel } from "../types/types.js";
10-
import type { UmbDefaultCollectionContext } from '@umbraco-cms/backoffice/collection';
11-
import { UMB_COLLECTION_CONTEXT } from '@umbraco-cms/backoffice/collection';
10+
import type { UmbDefaultCollectionContext } from "@umbraco-cms/backoffice/collection";
11+
import { UMB_COLLECTION_CONTEXT } from "@umbraco-cms/backoffice/collection";
12+
import { UmbPaginationManager } from "@umbraco-cms/backoffice/utils";
13+
import type { UUIPaginationEvent } from "@umbraco-cms/backoffice/external/uui";
1214

1315
const elementName = "shopify-products-modal";
1416

@@ -17,10 +19,23 @@ export default class ShopifyProductsModalElement extends UmbModalBaseElement<Sho
1719
#shopifyContext!: typeof SHOPIFY_CONTEXT_TOKEN.TYPE;
1820
#settingsModel?: EditorSettingsModel;
1921
#collectionContext!: UmbDefaultCollectionContext<ShopifyCollectionModel>;
22+
#paginationManager = new UmbPaginationManager();
2023
_modalSelectedProducts: Array<ProductDtoModel> = [];
2124
_numberOfSelection: number = 0;
2225
_addUpToItems: number = 0;
2326
_selectionIdList: Array<string | null> = [];
27+
28+
@state()
29+
_currentPageNumber = 1;
30+
31+
@state()
32+
_totalPages = 1;
33+
34+
@state()
35+
_nextPageInfo?: string;
36+
37+
@state()
38+
_previousPageInfo?: string;
2439

2540
@state()
2641
private _selection: Array<string | null> = [];
@@ -101,7 +116,7 @@ export default class ShopifyProductsModalElement extends UmbModalBaseElement<Sho
101116
(selection) => (this._selection = selection),
102117
'umbCollectionSelectionObserver',
103118
);
104-
});
119+
});
105120
}
106121

107122
async connectedCallback() {
@@ -119,25 +134,49 @@ export default class ShopifyProductsModalElement extends UmbModalBaseElement<Sho
119134
useOAuth: this.#settingsModel.isValid && this.#settingsModel.type.value === "OAuth"
120135
}
121136

122-
await this.#loadProducts();
137+
if (!this._serviceStatus.isValid) {
138+
this._showError("Invalid Shopify API Configuration");
139+
return;
140+
}
141+
142+
await this.#getTotalPages();
143+
await this.#loadProducts("");
123144
}
124145

125-
async #loadProducts() {
146+
async #loadProducts(pageInfo?: string) {
147+
await this.#getTotalPages();
148+
126149
this._loading = true;
127-
const { data } = await this.#shopifyContext.getList("");
150+
const { data } = await this.#shopifyContext.getList(pageInfo);
128151
if (!data) return;
129152

153+
if (!data.isValid) {
154+
this._showError("Cannot access Shopify API.");
155+
this._loading = false;
156+
return;
157+
}
158+
130159
this._products = data.result.products ?? [];
131160
this._loading = false;
132161

133162
if (!data.isValid || data.isExpired) {
134163
this._showError("Data is invalid or expired."!);
135164
}
136165

166+
this._nextPageInfo = data.nextPageInfo;
167+
this._previousPageInfo = data.previousPageInfo;
168+
137169
this.#createTableItems(this._products);
138170
this.#loadSelectionItems();
139171
}
140172

173+
async #getTotalPages() {
174+
const { data } = await this.#shopifyContext.getTotalPages();
175+
if (!data) return;
176+
177+
this._totalPages = data;
178+
}
179+
141180
#createTableItems(products: Array<ProductDtoModel>) {
142181
this._tableItems = products.map((product) => {
143182
return {
@@ -246,6 +285,17 @@ export default class ShopifyProductsModalElement extends UmbModalBaseElement<Sho
246285
}
247286
}
248287

288+
#onPageChange(event: UUIPaginationEvent) {
289+
const forward = event.target?.current > this._currentPageNumber;
290+
291+
const currentPageNumber = forward ? this._currentPageNumber + 1 : this._currentPageNumber - 1
292+
293+
this.#paginationManager.setCurrentPageNumber(currentPageNumber);
294+
295+
this._currentPageNumber = currentPageNumber;
296+
this.#loadProducts(forward ? this._nextPageInfo : this._previousPageInfo);
297+
}
298+
249299
private async _showError(message: string) {
250300
const notificationContext = await this.getContext(UMB_NOTIFICATION_CONTEXT);
251301
notificationContext?.peek("danger", {
@@ -257,15 +307,16 @@ export default class ShopifyProductsModalElement extends UmbModalBaseElement<Sho
257307
return html`
258308
<umb-body-layout>
259309
<uui-box headline=${this.data!.headline}>
260-
${this._loading ? html`<div class="center"><uui-loader></uui-loader></div>` : ""}
310+
${this._loading ? html`<div class="center loader"><uui-loader></uui-loader></div>` : ""}
261311
<umb-table
262312
.config=${this._tableConfig}
263313
.columns=${this._tableColumns}
264314
.items=${this._tableItems}
265315
.selection=${this._selection}
266316
@selected="${this.#onSelected}"
267-
@deselected="${this.#onDeselected}"></umb-table>
268-
317+
@deselected="${this.#onDeselected}">
318+
</umb-table>
319+
${this.#renderPagination()}
269320
</uui-box>
270321
271322
<div class="maximum-selection">
@@ -280,10 +331,36 @@ export default class ShopifyProductsModalElement extends UmbModalBaseElement<Sho
280331
`;
281332
}
282333

334+
#renderPagination() {
335+
return html`
336+
${this._totalPages > 1
337+
? html`
338+
<div class="shopify-pagination">
339+
<uui-pagination
340+
class="pagination"
341+
.current=${this._currentPageNumber}
342+
.total=${this._totalPages}
343+
@change=${this.#onPageChange}></uui-pagination>
344+
</div>
345+
`
346+
: nothing}
347+
`;
348+
}
349+
283350
static styles = [css`
351+
.loader {
352+
display: flex;
353+
justify-content: center;
354+
}
284355
.maximum-selection{
285356
margin-top: 10px;
286357
font-weight: bold;
287358
}
359+
.shopify-pagination {
360+
width: 50%;
361+
margin-top: 10px;
362+
margin-left: auto;
363+
margin-right: auto;
364+
}
288365
`];
289366
}

src/Umbraco.Cms.Integrations.Commerce.Shopify/Client/src/repository/shopify.repository.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class ShopifyRepository extends UmbControllerBase {
4848
return { data };
4949
}
5050

51-
async getList(pageInfo: string){
51+
async getList(pageInfo?: string){
5252
const { data, error } = await tryExecuteAndNotify(this, ShopifyService.getList({pageInfo: pageInfo}));
5353

5454
if (error || !data) {

0 commit comments

Comments
 (0)