-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
front: fix length, mass and speed reset in lmr
Signed-off-by: Theo Macron <[email protected]>
- Loading branch information
Showing
8 changed files
with
303 additions
and
65 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
88 changes: 88 additions & 0 deletions
88
front/src/applications/stdcm/hooks/useConsistFieldStatus.ts
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,88 @@ | ||
import type { InputProps } from '@osrd-project/ui-core'; | ||
import type { StatusWithMessage } from '@osrd-project/ui-core/dist/components/inputs/StatusMessage'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
import type { RollingStockWithLiveries, TowedRollingStock } from 'common/api/osrdEditoastApi'; | ||
import { kgToT, kmhToMs, msToKmh } from 'utils/physics'; | ||
|
||
import type { ConsistErrors } from '../types'; | ||
import { | ||
CONSIST_MAX_SPEED_MIN, | ||
CONSIST_TOTAL_LENGTH_MAX, | ||
CONSIST_TOTAL_MASS_MAX, | ||
} from '../utils/consistValidation'; | ||
|
||
const useConsistFieldStatus = ( | ||
type: 'totalMass' | 'totalLength' | 'maxSpeed', | ||
statusWithMessage: { | ||
totalMass?: InputProps['statusWithMessage']; | ||
totalLength?: InputProps['statusWithMessage']; | ||
maxSpeed?: InputProps['statusWithMessage']; | ||
}, | ||
consistErrors: ConsistErrors, | ||
statusMessagesVisible: { mass: boolean; length: boolean; speed: boolean }, | ||
rollingStock: RollingStockWithLiveries | undefined, | ||
towedRollingStock: TowedRollingStock | undefined | ||
): StatusWithMessage | undefined => { | ||
const { t } = useTranslation('stdcm'); | ||
|
||
switch (type) { | ||
case 'totalMass': { | ||
if (consistErrors?.totalMass) { | ||
return { | ||
status: 'error', | ||
tooltip: 'left', | ||
message: t(consistErrors.totalMass, { | ||
low: Math.ceil(kgToT((rollingStock?.mass ?? 0) + (towedRollingStock?.mass ?? 0))), | ||
high: CONSIST_TOTAL_MASS_MAX, | ||
}), | ||
}; | ||
} | ||
if (statusMessagesVisible.mass) { | ||
return statusWithMessage?.totalMass; | ||
} | ||
return undefined; | ||
} | ||
|
||
case 'totalLength': { | ||
if (consistErrors?.totalLength) { | ||
return { | ||
status: 'error', | ||
tooltip: 'left', | ||
message: t(consistErrors.totalLength, { | ||
low: Math.ceil((rollingStock?.length ?? 0) + (towedRollingStock?.length ?? 0)), | ||
high: CONSIST_TOTAL_LENGTH_MAX, | ||
}), | ||
}; | ||
} | ||
if (statusMessagesVisible.length) { | ||
return statusWithMessage?.totalLength; | ||
} | ||
return undefined; | ||
} | ||
|
||
case 'maxSpeed': { | ||
if (consistErrors?.maxSpeed) { | ||
return { | ||
status: 'error', | ||
tooltip: 'left', | ||
message: t(consistErrors.maxSpeed, { | ||
low: CONSIST_MAX_SPEED_MIN, | ||
high: Math.floor( | ||
msToKmh(Math.min(rollingStock?.max_speed ?? kmhToMs(CONSIST_MAX_SPEED_MIN))) | ||
), | ||
}), | ||
}; | ||
} | ||
if (statusMessagesVisible.speed) { | ||
return statusWithMessage?.maxSpeed; | ||
} | ||
return undefined; | ||
} | ||
|
||
default: | ||
return undefined; | ||
} | ||
}; | ||
|
||
export default useConsistFieldStatus; |
88 changes: 88 additions & 0 deletions
88
front/src/applications/stdcm/hooks/useStatusWithMessage.ts
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,88 @@ | ||
import type { InputProps } from '@osrd-project/ui-core'; | ||
import type { StatusWithMessage } from '@osrd-project/ui-core/dist/components/inputs/StatusMessage'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
import type { RollingStockWithLiveries, TowedRollingStock } from 'common/api/osrdEditoastApi'; | ||
import { kgToT, kmhToMs, msToKmh } from 'utils/physics'; | ||
|
||
import type { ConsistErrors } from '../types'; | ||
import { | ||
CONSIST_MAX_SPEED_MIN, | ||
CONSIST_TOTAL_LENGTH_MAX, | ||
CONSIST_TOTAL_MASS_MAX, | ||
} from '../utils/consistValidation'; | ||
|
||
const useConsistFieldStatus = ( | ||
type: 'totalMass' | 'totalLength' | 'maxSpeed', | ||
statusWithMessage: { | ||
totalMass?: InputProps['statusWithMessage']; | ||
totalLength?: InputProps['statusWithMessage']; | ||
maxSpeed?: InputProps['statusWithMessage']; | ||
}, | ||
consistErrors: ConsistErrors, | ||
statusMessagesVisible: { mass: boolean; length: boolean; speed: boolean }, | ||
rollingStock: RollingStockWithLiveries | undefined, | ||
towedRollingStock: TowedRollingStock | undefined | ||
): StatusWithMessage | undefined => { | ||
const { t } = useTranslation('stdcm'); | ||
|
||
switch (type) { | ||
case 'totalMass': { | ||
if (consistErrors?.totalMass) { | ||
return { | ||
status: 'error', | ||
tooltip: 'left', | ||
message: t(consistErrors.totalMass, { | ||
low: Math.ceil(kgToT((rollingStock?.mass ?? 0) + (towedRollingStock?.mass ?? 0))), | ||
high: CONSIST_TOTAL_MASS_MAX, | ||
}), | ||
}; | ||
} | ||
if (statusMessagesVisible.mass) { | ||
return statusWithMessage?.totalMass; | ||
} | ||
return undefined; | ||
} | ||
|
||
case 'totalLength': { | ||
if (consistErrors?.totalLength) { | ||
return { | ||
status: 'error', | ||
tooltip: 'left', | ||
message: t(consistErrors.totalLength, { | ||
low: Math.ceil((rollingStock?.length ?? 0) + (towedRollingStock?.length ?? 0)), | ||
high: CONSIST_TOTAL_LENGTH_MAX, | ||
}), | ||
}; | ||
} | ||
if (statusMessagesVisible.length) { | ||
return statusWithMessage?.totalLength; | ||
} | ||
return undefined; | ||
} | ||
|
||
case 'maxSpeed': { | ||
if (consistErrors?.maxSpeed) { | ||
return { | ||
status: 'error', | ||
tooltip: 'left', | ||
message: t(consistErrors.maxSpeed, { | ||
low: CONSIST_MAX_SPEED_MIN, | ||
high: Math.floor( | ||
msToKmh(Math.min(rollingStock?.max_speed ?? kmhToMs(CONSIST_MAX_SPEED_MIN))) | ||
), | ||
}), | ||
}; | ||
} | ||
if (statusMessagesVisible.speed) { | ||
return statusWithMessage?.maxSpeed; | ||
} | ||
return undefined; | ||
} | ||
|
||
default: | ||
return undefined; | ||
} | ||
}; | ||
|
||
export default useConsistFieldStatus; |
Oops, something went wrong.