mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-29 06:00:55 +00:00
booking operations and trains scheduling also allocations
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
|
||||
import { QUERY_KEYS } from "@/constants/QUERY_KEYS";
|
||||
import { trainSchedulingService } from "@/services/trainScheduling.service";
|
||||
import type {
|
||||
AssignBookingsPayload,
|
||||
CreateTrainSchedulePayload,
|
||||
FreightType,
|
||||
PinWagonsPayload,
|
||||
TrainScheduleFilters,
|
||||
TrainSchedulePreviewPayload,
|
||||
} from "@/types/trainScheduling";
|
||||
|
||||
export const useScheduleList = (freightType?: FreightType) =>
|
||||
useQuery({
|
||||
queryKey: QUERY_KEYS.TRAIN_SCHEDULING.schedules(),
|
||||
queryFn: () => trainSchedulingService.listSchedules(freightType),
|
||||
});
|
||||
|
||||
export const useScheduleDetail = (id: string | undefined, freightType?: FreightType) =>
|
||||
useQuery({
|
||||
queryKey: QUERY_KEYS.TRAIN_SCHEDULING.scheduleById(id ?? ""),
|
||||
queryFn: () => trainSchedulingService.getScheduleById(id!, freightType),
|
||||
enabled: Boolean(id),
|
||||
});
|
||||
|
||||
export const useEligibleBookings = (
|
||||
filters?: TrainScheduleFilters,
|
||||
enabled = true,
|
||||
freightType?: FreightType,
|
||||
) =>
|
||||
useQuery({
|
||||
queryKey: QUERY_KEYS.TRAIN_SCHEDULING.eligible(freightType, filters),
|
||||
queryFn: () => trainSchedulingService.getEligibleBookings(filters, freightType),
|
||||
enabled,
|
||||
});
|
||||
|
||||
export const useAvailableLocomotives = () =>
|
||||
useQuery({
|
||||
queryKey: QUERY_KEYS.TRAIN_SCHEDULING.locomotives(),
|
||||
queryFn: () => trainSchedulingService.getAvailableLocomotives(),
|
||||
});
|
||||
|
||||
export const useScheduleMutations = (scheduleId?: string) => {
|
||||
const qc = useQueryClient();
|
||||
|
||||
const invalidate = () => {
|
||||
void qc.invalidateQueries({ queryKey: QUERY_KEYS.TRAIN_SCHEDULING.ROOT });
|
||||
void qc.invalidateQueries({ queryKey: QUERY_KEYS.TRAIN_SCHEDULING.schedules() });
|
||||
void qc.invalidateQueries({ queryKey: QUERY_KEYS.TRAIN_SCHEDULING.locomotives() });
|
||||
if (scheduleId) {
|
||||
void qc.invalidateQueries({
|
||||
queryKey: QUERY_KEYS.TRAIN_SCHEDULING.scheduleById(scheduleId),
|
||||
});
|
||||
}
|
||||
void qc.invalidateQueries({ queryKey: QUERY_KEYS.BOOKINGS.ROOT });
|
||||
};
|
||||
|
||||
const create = useMutation({
|
||||
mutationFn: ({
|
||||
freightType,
|
||||
payload,
|
||||
}: {
|
||||
freightType?: FreightType;
|
||||
payload: CreateTrainSchedulePayload;
|
||||
}) => trainSchedulingService.createSchedule(payload, freightType),
|
||||
onSuccess: invalidate,
|
||||
});
|
||||
|
||||
const preview = useMutation({
|
||||
mutationFn: ({
|
||||
freightType,
|
||||
payload,
|
||||
}: {
|
||||
freightType?: FreightType;
|
||||
payload: TrainSchedulePreviewPayload;
|
||||
}) => trainSchedulingService.preview(payload, freightType),
|
||||
});
|
||||
|
||||
const assign = useMutation({
|
||||
mutationFn: ({
|
||||
id,
|
||||
freightType,
|
||||
payload,
|
||||
}: {
|
||||
id: string;
|
||||
freightType?: FreightType;
|
||||
payload: AssignBookingsPayload;
|
||||
}) => trainSchedulingService.assignBookings(id, payload, freightType),
|
||||
onSuccess: invalidate,
|
||||
});
|
||||
|
||||
const unassign = useMutation({
|
||||
mutationFn: ({ id, bookingId }: { id: string; bookingId: string }) =>
|
||||
trainSchedulingService.unassignBooking(id, bookingId),
|
||||
onSuccess: invalidate,
|
||||
});
|
||||
|
||||
const pin = useMutation({
|
||||
mutationFn: ({ id, payload }: { id: string; payload: PinWagonsPayload }) =>
|
||||
trainSchedulingService.pinWagons(id, payload),
|
||||
onSuccess: invalidate,
|
||||
});
|
||||
|
||||
const finalize = useMutation({
|
||||
mutationFn: (id: string) => trainSchedulingService.finalizeSchedule(id),
|
||||
onSuccess: invalidate,
|
||||
});
|
||||
|
||||
const dispatch = useMutation({
|
||||
mutationFn: (id: string) => trainSchedulingService.dispatchSchedule(id),
|
||||
onSuccess: invalidate,
|
||||
});
|
||||
|
||||
const cancel = useMutation({
|
||||
mutationFn: ({ id, freightType }: { id: string; freightType?: FreightType }) =>
|
||||
trainSchedulingService.cancelSchedule(id, freightType ?? "CONTAINER"),
|
||||
onSuccess: invalidate,
|
||||
});
|
||||
|
||||
return { create, preview, assign, unassign, pin, finalize, dispatch, cancel, invalidate };
|
||||
};
|
||||
Reference in New Issue
Block a user