Skip to content

Commit c27eb30

Browse files
committed
simplify page state logic
1 parent c3dc2c5 commit c27eb30

File tree

1 file changed

+10
-19
lines changed

1 file changed

+10
-19
lines changed

frontend/src/components/ui/pagination.ts

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -141,21 +141,11 @@ export class Pagination extends LitElement {
141141

142142
searchParams = new SearchParamsController(this, (params) => {
143143
const page = parsePage(params.get(this.name));
144-
145-
// TODO: figure out why previous params aren't working here
146-
// in the meantime, 0 works okay — we do an inequality check in `onPageChange` anyways
147-
this.onPageChange(page, 0);
144+
this.onPageChange(page);
148145
});
149146

150-
// @property({ type: Number })
151-
// public set page(page: number) {
152-
// this.onPageChange(page);
153-
// }
154-
155-
public get page() {
156-
const page = parsePage(this.searchParams.searchParams.get(this.name));
157-
return Math.max(1, Math.min(this.pages, page));
158-
}
147+
@state()
148+
page = 1;
159149

160150
@property({ type: String })
161151
name = "page";
@@ -188,9 +178,10 @@ export class Pagination extends LitElement {
188178
const parsedPage = parseFloat(
189179
this.searchParams.searchParams.get(this.name) ?? "1",
190180
);
191-
if (parsedPage > this.pages || parsedPage < 1 || parsedPage % 1 !== 0) {
192-
this.onPageChange(this.page, parsedPage);
193-
this.requestUpdate("page", parsedPage);
181+
if (parsedPage != this.page) {
182+
const page = parsePage(this.searchParams.searchParams.get(this.name));
183+
const constrainedPage = Math.max(1, Math.min(this.pages, page));
184+
this.onPageChange(constrainedPage);
194185
}
195186

196187
if (changedProperties.get("page") && this.page) {
@@ -354,8 +345,8 @@ export class Pagination extends LitElement {
354345
this.onPageChange(this.page < this.pages ? this.page + 1 : this.pages);
355346
}
356347

357-
private onPageChange(page: number, prevPage = this.page) {
358-
if (prevPage !== page) {
348+
private onPageChange(page: number) {
349+
if (this.page !== page) {
359350
this.searchParams.set((params) => {
360351
if (page === 1) {
361352
params.delete(this.name);
@@ -364,14 +355,14 @@ export class Pagination extends LitElement {
364355
}
365356
return params;
366357
});
367-
this.requestUpdate("page", prevPage);
368358
this.dispatchEvent(
369359
new CustomEvent<PageChangeDetail>("page-change", {
370360
detail: { page: page, pages: this.pages },
371361
composed: true,
372362
}),
373363
);
374364
}
365+
this.page = page;
375366
}
376367

377368
private calculatePages() {

0 commit comments

Comments
 (0)