todo,todo-backend: switch to and implement offset/limit pagination

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2021-03-10 00:37:48 +01:00
parent 2eeb884353
commit cf193329a4
7 changed files with 75 additions and 81 deletions
+7 -3
View File
@@ -34,7 +34,8 @@ export class TodoClient implements TodoApi {
async listTodos({
entity,
cursor,
offset,
limit,
}: TodoListOptions): Promise<TodoListResult> {
const baseUrl = await this.discoveryApi.getBaseUrl('todo');
const token = await this.identityApi.getIdToken();
@@ -43,8 +44,11 @@ export class TodoClient implements TodoApi {
if (entity) {
query.set('entity', serializeEntityRef(entity) as string);
}
if (cursor) {
query.set('cursor', cursor);
if (typeof offset === 'number') {
query.set('offset', String(offset));
}
if (typeof limit === 'number') {
query.set('limit', String(limit));
}
const res = await fetch(`${baseUrl}/v1/todos?${query}`, {
+4 -6
View File
@@ -26,17 +26,15 @@ export type TodoItem = {
export type TodoListOptions = {
entity?: Entity;
cursor?: string;
offset?: number;
limit?: number;
};
export type TodoListResult = {
items: TodoItem[];
totalCount: number;
cursors: {
prev: string;
self: string;
next: string;
};
offset: number;
limit: number;
};
export interface TodoApi {
@@ -17,7 +17,7 @@
import { Progress, Table, TableColumn, useApi } from '@backstage/core';
import { useEntity } from '@backstage/plugin-catalog-react';
import Alert from '@material-ui/lab/Alert';
import React from 'react';
import React, { useState } from 'react';
import { useAsync } from 'react-use';
import { todoApiRef } from '../../api';
import { TodoItem } from '../../api/types';
@@ -52,10 +52,17 @@ const columns: TableColumn<TodoItem>[] = [
export const TodoList = () => {
const { entity } = useEntity();
const todoApi = useApi(todoApiRef);
const [page, setPage] = useState(0);
const [pageSize, setPageSize] = useState(10);
const { value, loading, error } = useAsync(
async () => todoApi.listTodos({ entity }),
[todoApi, entity],
async () =>
todoApi.listTodos({
entity,
offset: page * pageSize,
limit: pageSize,
}),
[todoApi, entity, page, pageSize],
);
if (loading) {
@@ -67,9 +74,15 @@ export const TodoList = () => {
return (
<Table
title="TODOs"
options={{ search: false }}
page={3}
options={{
search: false,
pageSize,
}}
page={page}
columns={columns}
totalCount={value!.totalCount}
onChangePage={setPage}
onChangeRowsPerPage={setPageSize}
data={value!.items}
/>
);