SignIn type safety; remove unused formatNumericValue;

This commit is contained in:
cracklesparkle
2024-12-19 17:06:46 +09:00
parent 59dddfa02c
commit 65b7e275fe
2 changed files with 9 additions and 42 deletions

View File

@ -5,43 +5,6 @@ import { IObjectParam, IParam } from '../../interfaces/objects'
import TCBParameter from './TCBParameter' import TCBParameter from './TCBParameter'
import TableValue from './TableValue' import TableValue from './TableValue'
export function formatNumericValue(format: string, value: string) {
// Extract precision and scale from the format string
const regex = /numeric\((\d+),(\d+)\)/;
const match = format.match(regex);
if (!match) return value; // return original if format is not correct
const precision = parseInt(match[1], 10); // Total number of digits
const scale = parseInt(match[2], 10); // Number of digits after the decimal point
// Convert value to a number and handle cases like empty value or invalid input
const numericValue = parseFloat(value);
if (isNaN(numericValue)) {
return '0'.padStart(precision - scale, '0') + '.' + '0'.repeat(scale); // fallback in case of invalid value
}
// Ensure the value has the correct number of digits after the decimal point (scale)
let formattedValue = numericValue.toFixed(scale);
// Ensure the total length doesn't exceed the precision
const totalDigits = formattedValue.replace('.', '').length;
if (totalDigits > precision) {
// Truncate the value if it exceeds the total precision
formattedValue = numericValue.toPrecision(precision);
}
// Pad with leading zeros if necessary (if it's an integer and the precision is greater than the scale)
const [integerPart, decimalPart] = formattedValue.split('.');
// Ensure the integer part doesn't exceed the precision
const paddedInteger = integerPart.padStart(precision - scale, '0');
// Reassemble the number
return `${paddedInteger}.${decimalPart.padEnd(scale, '0')}`;
}
interface ObjectParameterProps { interface ObjectParameterProps {
showLabel?: boolean, showLabel?: boolean,
param: IObjectParam, param: IObjectParam,

View File

@ -1,5 +1,5 @@
import { useForm, SubmitHandler } from 'react-hook-form'; import { useForm, SubmitHandler } from 'react-hook-form';
import { AxiosResponse } from 'axios'; import { AxiosError, AxiosResponse } from 'axios';
import { ApiResponse, LoginFormData } from '../../interfaces/auth'; import { ApiResponse, LoginFormData } from '../../interfaces/auth';
import { login, setUserData } from '../../store/auth'; import { login, setUserData } from '../../store/auth';
import { useNavigate } from 'react-router-dom'; import { useNavigate } from 'react-router-dom';
@ -39,10 +39,14 @@ const SignIn = () => {
login(token) login(token)
navigate('/'); navigate('/');
} catch (error: any) { } catch (error: unknown) {
setError('password', { if ((error as AxiosError).response?.data) {
message: error?.response?.data?.detail const err = (error as AxiosError).response?.data
}) setError('password', {
message: (err as { detail: string })?.detail
})
}
} }
}; };