Skip to content

Commit

Permalink
Improve page URL fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
gilbitron committed Nov 15, 2023
1 parent edcf95a commit 6c12ea3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
36 changes: 24 additions & 12 deletions src/RenderlessPagination.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ export default {
: this.data.current_page ?? null;
},
firstPageUrl() {
return this.isApiResource
? this.data.links.first
: this.data.first_page_url ?? null;
return (
this.data.first_page_url ??
this.data.meta?.first_page_url ??
this.data.links?.first ??
null
);
},
from() {
return this.isApiResource
Expand All @@ -42,24 +45,33 @@ export default {
: this.data.last_page ?? null;
},
lastPageUrl() {
return this.isApiResource
? this.data.links.last
: this.data.last_page_url ?? null;
return (
this.data.last_page_url ??
this.data.meta?.last_page_url ??
this.data.links?.last ??
null
);
},
nextPageUrl() {
return this.isApiResource
? this.data.links.next
: this.data.next_page_url ?? null;
return (
this.data.next_page_url ??
this.data.meta?.next_page_url ??
this.data.links?.next ??
null
);
},
perPage() {
return this.isApiResource
? this.data.meta.per_page
: this.data.per_page ?? null;
},
prevPageUrl() {
return this.isApiResource
? this.data.links.prev
: this.data.prev_page_url ?? null;
return (
this.data.prev_page_url ??
this.data.meta?.prev_page_url ??
this.data.links?.prev ??
null
);
},
to() {
return this.isApiResource
Expand Down
10 changes: 6 additions & 4 deletions tests/unit/RenderlessPagination.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,15 @@ test('parses paginate data', function () {

expect(wrapper.vm.isApiResource).toEqual(true);
expect(wrapper.vm.currentPage).toEqual(dataPaginateData.meta.current_page);
expect(wrapper.vm.firstPageUrl).toEqual(dataPaginateData.links.first);
expect(wrapper.vm.firstPageUrl).toEqual(
dataPaginateData.meta.first_page_url
);
expect(wrapper.vm.from).toEqual(dataPaginateData.meta.from);
expect(wrapper.vm.lastPage).toEqual(dataPaginateData.meta.last_page);
expect(wrapper.vm.lastPageUrl).toEqual(dataPaginateData.links.last);
expect(wrapper.vm.nextPageUrl).toEqual(dataPaginateData.links.next);
expect(wrapper.vm.lastPageUrl).toEqual(dataPaginateData.meta.last_page_url);
expect(wrapper.vm.nextPageUrl).toEqual(dataPaginateData.meta.next_page_url);
expect(wrapper.vm.perPage).toEqual(dataPaginateData.meta.per_page);
expect(wrapper.vm.prevPageUrl).toEqual(dataPaginateData.links.prev);
expect(wrapper.vm.prevPageUrl).toEqual(dataPaginateData.meta.prev_page_url);
expect(wrapper.vm.to).toEqual(dataPaginateData.meta.to);
expect(wrapper.vm.total).toEqual(dataPaginateData.meta.total);
expect(wrapper.vm.pageRange).toEqual(10);
Expand Down

0 comments on commit 6c12ea3

Please sign in to comment.