数値入力のバリデーション修正

feature-frontend-styles
Masaharu.Kato 4 months ago
parent 026347f7e4
commit 82b0465556
  1. 18
      frontend/src/pages/TaskListPage.tsx

@ -44,7 +44,7 @@ import { TOBUY_ERRORS } from '../constants/errorMessages';
const EMPTY_TOBUY: Omit<ToBuy, 'tobuyId' | 'stuffId'> & { stuffId: number | null, category: string } & { newAddition: boolean } = {
stuffId: null,
stuffName: '',
amount: 0,
amount: 1,
shop: '',
category: '',
newAddition: false,
@ -174,6 +174,11 @@ const TaskListPage: React.FC = () => {
if (newToBuy.newAddition) {
newToBuy.stuffId = null;
}
if (isNaN(newToBuy.amount)) {
console.log('数量が正しくありません.');
return;
}
console.log(newToBuy)
await toBuyApi.addToBuy(newToBuy);
setOpenDialog(false); // ダイアログを閉じる
@ -191,6 +196,11 @@ const TaskListPage: React.FC = () => {
*/
const handleUpdateTask = async () => {
try {
if (isNaN(selectedEditingTask.amount)) {
console.log('数量が正しくありません.');
return;
}
console.log(selectedEditingTask)
await toBuyApi.updateToBuy(selectedEditingTask);
setOpenAmountDialog(false); // ダイアログを閉じる
@ -299,7 +309,7 @@ const TaskListPage: React.FC = () => {
<IconButton
edge="end"
sx={{ marginLeft: 3 }}
sx={{ marginRight: 0, marginLeft: 0 }}
aria-label="delete"
onClick={() => handleDeleteTask(tobuy.tobuyId)}
>
@ -411,7 +421,7 @@ const TaskListPage: React.FC = () => {
onChange={(e) => {
const value = e.target.value;
const parsedValue = parseInt(value, 10); // 数値に変換
if (!isNaN(parsedValue) && parsedValue >= 0) { //負数除外
if (/*!isNaN(parsedValue) && */ isNaN(parsedValue) || parsedValue >= 1) { //負数除外
setNewToBuy({ ...newToBuy, amount: parsedValue }); // number型で保存
}
}}
@ -517,7 +527,7 @@ const TaskListPage: React.FC = () => {
onChange={(e) => {
const value = e.target.value;
const parsedValue = parseInt(value, 10); // 数値に変換
if (!isNaN(parsedValue) && parsedValue >= 0) { //負数除外
if (/*!isNaN(parsedValue) && */ isNaN(parsedValue) || parsedValue >= 1) { //負数除外
setSelectedEditingTask({ ...selectedEditingTask, amount: parsedValue }); // number型で保存
}
}}

Loading…
Cancel
Save