Files
solidtime/resources/js/utils/useTasksQuery.ts
Gregor Vostrak 556bbedeca add dynamic loading of paginated endpoints above page_limit
add request classes and fix collection typing for clients, tasks and tags
2026-02-18 22:32:56 +01:00

44 lines
1.3 KiB
TypeScript

import { useQuery, useQueryClient } from '@tanstack/vue-query';
import { api } from '@/packages/api/src';
import { getCurrentOrganizationId } from '@/utils/useUser';
import type { Task } from '@/packages/api/src';
import { computed } from 'vue';
import { fetchAllPages } from '@/utils/fetchAllPages';
export async function fetchAllTasks(organizationId: string): Promise<Task[]> {
return fetchAllPages((page) =>
api.getTasks({
params: { organization: organizationId },
queries: { done: 'all', page },
})
);
}
export function useTasksQuery() {
const queryClient = useQueryClient();
const query = useQuery({
queryKey: computed(() => ['tasks', getCurrentOrganizationId()]),
queryFn: async () => {
const organizationId = getCurrentOrganizationId();
if (!organizationId) throw new Error('No organization');
const data = await fetchAllTasks(organizationId);
return { data };
},
enabled: () => !!getCurrentOrganizationId(),
staleTime: 1000 * 30, // 30 seconds
});
const tasks = computed<Task[]>(() => query.data.value?.data ?? []);
const invalidateTasks = () => {
queryClient.invalidateQueries({ queryKey: ['tasks'] });
};
return {
...query,
tasks,
invalidateTasks,
};
}