|
| 1 | +import { service } from '@ember/service'; |
| 2 | +import Component from '@glimmer/component'; |
| 3 | +import { cached, tracked } from '@glimmer/tracking'; |
| 4 | + |
| 5 | +import { query } from '@ember-data/json-api/request'; |
| 6 | +import { filterEmpty } from '@ember-data/request-utils'; |
| 7 | +import type Store from '@ember-data/store'; |
| 8 | +import type { Document } from '@ember-data/store/-private/document'; |
| 9 | + |
| 10 | +import type Book from '../models/book'; |
| 11 | +import type { ApiPage } from '../utils/pagination-links'; |
| 12 | +import { PaginationLinks } from '../utils/pagination-links'; |
| 13 | + |
| 14 | +export interface BookListSignature { |
| 15 | + Element: HTMLDivElement; |
| 16 | + Args: { |
| 17 | + sort: string | null; |
| 18 | + filter: string | null; |
| 19 | + genre: string | null; |
| 20 | + author: string | null; |
| 21 | + page: number | null; |
| 22 | + limit: number | null; |
| 23 | + }; |
| 24 | +} |
| 25 | + |
| 26 | +class AsyncContent<T> { |
| 27 | + @tracked content: T | undefined; |
| 28 | +} |
| 29 | + |
| 30 | +export default class BookListComponent extends Component<BookListSignature> { |
| 31 | + @service declare store: Store; |
| 32 | + @tracked currentUrl: string | null = null; |
| 33 | + links = new PaginationLinks(); |
| 34 | + dataWrapper = new AsyncContent<Document<Book[]>>(); |
| 35 | + |
| 36 | + // we use this to detect inbound data changes |
| 37 | + _firstPageOptions: { url: string } | null = null; |
| 38 | + |
| 39 | + @cached |
| 40 | + get firstPageOptions(): { url: string } { |
| 41 | + const { sort, filter, genre, author, page, limit } = this.args; |
| 42 | + |
| 43 | + const options = query('book', filterEmpty({ sort, filter, genre, author, page, limit })); |
| 44 | + this._firstPageOptions = options; |
| 45 | + return options; |
| 46 | + } |
| 47 | + |
| 48 | + @cached |
| 49 | + get currentPage() { |
| 50 | + const _firstPageOptions = this._firstPageOptions; |
| 51 | + const firstPageOptions = this.firstPageOptions; |
| 52 | + const currentUrl = this.currentUrl; |
| 53 | + |
| 54 | + // if the first page options changed, we need to fetch a new first page |
| 55 | + if (_firstPageOptions?.url !== firstPageOptions.url) { |
| 56 | + return this.fetchPage(firstPageOptions); |
| 57 | + } |
| 58 | + |
| 59 | + return this.fetchPage(currentUrl ? { url: currentUrl } : firstPageOptions); |
| 60 | + } |
| 61 | + |
| 62 | + get books(): Document<Book[]> | null { |
| 63 | + return this.currentPage.content || null; |
| 64 | + } |
| 65 | + |
| 66 | + fetchPage(options: { url: string }) { |
| 67 | + const dataWrapper = this.dataWrapper; |
| 68 | + const future = this.store.request<Document<Book[]>>(options); |
| 69 | + |
| 70 | + void future.then((books) => { |
| 71 | + dataWrapper.content = books.content; |
| 72 | + this.links.addPage(books.content as unknown as ApiPage); |
| 73 | + }); |
| 74 | + |
| 75 | + return dataWrapper; |
| 76 | + } |
| 77 | + |
| 78 | + updatePage = (url: string) => { |
| 79 | + this.currentUrl = url; |
| 80 | + }; |
| 81 | +} |
0 commit comments