Files
lcbp3/frontend/hooks/use-rfa.ts
admin 158179d4a5
All checks were successful
Build and Deploy / deploy (push) Successful in 6m25s
260224:1606 20260224:1600 V1.8.0
2026-02-24 16:06:15 +07:00

91 lines
2.8 KiB
TypeScript

import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { rfaService } from '@/lib/services/rfa.service';
import { SearchRfaDto, CreateRfaDto, UpdateRfaDto } from '@/types/dto/rfa/rfa.dto';
import { WorkflowActionDto } from '@/lib/services/rfa.service';
import { toast } from 'sonner';
import { getApiErrorMessage } from '@/types/api-error';
// Keys
export const rfaKeys = {
all: ['rfas'] as const,
lists: () => [...rfaKeys.all, 'list'] as const,
list: (params: SearchRfaDto) => [...rfaKeys.lists(), params] as const,
details: () => [...rfaKeys.all, 'detail'] as const,
detail: (id: number | string) => [...rfaKeys.details(), id] as const,
};
// --- Queries ---
export function useRFAs(params: SearchRfaDto) {
return useQuery({
queryKey: rfaKeys.list(params),
queryFn: () => rfaService.getAll(params),
placeholderData: (previousData) => previousData,
});
}
export function useRFA(id: number | string) {
return useQuery({
queryKey: rfaKeys.detail(id),
queryFn: () => rfaService.getById(id),
enabled: !!id,
});
}
// --- Mutations ---
export function useCreateRFA() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (data: CreateRfaDto) => rfaService.create(data),
onSuccess: () => {
toast.success('RFA created successfully');
queryClient.invalidateQueries({ queryKey: rfaKeys.lists() });
},
onError: (error: unknown) => {
toast.error('Failed to create RFA', {
description: getApiErrorMessage(error, 'Something went wrong'),
});
},
});
}
export function useUpdateRFA() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: ({ id, data }: { id: number | string; data: UpdateRfaDto }) =>
rfaService.update(id, data),
onSuccess: (_, { id }) => {
toast.success('RFA updated successfully');
queryClient.invalidateQueries({ queryKey: rfaKeys.detail(id) });
queryClient.invalidateQueries({ queryKey: rfaKeys.lists() });
},
onError: (error: unknown) => {
toast.error('Failed to update RFA', {
description: getApiErrorMessage(error, 'Something went wrong'),
});
},
});
}
export function useProcessRFA() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: ({ id, data }: { id: number | string; data: WorkflowActionDto }) =>
rfaService.processWorkflow(id, data),
onSuccess: (_, { id }) => {
toast.success('Workflow status updated successfully');
queryClient.invalidateQueries({ queryKey: rfaKeys.detail(id) });
queryClient.invalidateQueries({ queryKey: rfaKeys.lists() });
},
onError: (error: unknown) => {
toast.error('Failed to process workflow', {
description: getApiErrorMessage(error, 'Something went wrong'),
});
},
});
}