From be67df904ae963bd4350577c56ec50b7596e09c6 Mon Sep 17 00:00:00 2001 From: Radoslaw Szwajkowski Date: Tue, 11 Jun 2024 21:29:55 +0200 Subject: [PATCH] Fetch tasks with server-based filtering Part-of: https://github.com/konveyor/tackle2-ui/issues/1931 Reference-Url: https://github.com/konveyor/tackle2-hub/pull/640 --- 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 df92de2d33..4b839e92e9 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 9d07bd42c5..b92e4ebd89 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