import { attr, controller, target } from '@github/catalyst'; import { html, TemplateResult } from 'core/utils'; import { BaseComponentElement } from 'common/'; import { CircleLoaderElement } from 'components/circle-loader/CircleLoaderElement'; @controller class AppPaginationElement extends BaseComponentElement { public items: Array; @attr page: number; @attr rpp: number; @attr totalItems: number; @attr autoInit: string; @attr tableLayout: string = 'transactions-table'; initial: boolean = false; private customRenderItems: () => TemplateResult; private customRenderItem: (item: any) => TemplateResult; fetchFunc: Function = () => {}; constructor() { super(); } elementConnected = (): void => {}; attributeChangedCallback(): void { this.update(); } setItems = (items): void => { this.items = items; this.update(); }; setFetchFunc = async (fetchFunc: Function, autoInit?): Promise => { this.fetchFunc = fetchFunc; if (autoInit) { const options = { rpp: this.rpp || 5, page: this.page || 1, }; this.executeFetch(options); } }; setCustomRenderItems = (customRenderItems: () => TemplateResult) => { this.customRenderItems = customRenderItems; this.update(); }; setCustomRenderItem = (customRenderItem: (item: any) => TemplateResult) => { this.customRenderItem = customRenderItem; this.update(); }; executeFetch = async (options?): Promise => { if (!options) { options = { rpp: this.rpp || 5, page: this.page || 1, }; } try { this.loader?.start?.(); const response = await this.fetchFunc(options); this.loader?.stop?.(); this.setItems(response?.items); this.totalItems = response?.totalRecords; this.page = response?.page; this.rpp = response?.rpp; } catch (err) { this.loader?.stop?.(); console.error(err); } finally { this.initial = true; } }; pageBack = (): void => { const { page } = this; if (page > 1) { this.page--; this.executeFetch(); } }; pageNext = (): void => { const { rpp, totalItems, page } = this; const pageRange = Math.ceil(totalItems / rpp); if (page < pageRange) { this.page++; this.executeFetch(); } }; render = (): TemplateResult => { const { rpp, totalItems, page, items } = this; const renderItem = this.customRenderItem ? this.customRenderItem : (item, iter) => html` ${iter + 1 + rpp * (page - 1)} ${item.description} ${Number(item.amount).toLocaleString('en-US', { maximumFractionDigits: 2, minimumFractionDigits: 2, })} (${item.currency ? item.currency : 'USD'}) `; const renderItems = this.customRenderItems ? this.customRenderItems : () => { if (this.loader && this.loader.loading && !this.initial) { return html``; } else { if (items?.length > 0) { return html` ${items?.map((item, iter) => renderItem(item, iter))} ${renderPagination()}
`; } return html``; } }; const renderPagination = () => { if (totalItems > items?.length) { const pageRange = Math.ceil(totalItems / rpp); return html`
`; } }; return html`
${renderItems()}
`; }; } export type { AppPaginationElement };