From c2f4f067b14eacf6742e0033e2244ff864d00085 Mon Sep 17 00:00:00 2001 From: Radoslaw Szwajkowski Date: Thu, 13 Jun 2024 16:10:53 +0200 Subject: [PATCH] :sparkles: Fetch tasks with server-based filtering (#1956) Part-of: https://github.com/konveyor/tackle2-ui/issues/1931 Reference-Url: https://github.com/konveyor/tackle2-hub/pull/640 Signed-off-by: Radoslaw Szwajkowski --- client/src/app/api/rest.ts | 3 +++ client/src/app/queries/tasks.ts | 29 ++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/client/src/app/api/rest.ts b/client/src/app/api/rest.ts index df92de2d3..4b839e92e 100644 --- a/client/src/app/api/rest.ts +++ b/client/src/app/api/rest.ts @@ -345,6 +345,9 @@ export function getTaskById( export const getTasks = () => axios.get(TASKS).then((response) => response.data); +export const getServerTasks = (params: HubRequestParams = {}) => + getHubPaginatedResult(TASKS, params); + export const deleteTask = (id: number) => axios.delete(`${TASKS}/${id}`); export const cancelTask = (id: number) => diff --git a/client/src/app/queries/tasks.ts b/client/src/app/queries/tasks.ts index 9d07bd42c..b92e4ebd8 100644 --- a/client/src/app/queries/tasks.ts +++ b/client/src/app/queries/tasks.ts @@ -1,7 +1,14 @@ import { useMutation, useQuery } from "@tanstack/react-query"; -import { cancelTask, deleteTask, getTaskById, getTasks } from "@app/api/rest"; +import { + cancelTask, + deleteTask, + getServerTasks, + getTaskById, + getTasks, +} from "@app/api/rest"; import { universalComparator } from "@app/utils/utils"; +import { HubPaginatedResult, HubRequestParams, Task } from "@app/api/models"; interface FetchTasksFilters { addon?: string; @@ -50,6 +57,26 @@ export const useFetchTasks = ( }; }; +export const useServerTasks = (params: HubRequestParams = {}) => { + const { data, isLoading, error, refetch } = useQuery({ + queryKey: [TasksQueryKey, params], + queryFn: async () => await getServerTasks(params), + onError: (error) => console.log("error, ", error), + keepPreviousData: true, + }); + + return { + result: { + data: data?.data, + total: data?.total ?? 0, + params: data?.params ?? params, + } as HubPaginatedResult, + isFetching: isLoading, + fetchError: error, + refetch, + }; +}; + export const useDeleteTaskMutation = ( onSuccess: () => void, onError: (err: Error | null) => void