Tempo: Fix NaN value using fallback (#81150)

This commit is contained in:
Fabrizio 2024-01-24 16:21:36 +01:00 committed by GitHub
parent f726ea1e52
commit 0434f191fe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -13,6 +13,19 @@ interface Props {
query: Partial<TempoQuery> & TempoQuery;
}
/**
* Parse a string value to integer. If the conversion fails, for example because we are prosessing an empty value for
* a field, return a fallback (default) value.
*
* @param val the value to be parsed to an integer
* @param fallback the fallback value
* @returns the converted value or the fallback value if the conversion fails
*/
const parseIntWithFallback = (val: string, fallback: number) => {
const parsed = parseInt(val, 10);
return isNaN(parsed) ? fallback : parsed;
};
export const TempoQueryBuilderOptions = React.memo<Props>(({ onChange, query }) => {
if (!query.hasOwnProperty('limit')) {
query.limit = DEFAULT_LIMIT;
@ -23,10 +36,10 @@ export const TempoQueryBuilderOptions = React.memo<Props>(({ onChange, query })
}
const onLimitChange = (e: React.FormEvent<HTMLInputElement>) => {
onChange({ ...query, limit: parseInt(e.currentTarget.value, 10) });
onChange({ ...query, limit: parseIntWithFallback(e.currentTarget.value, DEFAULT_LIMIT) });
};
const onSpssChange = (e: React.FormEvent<HTMLInputElement>) => {
onChange({ ...query, spss: parseInt(e.currentTarget.value, 10) });
onChange({ ...query, spss: parseIntWithFallback(e.currentTarget.value, DEFAULT_SPSS) });
};
const onTableTypeChange = (val: SearchTableType) => {
onChange({ ...query, tableType: val });