diff --git a/src/common/DialogTable.ts b/src/common/DialogTable.ts new file mode 100644 index 0000000..a5b3b08 --- /dev/null +++ b/src/common/DialogTable.ts @@ -0,0 +1,113 @@ +import { ListResult } from '../lib/client'; + +const translations: Record> = { + pl_PL: { + Loading: 'Wczytywanie', + 'Previous page': 'Poprzednia strona', + 'Next page': 'Następna strona', + }, +}; + +const t = (s: string) => { + return translations[window.game_data.locale]?.[s] ?? s; +}; + +export type DialogTableColumn = { + header: string; + accessor: (row: T) => string; +}; + +export class DialogTable { + constructor( + private readonly id: string, + private readonly columns: DialogTableColumn[], + private readonly limit: number, + private readonly loadData: ( + page: number, + limit: number + ) => Promise> + ) {} + + public async render() { + await this.renderPage(1); + } + + private async renderPage(page: number) { + window.Dialog.show(`${this.id}_loading`, `

${t('Loading')}...

`); + + const { data, total } = await this.loadData(page, this.limit); + + const maxPage = Math.ceil(total / this.limit); + const pageOpts = []; + for (let i = 1; i <= (page > maxPage ? page : maxPage); i++) { + pageOpts.push( + `${i}` + ); + } + + const prevPageId = `${this.id}_page_prev`; + const selectId = `${this.id}_page_select`; + const nextPageId = `${this.id}_page_next`; + + window.Dialog.show( + this.id, + ` +
+ + + +
+ + + + ${this.columns.map((col) => ``).join('')} + + ${data + .map( + (r) => ` + + ${this.columns + .map((col) => ``) + .join('')} + + ` + ) + .join('')} + +
${col.header}
${col.accessor(r)}
+ ` + ); + + document + .querySelector('#' + prevPageId) + ?.addEventListener('click', (e: Event) => { + this.renderPage(page - 1); + }); + + document + .querySelector('#' + selectId) + ?.addEventListener('change', (e: Event) => { + if (!(e.currentTarget instanceof HTMLSelectElement)) { + return; + } + + this.renderPage(parseInt(e.currentTarget.value)); + }); + + document + .querySelector('#' + nextPageId) + ?.addEventListener('click', (e: Event) => { + this.renderPage(page + 1); + }); + } +} diff --git a/src/extendedPlayerProfile.ts b/src/extendedPlayerProfile.ts index 41d5df1..6001833 100644 --- a/src/extendedPlayerProfile.ts +++ b/src/extendedPlayerProfile.ts @@ -1,6 +1,7 @@ // Extended player profile import { ListResult, Player, TribeChange, TWHelpClient } from './lib/client'; +import { DialogTable } from './common/DialogTable'; const SCREEN = 'info_player'; const MODE = null; @@ -15,7 +16,6 @@ const translations: Record> = { 'Show tribe changes': 'Pokaż zmiany plemion', 'Show history': 'Pokaż historię', 'Show ennoblements': 'Pokaż przejęcia', - 'Fetching data': 'Pobieranie danych', 'Old tribe': 'Stare plemię', 'New tribe': 'Nowe plemię', 'Date/time': 'Data/czas', @@ -47,13 +47,12 @@ class TWHelpConnector { } } -interface UIConnector { - tribeChanges(page: number, limit: number): Promise>; +enum DialogId { + TRIBE_CHANGES = 'kichiyaki_tribe_changes', } -enum DialogId { - TRIBE_CHANGES_LOADING = 'kichiyaki_tribe_changes_loading', - TRIBE_CHANGES = 'kichiyaki_tribe_changes', +interface UIConnector { + tribeChanges(page: number, limit: number): Promise>; } class UI { @@ -143,81 +142,33 @@ class UI { private async showTribeChanges(e: Event) { e.preventDefault(); - await this._showTribeChanges(1); - } - - private async _showTribeChanges(page: number) { - window.Dialog.show( - DialogId.TRIBE_CHANGES_LOADING, - `

${t('Fetching data')}...

` - ); - - const tcs = await this.connector.tribeChanges(page, UI.TRIBE_CHANGES_LIMIT); - - const maxPage = Math.ceil(tcs.total / UI.TRIBE_CHANGES_LIMIT); - const pageOpts = []; - for (let i = 1; i <= (page > maxPage ? page : maxPage); i++) { - pageOpts.push( - `${i}` - ); - } - - const selectId = `${DialogId.TRIBE_CHANGES}_page`; - - window.Dialog.show( + await new DialogTable( DialogId.TRIBE_CHANGES, - ` -
- -
- - - - - - - - ${tcs.data - .map( - (tc) => ` - - - - - - ` - ) - .join('')} - -
${t('Old tribe')}${t('New tribe')}${t('Date/time')}
${ - tc.player.tribe - ? `${tc.player.tribe.tag}` - : '-' - }${ - tc.newTribe - ? `${tc.newTribe.tag}` - : '-' - }${new Date(tc.createdAt).toLocaleString()}
- ` - ); - - document - .querySelector('#' + selectId) - ?.addEventListener('change', (e: Event) => { - console.log(e.currentTarget); - if (!(e.currentTarget instanceof HTMLSelectElement)) { - return; - } - - this._showTribeChanges(parseInt(e.currentTarget.value)); - }); + [ + { + header: t('Old tribe'), + accessor: (tc) => + tc.player.tribe + ? `${tc.player.tribe.tag}` + : '-', + }, + { + header: t('New tribe'), + accessor: (tc) => + tc.newTribe + ? `${tc.newTribe.tag}` + : '-', + }, + { + header: t('Date/time'), + accessor: (tc) => new Date(tc.createdAt).toLocaleString(), + }, + ], + 2, + (page: number, limit: number) => { + return this.connector.tribeChanges(page, limit); + } + ).render(); } private showHistory(e: Event) {