mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-30 07:38:10 +00:00
feat(freight): added routes and locomotives
This commit is contained in:
47
apps/edr-freight-web/backoffice/src/hooks/useLocomotives.ts
Normal file
47
apps/edr-freight-web/backoffice/src/hooks/useLocomotives.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
||||
|
||||
import { locomotivesService } from '@/services/locomotives.service';
|
||||
|
||||
export const locomotiveKeys = {
|
||||
all: ['locomotives'] as const,
|
||||
details: () => [...locomotiveKeys.all, 'detail'] as const,
|
||||
detail: (id: string) => [...locomotiveKeys.details(), id] as const,
|
||||
};
|
||||
|
||||
export function useLocomotives() {
|
||||
return useQuery({
|
||||
queryKey: locomotiveKeys.all,
|
||||
queryFn: () => locomotivesService.getAll().then((response) => response.data),
|
||||
});
|
||||
}
|
||||
|
||||
export function useCreateLocomotive() {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: locomotivesService.create,
|
||||
onSuccess: () => qc.invalidateQueries({ queryKey: locomotiveKeys.all }),
|
||||
});
|
||||
}
|
||||
|
||||
export function useUpdateLocomotive() {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: ({ id, data }: { id: string; data: Record<string, unknown> }) =>
|
||||
locomotivesService.update(id, data),
|
||||
onSuccess: (_, { id }) => {
|
||||
qc.invalidateQueries({ queryKey: locomotiveKeys.all });
|
||||
qc.invalidateQueries({ queryKey: locomotiveKeys.detail(id) });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useDecommissionLocomotive() {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: locomotivesService.decommission,
|
||||
onSuccess: (_, id) => {
|
||||
qc.invalidateQueries({ queryKey: locomotiveKeys.all });
|
||||
qc.invalidateQueries({ queryKey: locomotiveKeys.detail(id) });
|
||||
},
|
||||
});
|
||||
}
|
||||
55
apps/edr-freight-web/backoffice/src/hooks/useRoutes.ts
Normal file
55
apps/edr-freight-web/backoffice/src/hooks/useRoutes.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
||||
|
||||
import { routesService } from '@/services/routes.service';
|
||||
|
||||
export const routeKeys = {
|
||||
all: ['routes'] as const,
|
||||
yards: ['routes', 'yards'] as const,
|
||||
details: () => [...routeKeys.all, 'detail'] as const,
|
||||
detail: (id: string) => [...routeKeys.details(), id] as const,
|
||||
};
|
||||
|
||||
export function useRoutes() {
|
||||
return useQuery({
|
||||
queryKey: routeKeys.all,
|
||||
queryFn: () => routesService.getAll().then((response) => response.data),
|
||||
});
|
||||
}
|
||||
|
||||
export function useRouteYards() {
|
||||
return useQuery({
|
||||
queryKey: routeKeys.yards,
|
||||
queryFn: () => routesService.getYards().then((response) => response.data.data),
|
||||
});
|
||||
}
|
||||
|
||||
export function useCreateRoute() {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: routesService.create,
|
||||
onSuccess: () => qc.invalidateQueries({ queryKey: routeKeys.all }),
|
||||
});
|
||||
}
|
||||
|
||||
export function useUpdateRoute() {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: ({ id, data }: { id: string; data: Record<string, unknown> }) =>
|
||||
routesService.update(id, data),
|
||||
onSuccess: (_, { id }) => {
|
||||
qc.invalidateQueries({ queryKey: routeKeys.all });
|
||||
qc.invalidateQueries({ queryKey: routeKeys.detail(id) });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useDeactivateRoute() {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: routesService.deactivate,
|
||||
onSuccess: (_, id) => {
|
||||
qc.invalidateQueries({ queryKey: routeKeys.all });
|
||||
qc.invalidateQueries({ queryKey: routeKeys.detail(id) });
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user