-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
front: stdcm consist form validation
Signed-off-by: Egor Berezovskiy <[email protected]>
- Loading branch information
Showing
8 changed files
with
215 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { kgToT, msToKmh } from 'utils/physics'; | ||
|
||
export const CONSIST_TOTAL_MASS_MAX = 10000; // ton | ||
export const CONSIST_TOTAL_LENGTH_MAX = 750; // m | ||
export const CONSIST_MAX_SPEED_MIN = 30; // km/h | ||
|
||
export const validateTotalMass = ({ | ||
tractionEngineMass = 0, | ||
towedMass = 0, | ||
totalMass, | ||
}: { | ||
tractionEngineMass?: number; | ||
towedMass?: number; | ||
totalMass?: number; | ||
}) => { | ||
if (!totalMass) { | ||
return undefined; | ||
} | ||
|
||
if (totalMass <= 0) { | ||
return 'consist.errors.totalMass.negative'; | ||
} | ||
|
||
const tractionMassInTons = kgToT(tractionEngineMass); | ||
const consistMassInTons = kgToT(tractionEngineMass + towedMass); | ||
const massLimit = towedMass ? consistMassInTons : tractionMassInTons; | ||
|
||
if (totalMass < massLimit || totalMass >= CONSIST_TOTAL_MASS_MAX) { | ||
return 'consist.errors.totalMass.range'; | ||
} | ||
|
||
return undefined; | ||
}; | ||
|
||
export const validateTotalLength = ({ | ||
tractionEngineLength = 0, | ||
towedLength = 0, | ||
totalLength, | ||
}: { | ||
tractionEngineLength?: number; | ||
towedLength?: number; | ||
totalLength?: number; | ||
}) => { | ||
if (!totalLength) { | ||
return undefined; | ||
} | ||
|
||
if (totalLength <= 0) { | ||
return 'consist.errors.totalLength.negative'; | ||
} | ||
|
||
const consistLength = Math.floor(tractionEngineLength + towedLength); | ||
|
||
if (totalLength < consistLength || totalLength >= CONSIST_TOTAL_LENGTH_MAX) { | ||
return 'consist.errors.totalLength.range'; | ||
} | ||
|
||
return undefined; | ||
}; | ||
|
||
export const validateMaxSpeed = (maxSpeed?: number, tractionEngineMaxSpeed?: number) => { | ||
if (!maxSpeed) { | ||
return undefined; | ||
} | ||
|
||
if (maxSpeed <= 0) { | ||
return 'consist.errors.maxSpeed.negative'; | ||
} | ||
|
||
if ( | ||
maxSpeed < CONSIST_MAX_SPEED_MIN || | ||
(tractionEngineMaxSpeed && maxSpeed > Math.floor(msToKmh(tractionEngineMaxSpeed))) | ||
) { | ||
return 'consist.errors.maxSpeed.range'; | ||
} | ||
|
||
return undefined; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters