68 lines
1.8 KiB
TypeScript
68 lines
1.8 KiB
TypeScript
'use client';
|
|
|
|
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
|
import { useMemo } from 'react';
|
|
import { toast } from 'sonner';
|
|
|
|
import {
|
|
chainageService,
|
|
packageService,
|
|
projectService,
|
|
} from '@/services/api';
|
|
import type { Chainage, PaginationParams } from '@/types';
|
|
|
|
import { packageKeys } from '../../package/queries/packageKeys';
|
|
import { projectKeys } from '../../project/queries/projectKeys';
|
|
import { segmentKeys } from '../queries/segmentKeys';
|
|
|
|
interface UseSegmentsQueryParams {
|
|
skip: number;
|
|
limit: number;
|
|
}
|
|
|
|
export function useSegmentsQuery({ skip, limit }: UseSegmentsQueryParams) {
|
|
const listParams = useMemo<PaginationParams>(
|
|
() => ({ skip, limit }),
|
|
[limit, skip],
|
|
);
|
|
|
|
return useQuery({
|
|
queryKey: segmentKeys.list(listParams),
|
|
queryFn: () => chainageService.getChainages(listParams),
|
|
});
|
|
}
|
|
|
|
export function useProjectOptionsQuery() {
|
|
return useQuery({
|
|
queryKey: projectKeys.options(),
|
|
queryFn: () => projectService.getProjects({ skip: 0, limit: 1000 }),
|
|
});
|
|
}
|
|
|
|
export function useAllPackageOptionsQuery() {
|
|
return useQuery({
|
|
queryKey: packageKeys.options(),
|
|
queryFn: () => packageService.getPackages({ skip: 0, limit: 1000 }),
|
|
});
|
|
}
|
|
|
|
export function useDeleteSegmentMutation() {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: (segment: Chainage) =>
|
|
chainageService.deleteChainage(segment.id),
|
|
onSuccess: (_data, segment) => {
|
|
toast.success('Segment deleted', {
|
|
description: `${segment.segment_name} has been removed from the system.`,
|
|
});
|
|
queryClient.invalidateQueries({ queryKey: segmentKeys.all });
|
|
},
|
|
onError: () => {
|
|
toast.error('Deletion failed', {
|
|
description: 'The segment could not be removed. Please try again.',
|
|
});
|
|
},
|
|
});
|
|
}
|