材料の追加の実装

dev-backend-tobuy
Masaharu.Kato 9 months ago
parent 366637bb76
commit 50813ac9eb
  1. 16
      frontend/src/components/StuffNameDropDown.tsx
  2. 85
      frontend/src/pages/TaskListPage.tsx
  3. 2
      frontend/src/services/api.ts

@ -1,16 +0,0 @@
import React from "react";
import { toBuyApi } from '../services/api';
import { FormControl, InputLabel, Select, MenuItem } from "@mui/material";
const StuffNameDropDown = ({ value, onChange }) => {
return (
<FormControl fullWidth>
<InputLabel></InputLabel>
<Select value={value} onChange={onChange}>
<MenuItem value="じゃがいも"></MenuItem>
</Select>
</FormControl>
);
};
export default StuffNameDropDown;

@ -3,7 +3,7 @@
* *
*/ */
import React, { useState, useEffect } from 'react'; import React, { useState, useEffect } from 'react';
import { toBuyApi } from '../services/api'; import { toBuyApi, stuffApi } from '../services/api';
import { import {
Container, Container,
Typography, Typography,
@ -23,13 +23,17 @@ import {
Button, Button,
Box, Box,
FormControlLabel, FormControlLabel,
FormGroup FormGroup,
FormControl,
InputLabel,
Select,
MenuItem
} from '@mui/material'; } from '@mui/material';
import { import {
Add as AddIcon, Delete as DeleteIcon, ShoppingBasket as ShoppingBasketIcon, Add as AddIcon, Delete as DeleteIcon, ShoppingBasket as ShoppingBasketIcon,
SoupKitchen as SoupKitchenIcon SoupKitchen as SoupKitchenIcon
} from '@mui/icons-material'; } from '@mui/icons-material';
import { Task, ToBuy } from '../types/types'; import { Task, ToBuy, Stuff } from '../types/types';
import { TASK_ERRORS } from '../constants/errorMessages'; import { TASK_ERRORS } from '../constants/errorMessages';
//import { FaCarrot } from "react-icons/fa6"; //エラー起きる いったん保留 //import { FaCarrot } from "react-icons/fa6"; //エラー起きる いったん保留
import CategoryDropDown from "../components/CategoryDropDown"; import CategoryDropDown from "../components/CategoryDropDown";
@ -38,8 +42,8 @@ import CategoryDropDown from "../components/CategoryDropDown";
// 新規タスクの初期状態 // 新規タスクの初期状態
const EMPTY_TASK: Omit<ToBuy, 'tobuy_id'> & {category: string} & {newAddition: boolean} = { const EMPTY_TASK: Omit<ToBuy, 'tobuy_id' | 'stuff_id'> & { stuff_id: number | null, category: string } & { newAddition: boolean } = {
stuff_id: 0, stuff_id: null,
stuff_name: '', stuff_name: '',
amount: 0, amount: 0,
shop: '', shop: '',
@ -55,12 +59,15 @@ const TaskListPage: React.FC = () => {
//在庫登録ダイアログの表示状態 //在庫登録ダイアログの表示状態
const [openInfoDialog, setOpenInfoDialog] = useState(false); const [openInfoDialog, setOpenInfoDialog] = useState(false);
const [selectedTask, setSelectedTask] = useState<ToBuy>(); const [selectedTask, setSelectedTask] = useState<ToBuy>();
const [newToBuy, setNewToBuy] = useState(EMPTY_TASK); const [newToBuy, setNewToBuy] = useState(EMPTY_TASK);
const [stuffs, setStuffs] = useState<Stuff[]>([]);
// コンポーネントマウント時にタスク一覧を取得 // コンポーネントマウント時にタスク一覧を取得
useEffect(() => { useEffect(() => {
fetchTasks(); fetchTasks();
@ -79,6 +86,12 @@ const TaskListPage: React.FC = () => {
} }
}; };
const onChangeCategory = async (category: string) => {
setNewToBuy({ ...newToBuy, category })
const result = await stuffApi.getStuffs(category)
setStuffs(result.stuff_array)
}
// /** // /**
// * タスクの完了状態を切り替えるハンドラー // * タスクの完了状態を切り替えるハンドラー
// * 対象タスクの完了状態を反転させてAPIに更新を要求 // * 対象タスクの完了状態を反転させてAPIに更新を要求
@ -222,23 +235,53 @@ const TaskListPage: React.FC = () => {
{/* 新規タスク作成ダイアログ */} {/* 新規タスク作成ダイアログ */}
<Dialog open={openDialog} onClose={() => setOpenDialog(false)} disableScrollLock={true}> <Dialog open={openDialog} onClose={() => setOpenDialog(false)} disableScrollLock={true}>
<Box display="flex" alignItems="center"> <Box display="flex" alignItems="center">
<DialogTitle sx={{ flexGrow: 1 }}></DialogTitle> <DialogTitle sx={{ flexGrow: 1 }}></DialogTitle>
<FormGroup row> <FormGroup row>
<FormControlLabel <FormControlLabel
control={<Checkbox />} control={<Checkbox />}
label="食材を新規追加" label="食材を新規追加"
checked={newToBuy.newAddition} checked={newToBuy.newAddition}
onChange={(e) => setNewToBuy({ ...newToBuy, newAddition:(e.target as HTMLInputElement).checked })} onChange={(e) => setNewToBuy({ ...newToBuy, newAddition: (e.target as HTMLInputElement).checked })}
/> />
</FormGroup> </FormGroup>
</Box> </Box>
<DialogContent> <DialogContent>
<Box sx={{ pt: 1 }}> <Box sx={{ pt: 1 }}>
{/*材料カテゴリ選択 */} {/*材料カテゴリ選択 */}
<CategoryDropDown></CategoryDropDown>
<FormControl sx={{ width: "50%", marginBottom: 2 }}>
<InputLabel id="demo-simple-select-label"></InputLabel>
<Select
labelId="demo-simple-select-label"
value={newToBuy.category}
onChange={(e) => onChangeCategory(e.target.value) }
>
<MenuItem value="乳製品"></MenuItem>
<MenuItem value="肉・魚"></MenuItem>
<MenuItem value="野菜"></MenuItem>
<MenuItem value="調味料">調</MenuItem>
<MenuItem value="その他"></MenuItem>
</Select>
</FormControl>
{!newToBuy.newAddition && <FormControl sx={{ width: "50%", marginBottom: 2 }}>
<InputLabel id="demo-simple-select-label"></InputLabel>
<Select
labelId="demo-simple-select-label"
value={newToBuy.stuff_id}
onChange={(e) => setNewToBuy({ ...newToBuy, stuff_id: Number(e.target.value) })}
>
{stuffs.map((stuff) => (
<MenuItem key={stuff.stuff_id} value={stuff.stuff_id}>
{stuff.stuff_name}
</MenuItem>
))}
</Select>
</FormControl>}
{/* タスクタイトル入力フィールド */} {/* タスクタイトル入力フィールド */}
<TextField {newToBuy.newAddition && <TextField
autoFocus autoFocus
margin="dense" margin="dense"
label="材料名" label="材料名"
@ -246,7 +289,7 @@ const TaskListPage: React.FC = () => {
value={newToBuy.stuff_name} value={newToBuy.stuff_name}
onChange={(e) => setNewToBuy({ ...newToBuy, stuff_name: e.target.value })} onChange={(e) => setNewToBuy({ ...newToBuy, stuff_name: e.target.value })}
sx={{ marginBottom: 2 }} sx={{ marginBottom: 2 }}
/> />}
{/* 数量入力フィールド */} {/* 数量入力フィールド */}
<TextField <TextField
margin="dense" margin="dense"
@ -310,14 +353,14 @@ const TaskListPage: React.FC = () => {
handleDeleteTask(selectedTask.tobuy_id) handleDeleteTask(selectedTask.tobuy_id)
setOpenInfoDialog(false) setOpenInfoDialog(false)
} }
}} }}
variant="contained"> variant="contained">
</Button> </Button>
</DialogActions> </DialogActions>
</Dialog> </Dialog>
</Container> </Container>
); );
}; };

@ -128,7 +128,7 @@ export const toBuyApi = {
* @param tobuy * @param tobuy
* @returns * @returns
*/ */
addToBuy: async (tobuy: Omit<ToBuy, 'tobuy_id'> & { category: string }): Promise<any> => { addToBuy: async (tobuy: Omit<ToBuy, 'stuff_id' | 'tobuy_id'> & { stuff_id: number | null, category: string }): Promise<any> => {
// const response = await fetch(`${API_BASE_URL}/api/tasks`, { // const response = await fetch(`${API_BASE_URL}/api/tasks`, {
// method: 'POST', // method: 'POST',
// headers: getHeaders(), // headers: getHeaders(),

Loading…
Cancel
Save