mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-29 19:58:11 +00:00
fix build num
This commit is contained in:
@@ -16,6 +16,7 @@ import { useEffect, useState } from "react";
|
||||
import { api } from "@/services/api";
|
||||
import type { TrainComposition } from "@/services/trainBuilder.service";
|
||||
import { useToast } from "@/hooks/use-toast";
|
||||
import { EXPORT_TRAIN_OPTIONS, importRunFor } from "@/constants/trainRuns";
|
||||
|
||||
const parseError = (error: unknown, fallback: string) => {
|
||||
if (isAxiosError(error)) {
|
||||
@@ -26,10 +27,6 @@ const parseError = (error: unknown, fallback: string) => {
|
||||
return fallback;
|
||||
};
|
||||
|
||||
// Run-number parity carries the trade direction: odd = export, even = import.
|
||||
const isOddNumber = (value: string) => /^\d*[13579]$/.test(value.trim());
|
||||
const isEvenNumber = (value: string) => /^\d*[02468]$/.test(value.trim());
|
||||
|
||||
/**
|
||||
* Step one of the Train Builder: pick the yard it is being assembled in and
|
||||
* couple at least two locomotives from that yard. The train code is assigned by
|
||||
@@ -59,6 +56,12 @@ export default function BuildTrainModal({ opened, onClose, onBuilt }: BuildTrain
|
||||
setLocomotiveIds([]);
|
||||
}, [yardId]);
|
||||
|
||||
// The import run is fixed by the export run, so it tracks it rather than
|
||||
// being entered by hand (and clears back to empty when the export is cleared).
|
||||
useEffect(() => {
|
||||
setImportTrainNumber(importRunFor(exportTrainNumber));
|
||||
}, [exportTrainNumber]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!opened) {
|
||||
setExportTrainNumber("");
|
||||
@@ -78,9 +81,11 @@ export default function BuildTrainModal({ opened, onClose, onBuilt }: BuildTrain
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (!isOddNumber(exportTrainNumber) || !isEvenNumber(importTrainNumber)) {
|
||||
// Both numbers come from the fixed run pairs, so parity cannot be wrong —
|
||||
// only "nothing picked" is reachable here.
|
||||
if (!exportTrainNumber || !importTrainNumber) {
|
||||
toast({
|
||||
title: "Enter both run numbers — export must be odd (e.g. 8001), import even (e.g. 8002)",
|
||||
title: "Pick an export train number (e.g. 8001) — the import run follows it",
|
||||
variant: "destructive",
|
||||
});
|
||||
return;
|
||||
@@ -133,31 +138,24 @@ export default function BuildTrainModal({ opened, onClose, onBuilt }: BuildTrain
|
||||
maxLength={100}
|
||||
/>
|
||||
<Group grow>
|
||||
<TextInput
|
||||
<Select
|
||||
label="Export train number"
|
||||
description="Odd — Ethiopia → Djibouti runs"
|
||||
placeholder="e.g. 8001"
|
||||
value={exportTrainNumber}
|
||||
onChange={(e) => setExportTrainNumber(e.currentTarget.value)}
|
||||
maxLength={20}
|
||||
error={
|
||||
exportTrainNumber && !isOddNumber(exportTrainNumber)
|
||||
? "Must be numeric and odd"
|
||||
: undefined
|
||||
}
|
||||
data={EXPORT_TRAIN_OPTIONS}
|
||||
value={exportTrainNumber || null}
|
||||
onChange={(value) => setExportTrainNumber(value ?? "")}
|
||||
searchable
|
||||
clearable
|
||||
/>
|
||||
{/* Fixed by the export run — derived, never typed. */}
|
||||
<TextInput
|
||||
label="Import train number"
|
||||
description="Even — Djibouti → Ethiopia runs"
|
||||
placeholder="e.g. 8002"
|
||||
value={importTrainNumber}
|
||||
onChange={(e) => setImportTrainNumber(e.currentTarget.value)}
|
||||
maxLength={20}
|
||||
error={
|
||||
importTrainNumber && !isEvenNumber(importTrainNumber)
|
||||
? "Must be numeric and even"
|
||||
: undefined
|
||||
}
|
||||
readOnly
|
||||
variant="filled"
|
||||
/>
|
||||
</Group>
|
||||
<Select
|
||||
|
||||
Reference in New Issue
Block a user