admin_ruspet/src/features/approvals/hooks/useApprovals.ts

139 lines
4.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
import {
approvalsApi,
type ApprovalsListParams,
type ApprovalsReportParams,
type UpdateApprovalRequest
} from '@/api/approvals'
import { notifications } from '@mantine/notifications'
export const APPROVALS_QUERY_KEY = 'approvals'
export function useApprovals(params: ApprovalsListParams = {}) {
return useQuery({
queryKey: [APPROVALS_QUERY_KEY, params],
queryFn: async () => {
const response = await approvalsApi.getList(params)
return response.data
},
})
}
export function useApproval(id: number | null) {
return useQuery({
queryKey: ['approval', id],
queryFn: async () => {
if (!id) return null
const response = await approvalsApi.getById(id)
return response.data
},
enabled: !!id,
})
}
// 🆕 Хук для получения заявок по отчёту (с фильтрами по датам)
export function useApprovalsReport(params: ApprovalsReportParams = {}) {
return useQuery({
queryKey: ['approvals-report', params],
queryFn: async () => {
const response = await approvalsApi.getForReport(params)
return response.data.data || []
},
enabled: false, // Запускаем вручную
})
}
export function useUpdateApproval() {
const queryClient = useQueryClient()
return useMutation({
mutationFn: (data: UpdateApprovalRequest) => approvalsApi.update(data),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: [APPROVALS_QUERY_KEY] })
notifications.show({
title: 'Успешно',
message: 'Заявка обновлена',
color: 'green',
})
},
onError: (error: any) => {
notifications.show({
title: 'Ошибка',
message: error.response?.data?.error_description || 'Не удалось обновить заявку',
color: 'red',
})
},
})
}
export function useDeleteApproval() {
const queryClient = useQueryClient()
return useMutation({
mutationFn: (id: number) => approvalsApi.delete(id),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: [APPROVALS_QUERY_KEY] })
notifications.show({
title: 'Успешно',
message: 'Заявка удалена',
color: 'green',
})
},
onError: (error: any) => {
notifications.show({
title: 'Ошибка',
message: error.response?.data?.error_description || 'Не удалось удалить заявку',
color: 'red',
})
},
})
}
export function useAdditionalApprovers(approvalId: number | null) {
return useQuery({
queryKey: ['approval-additional-approvers', approvalId],
queryFn: async () => {
if (!approvalId) return null
const response = await approvalsApi.getAdditionalApprovers(approvalId)
return response.data
},
enabled: !!approvalId,
})
}
export function useApprovalComments(approvalId: number | null) {
return useQuery({
queryKey: ['approval-comments', approvalId],
queryFn: async () => {
if (!approvalId) return null
const response = await approvalsApi.getComments(approvalId)
return response.data
},
enabled: !!approvalId,
})
}
export function useApprovalObservers(approvalId: number | null) {
return useQuery({
queryKey: ['approval-observers', approvalId],
queryFn: async () => {
if (!approvalId) return null
const response = await approvalsApi.getObservers(approvalId)
return response.data
},
enabled: !!approvalId,
})
}
export function useRevocationRequest(approvalId: number | null) {
return useQuery({
queryKey: ['approval-revocation-request', approvalId],
queryFn: async () => {
if (!approvalId) return null
const response = await approvalsApi.getRevocationRequest(approvalId)
return response.data
},
enabled: !!approvalId,
})
}