From 5951ed1e600c1b16f01e9ff0bac9e9b39a66b88b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=20Jurmanovi=C4=87?= Date: Wed, 2 Jun 2021 09:06:45 +0200 Subject: [PATCH] created basic pagination --- .../app-pagination/AppPaginationElement.ts | 131 ++++++++++++++++++ src/components/index.ts | 5 +- src/pages/history-page/HistoryPageElement.ts | 39 ++---- 3 files changed, 143 insertions(+), 32 deletions(-) create mode 100644 src/components/app-pagination/AppPaginationElement.ts diff --git a/src/components/app-pagination/AppPaginationElement.ts b/src/components/app-pagination/AppPaginationElement.ts new file mode 100644 index 0000000..f9f59d1 --- /dev/null +++ b/src/components/app-pagination/AppPaginationElement.ts @@ -0,0 +1,131 @@ +import { attr, controller, target } from "@github/catalyst"; +import { html, render } from "@github/jtml"; +import { AppMainElement } from "components/app-main/AppMainElement"; +import { closest, isTrue } from "core/utils"; + +@controller +class AppPaginationElement extends HTMLElement { + @closest appMain: AppMainElement; + public items: Array; + @attr page: number; + @attr rpp: number; + @attr totalItems: number; + @attr autoInit: string; + fetchFunc: Function = () => {}; + constructor() { + super(); + } + + connectedCallback() {} + + attributeChangedCallback() { + this.update(); + } + + setItems = (items) => { + this.items = items; + this.update(); + }; + + setFetchFunc = async (fetchFunc: Function, autoInit?) => { + this.fetchFunc = fetchFunc; + if (autoInit) { + const options = { + rpp: this.rpp || 5, + page: this.page || 1, + }; + this.executeFetch(options); + } + }; + + executeFetch = async (options?) => { + if (!options) { + options = { + rpp: this.rpp || 5, + page: this.page || 1, + }; + } + + try { + const response = await this.fetchFunc(options); + this.setItems(response?.items); + this.totalItems = response?.totalRecords; + this.page = response?.page; + this.rpp = response?.rpp; + } catch (err) { + console.error(err); + } + }; + + pageBack = () => { + const { page } = this; + if (page > 1) { + this.page--; + this.executeFetch(); + } + }; + + pageNext = () => { + const { rpp, totalItems, page } = this; + const pageRange = Math.ceil(totalItems / rpp); + if (page < pageRange) { + this.page++; + this.executeFetch(); + } + }; + + render = () => { + const { rpp, totalItems, page, items } = this; + + const renderItem = (item) => html` + ${item.description} + ${item.amount} + `; + + const renderItems = () => { + if (items?.length > 0) { + return html`${items?.map((item) => renderItem(item))}`; + } + return html``; + }; + + const renderPagination = () => { + if (totalItems > items?.length) { + const pageRange = Math.ceil(totalItems / rpp); + return html` +
+ + +
+ `; + } + }; + + return html`
+ + ${renderItems()} +
+ ${renderPagination()} +
`; + }; + + update = () => { + render(this.render(), this); + }; +} + +export type { AppPaginationElement }; diff --git a/src/components/index.ts b/src/components/index.ts index 7deca3a..9502f3c 100644 --- a/src/components/index.ts +++ b/src/components/index.ts @@ -1,9 +1,10 @@ -export * from "./app-shadow/AppShadowElement"; export * from "./app-link/AppLinkElement"; export * from "./menu-item/MenuItemElement"; -export * from "./app-main/AppMainElement"; +export * from "./app-pagination/AppPaginationElement"; export * from "./app-modal/AppModalElement"; export * from "./app-root/AppRootElement"; export * from "./app-slot/AppSlotElement"; export * from "./app-menu/AppMenuElement"; export * from "./input-field/InputFieldElement"; +export * from "./app-main/AppMainElement"; +export * from "./app-shadow/AppShadowElement"; diff --git a/src/pages/history-page/HistoryPageElement.ts b/src/pages/history-page/HistoryPageElement.ts index b21994e..d2cba12 100644 --- a/src/pages/history-page/HistoryPageElement.ts +++ b/src/pages/history-page/HistoryPageElement.ts @@ -2,13 +2,13 @@ import { attr, targets, controller, target } from "@github/catalyst"; import { closest, index, update, isTrue } from "core/utils"; import { html, render, until } from "@github/jtml"; import { TransactionsService } from "services/"; -import { AppMainElement } from "components/"; +import { AppMainElement, AppPaginationElement } from "components/"; @controller class HistoryPageElement extends HTMLElement { private transactionsService: TransactionsService; - private transactions: Array = []; @closest appMain: AppMainElement; + @target pagination: AppPaginationElement; constructor() { super(); } @@ -17,8 +17,8 @@ class HistoryPageElement extends HTMLElement { this.transactionsService = new TransactionsService( this.appMain?.appService ); - if (this.appMain.isAuth) this.getTransactions(); this.update(); + this.pagination?.setFetchFunc?.(this.getTransactions, true)!; window.addEventListener("tokenchange", this.update); } @@ -26,41 +26,20 @@ class HistoryPageElement extends HTMLElement { window.removeEventListener("tokenchange", this.update); } - getTransactions = async () => { + getTransactions = async (options) => { try { - const response = await this.transactionsService.getAll(); - if (response) { - this.setTransactions(response?.items); - } + const response = await this.transactionsService.getAll(options); + return response; } catch (err) { throw err; } }; - setTransactions(transactions: Array) { - this.transactions = transactions; - console.log(transactions); - this.update(); - } - - openModal = () => { - const _modal = this.appMain.appModal; - if (_modal) { - this.appMain.closeModal(); - } else { - this.appMain.createModal("login-page"); - } - }; - render = () => { return html` -
    - ${this.transactions - ? this.transactions.map((transaction) => { - return html`
  • ${transaction.description}
  • `; - }) - : null} -
+ `; };