mirror of
https://github.com/FJurmanovic/wallet-web.git
synced 2026-02-06 06:08:10 +00:00
Merge branch 'feature/WW-26-architecture'
This commit is contained in:
131
src/components/app-pagination/AppPaginationElement.ts
Normal file
131
src/components/app-pagination/AppPaginationElement.ts
Normal file
@@ -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<any>;
|
||||||
|
@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`<tr>
|
||||||
|
<td>${item.description}</td>
|
||||||
|
<td>${item.amount}</td>
|
||||||
|
</tr>`;
|
||||||
|
|
||||||
|
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`
|
||||||
|
<div>
|
||||||
|
<button
|
||||||
|
class="btn btn-blue btn-squared ${page <= 1
|
||||||
|
? "disabled"
|
||||||
|
: ""}"
|
||||||
|
data-action="click:app-pagination#pageBack"
|
||||||
|
>
|
||||||
|
Prev
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
class="btn btn-blue btn-squared ${page >= pageRange
|
||||||
|
? "disabled"
|
||||||
|
: ""}"
|
||||||
|
data-action="click:app-pagination#pageNext"
|
||||||
|
>
|
||||||
|
Next
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return html`<div>
|
||||||
|
<table>
|
||||||
|
${renderItems()}
|
||||||
|
</table>
|
||||||
|
${renderPagination()}
|
||||||
|
</div>`;
|
||||||
|
};
|
||||||
|
|
||||||
|
update = () => {
|
||||||
|
render(this.render(), this);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export type { AppPaginationElement };
|
||||||
@@ -1,9 +1,10 @@
|
|||||||
export * from "./app-shadow/AppShadowElement";
|
|
||||||
export * from "./app-link/AppLinkElement";
|
export * from "./app-link/AppLinkElement";
|
||||||
export * from "./menu-item/MenuItemElement";
|
export * from "./menu-item/MenuItemElement";
|
||||||
export * from "./app-main/AppMainElement";
|
export * from "./app-pagination/AppPaginationElement";
|
||||||
export * from "./app-modal/AppModalElement";
|
export * from "./app-modal/AppModalElement";
|
||||||
export * from "./app-root/AppRootElement";
|
export * from "./app-root/AppRootElement";
|
||||||
export * from "./app-slot/AppSlotElement";
|
export * from "./app-slot/AppSlotElement";
|
||||||
export * from "./app-menu/AppMenuElement";
|
export * from "./app-menu/AppMenuElement";
|
||||||
export * from "./input-field/InputFieldElement";
|
export * from "./input-field/InputFieldElement";
|
||||||
|
export * from "./app-main/AppMainElement";
|
||||||
|
export * from "./app-shadow/AppShadowElement";
|
||||||
|
|||||||
@@ -2,13 +2,13 @@ import { attr, targets, controller, target } from "@github/catalyst";
|
|||||||
import { closest, index, update, isTrue } from "core/utils";
|
import { closest, index, update, isTrue } from "core/utils";
|
||||||
import { html, render, until } from "@github/jtml";
|
import { html, render, until } from "@github/jtml";
|
||||||
import { TransactionsService } from "services/";
|
import { TransactionsService } from "services/";
|
||||||
import { AppMainElement } from "components/";
|
import { AppMainElement, AppPaginationElement } from "components/";
|
||||||
|
|
||||||
@controller
|
@controller
|
||||||
class HistoryPageElement extends HTMLElement {
|
class HistoryPageElement extends HTMLElement {
|
||||||
private transactionsService: TransactionsService;
|
private transactionsService: TransactionsService;
|
||||||
private transactions: Array<any> = [];
|
|
||||||
@closest appMain: AppMainElement;
|
@closest appMain: AppMainElement;
|
||||||
|
@target pagination: AppPaginationElement;
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
@@ -17,8 +17,8 @@ class HistoryPageElement extends HTMLElement {
|
|||||||
this.transactionsService = new TransactionsService(
|
this.transactionsService = new TransactionsService(
|
||||||
this.appMain?.appService
|
this.appMain?.appService
|
||||||
);
|
);
|
||||||
if (this.appMain.isAuth) this.getTransactions();
|
|
||||||
this.update();
|
this.update();
|
||||||
|
this.pagination?.setFetchFunc?.(this.getTransactions, true)!;
|
||||||
window.addEventListener("tokenchange", this.update);
|
window.addEventListener("tokenchange", this.update);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,41 +26,20 @@ class HistoryPageElement extends HTMLElement {
|
|||||||
window.removeEventListener("tokenchange", this.update);
|
window.removeEventListener("tokenchange", this.update);
|
||||||
}
|
}
|
||||||
|
|
||||||
getTransactions = async () => {
|
getTransactions = async (options) => {
|
||||||
try {
|
try {
|
||||||
const response = await this.transactionsService.getAll();
|
const response = await this.transactionsService.getAll(options);
|
||||||
if (response) {
|
return response;
|
||||||
this.setTransactions(response?.items);
|
|
||||||
}
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
setTransactions(transactions: Array<any>) {
|
|
||||||
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 = () => {
|
render = () => {
|
||||||
return html`
|
return html`
|
||||||
<ul>
|
<app-pagination
|
||||||
${this.transactions
|
data-target="history-page.pagination"
|
||||||
? this.transactions.map((transaction) => {
|
></app-pagination>
|
||||||
return html` <li>${transaction.description}</li> `;
|
|
||||||
})
|
|
||||||
: null}
|
|
||||||
</ul>
|
|
||||||
`;
|
`;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user