Merge pull request #999 from Tria-plc/tests

Fixing the round trip disabling dates
This commit is contained in:
mulish77
2026-07-28 16:27:22 +03:00
committed by GitHub
7 changed files with 241 additions and 1 deletions

View File

@@ -758,6 +758,53 @@ export default function SearchPage() {
}
}, [departureDate, disabledDates, setValue, setError]);
// Same idea as the departure-date availability above, but for the return leg — which travels
// destination -> origin, the reverse pair. Only fetched for round trips once both stations are
// picked; deliberately unaware of which specific outbound schedule will end up chosen (that's
// a searchTrips()-time concern via the latestOutboundArrival same-day-connection filter, not
// something this pre-search picker can know).
const { data: returnAvailableDates } = useQuery<AvailableDatesResponse>({
queryKey: ["available-dates", destId, originId],
queryFn: async () => {
const from = new Date();
const to = new Date();
to.setDate(to.getDate() + AVAILABLE_DATES_RANGE_DAYS);
return (await apiClient.get("/search/available-dates", {
params: {
originStationId: destId,
destinationStationId: originId,
from: toDateStr(from),
to: toDateStr(to),
},
})) as AvailableDatesResponse;
},
enabled: !!originId && !!destId && tripType === "ROUND_TRIP",
staleTime: 5 * 60 * 1000,
});
const returnDisabledDates = useMemo(() => {
const set = new Set<string>();
if (!returnAvailableDates?.routeExists) return set;
for (const d of returnAvailableDates.dates) if (!d.available) set.add(d.date);
return set;
}, [returnAvailableDates]);
const returnMaxDate = originId && destId && tripType === "ROUND_TRIP" ? maxSearchDate : undefined;
// If the currently selected return date becomes unavailable (From/To changed, or the
// availability query just resolved), clear it and surface an inline error — mirrors the
// departure-date effect above.
useEffect(() => {
if (returnDate && returnDisabledDates.has(returnDate)) {
setValue("returnDate", "");
setError("returnDate", {
type: "manual",
message:
"No trains run this route on the selected return date — please pick another date.",
});
}
}, [returnDate, returnDisabledDates, setValue, setError]);
const saveRecent = useCallback((id: string) => {
setRecentStationIds((prev) => {
const next = [id, ...prev.filter((x) => x !== id)].slice(0, 5);
@@ -1229,6 +1276,8 @@ export default function SearchPage() {
? new Date(departureDate + "T00:00:00")
: new Date()
}
maxDate={returnMaxDate}
disabledDates={returnDisabledDates}
placeholder="Return date"
error={!!errors.returnDate}
/>
@@ -1574,6 +1623,8 @@ export default function SearchPage() {
? new Date(departureDate + "T00:00:00")
: new Date()
}
maxDate={returnMaxDate}
disabledDates={returnDisabledDates}
placeholder="Return date"
/>
{errors.returnDate && (