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

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

Loading…
Cancel
Save