WIP
This commit is contained in:
78
frontend/src/hooks/useDocuments.ts
Normal file
78
frontend/src/hooks/useDocuments.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import { documentsApi } from '../api/endpoints'
|
||||
import type { DocumentListResponse, UploadDocumentResponse } from '../api/types'
|
||||
|
||||
interface UseDocumentsParams {
|
||||
status?: string
|
||||
limit?: number
|
||||
offset?: number
|
||||
}
|
||||
|
||||
export const useDocuments = (params: UseDocumentsParams = {}) => {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
const { data, isLoading, error, refetch } = useQuery<DocumentListResponse>({
|
||||
queryKey: ['documents', params],
|
||||
queryFn: () => documentsApi.list(params),
|
||||
staleTime: 30000,
|
||||
})
|
||||
|
||||
const uploadMutation = useMutation({
|
||||
mutationFn: (file: File) => documentsApi.upload(file),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['documents'] })
|
||||
},
|
||||
})
|
||||
|
||||
const batchUploadMutation = useMutation({
|
||||
mutationFn: ({ files, csvFile }: { files: File[]; csvFile?: File }) =>
|
||||
documentsApi.batchUpload(files, csvFile),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['documents'] })
|
||||
},
|
||||
})
|
||||
|
||||
const deleteMutation = useMutation({
|
||||
mutationFn: (documentId: string) => documentsApi.delete(documentId),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['documents'] })
|
||||
},
|
||||
})
|
||||
|
||||
const updateStatusMutation = useMutation({
|
||||
mutationFn: ({ documentId, status }: { documentId: string; status: string }) =>
|
||||
documentsApi.updateStatus(documentId, status),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['documents'] })
|
||||
},
|
||||
})
|
||||
|
||||
const triggerAutoLabelMutation = useMutation({
|
||||
mutationFn: (documentId: string) => documentsApi.triggerAutoLabel(documentId),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['documents'] })
|
||||
},
|
||||
})
|
||||
|
||||
return {
|
||||
documents: data?.documents || [],
|
||||
total: data?.total || 0,
|
||||
limit: data?.limit || params.limit || 20,
|
||||
offset: data?.offset || params.offset || 0,
|
||||
isLoading,
|
||||
error,
|
||||
refetch,
|
||||
uploadDocument: uploadMutation.mutate,
|
||||
uploadDocumentAsync: uploadMutation.mutateAsync,
|
||||
isUploading: uploadMutation.isPending,
|
||||
batchUpload: batchUploadMutation.mutate,
|
||||
batchUploadAsync: batchUploadMutation.mutateAsync,
|
||||
isBatchUploading: batchUploadMutation.isPending,
|
||||
deleteDocument: deleteMutation.mutate,
|
||||
isDeleting: deleteMutation.isPending,
|
||||
updateStatus: updateStatusMutation.mutate,
|
||||
isUpdatingStatus: updateStatusMutation.isPending,
|
||||
triggerAutoLabel: triggerAutoLabelMutation.mutate,
|
||||
isTriggeringAutoLabel: triggerAutoLabelMutation.isPending,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user