diff --git a/frontend/src/pages/TaskListPage.tsx b/frontend/src/pages/TaskListPage.tsx index 074f03c..51493a9 100644 --- a/frontend/src/pages/TaskListPage.tsx +++ b/frontend/src/pages/TaskListPage.tsx @@ -32,7 +32,7 @@ import { } from '@mui/material'; import { Add as AddIcon, Delete as DeleteIcon, ShoppingBasket as ShoppingBasketIcon, - SoupKitchen as SoupKitchenIcon + SoupKitchen as SoupKitchenIcon, Edit as EditIcon } from '@mui/icons-material'; import { ToBuy, Stuff, /*Stock*/ } from '../types/types'; import { TOBUY_ERRORS } from '../constants/errorMessages'; @@ -64,9 +64,20 @@ const TaskListPage: React.FC = () => { //在庫登録ダイアログの表示状態 const [openInfoDialog, setOpenInfoDialog] = useState(false); + //数量変更ダイアログの表示状態 + const [openAmountDialog, setOpenAmountDialog] = useState(false); const [selectedTask, setSelectedTask] = useState(0); + const [selectedEditingTask, setSelectedEditingTask] = useState({ + tobuyId: 0, + stuffId: 0, + stuffName: "", + amount: 0, + shop: undefined, +}); + + const [newToBuy, setNewToBuy] = useState(EMPTY_TOBUY); const [stuffs, setStuffs] = useState([]); @@ -88,6 +99,7 @@ const TaskListPage: React.FC = () => { const fetchTasks = async () => { try { const tobuys = await toBuyApi.getToBuys(); + console.log(tobuys); setToBuys(tobuys); } catch (error) { console.error(`${TOBUY_ERRORS.FETCH_FAILED}:`, error); @@ -172,6 +184,38 @@ const TaskListPage: React.FC = () => { } }; + /** + * 買い物リストの数量を変更するハンドラー + * 入力されたタスク情報をAPIに送信して変更 + * 作成後はダイアログを閉じ、入力内容をリセット + */ + const handleUpdateTask = async () => { + try { + console.log(selectedEditingTask) + await toBuyApi.updateToBuy(selectedEditingTask); + setOpenAmountDialog(false); // ダイアログを閉じる + //setNewToBuy(EMPTY_TOBUY); // 入力内容をリセット + fetchTasks(); // 作成後のタスク一覧を再取得 + } catch (error) { + console.error(`${TOBUY_ERRORS.CREATE_FAILED}:`, error); + } + }; + +// /** +// * クリックされたタスクの情報を取得するための関数 +// * @param tobuyId +// * @returns tobuyIdが一致するtoBuy +// */ +// const getToBuyDetails = (tobuyId: number): ToBuy => { +// console.log(tobuyId) +// const result = tobuys.find((toBuy) => toBuy.tobuyId === tobuyId); +// if(result === undefined){ +// throw new Error(`tobuyId: ${tobuyId} に対応するデータが見つかりません`); +// } +// return result; +// }; + + return ( @@ -207,11 +251,25 @@ const TaskListPage: React.FC = () => { {`× ${tobuy.amount}`} + {/* 買い物リスト:数量変更ボタン */} + + { + setOpenAmountDialog(true) + setSelectedEditingTask(tobuy) + }} + > + + + + {/* 買い物リスト:食材情報記入ボタン */} - {/* デフォルトの marginRightの設定(マイナス)を上書き */} { @@ -223,6 +281,7 @@ const TaskListPage: React.FC = () => { + {/* 買い物リスト:食材削除ボタン */} { handleDeleteTask(tobuy.tobuyId)} > @@ -253,31 +313,37 @@ const TaskListPage: React.FC = () => { {/* 新規材料作成ボタン - 画面下部に固定表示 */} - - setOpenDialog(true)} - > - - - - {/*新規料理追加ボタン - 画面下部に固定表示 */} - - { - setOpenDialog(true); - //handleNavigate('/AddDishies1'); - }} - //selected={isSelected('/test')} + + + 材料のみ追加 + + + setOpenDialog(true)} + > + + + {/*新規料理追加ボタン - 画面下部に固定表示 */} + + + 料理から追加 + + + { + setOpenDialog(true); + //handleNavigate('/AddDishies1'); + }} + //selected={isSelected('/test')} + > + + - > - - - {/* 新規タスク作成ダイアログ */} setOpenDialog(false)} disableScrollLock={true}> @@ -345,7 +411,7 @@ const TaskListPage: React.FC = () => { onChange={(e) => { const value = e.target.value; const parsedValue = parseInt(value, 10); // 数値に変換 - if (!isNaN(parsedValue)) { + if (!isNaN(parsedValue) && parsedValue >= 0) { //負数除外 setNewToBuy({ ...newToBuy, amount: parsedValue }); // number型で保存 } }} @@ -423,6 +489,52 @@ const TaskListPage: React.FC = () => { + + {/* 数量変更ダイアログ */} + setOpenAmountDialog(false)} disableScrollLock={true}> + + 数量の変更 + + + + + {/* 材料名表示 */} + + {/* 数量入力フィールド */} + { + const value = e.target.value; + const parsedValue = parseInt(value, 10); // 数値に変換 + if (!isNaN(parsedValue) && parsedValue >= 0) { //負数除外 + setSelectedEditingTask({ ...selectedEditingTask, amount: parsedValue }); // number型で保存 + } + }} + sx={{ width: "20%" }} + type="number" + inputProps={{ inputMode: "numeric", pattern: "[0-9]*" }} // ここで整数のみ許可 + + /> + + + + + + + ); diff --git a/frontend/src/services/api.ts b/frontend/src/services/api.ts index 59951bf..fa03a85 100644 --- a/frontend/src/services/api.ts +++ b/frontend/src/services/api.ts @@ -109,7 +109,7 @@ export const toBuyApi = { * @param tobuy 作成する材料情報 * @returns 作成された材料情報 */ - addToBuy: async (tobuy: Omit & { stuffId: number | null, category: string }): Promise => { + addToBuy:async (tobuy: Omit & { stuffId: number | null, category: string }): Promise => { const response = await fetch(`${API_BASE_URL}/api/tobuy/add`, { method: 'POST', headers: getHeaders(), @@ -154,6 +154,25 @@ export const toBuyApi = { // } }, + /** + * 買うものリストの変更 + * @param tobuy 変更する材料情報 + * @returns 変更された材料情報 + */ + updateToBuy: async (tobuy:ToBuy): Promise => { + const response = await fetch(`${API_BASE_URL}/api/tobuy/update`, { + method: 'PUT', + headers: getHeaders(), + body: JSON.stringify(tobuy), + }); + + if (!response.ok) { + throw new Error(TOBUY_ERRORS.CREATE_FAILED); + } + + return response.json(); + }, + /** * 買うものリストの在庫登録(購入処理) */