料理リストにAPIの内容を表示

feature-frontend-dishedit
masato.fujita 4 months ago
parent 138df3084a
commit dc6ab8662f
  1. 144
      frontend/src/pages/DishList.tsx

@ -56,15 +56,40 @@ const DishList: React.FC = () => {
{ recipeId: 3, recipeName: '人参', summary:2 },
{ recipeId: 4, recipeName: '人参', summary:2 }
];
interface RecipeInfo {
recipeId: number;
recipeName: string;
summary: string;
stuffs: Array<{
stuffId: number; stuffName: string; amount: number
}>;
}
// interface testdish {
// recipeId: number,
// recipeName: string,
// summary: number
// }
const EMPTY_RECIPE: RecipeInfo = {
recipeId: 0,
recipeName: '',
summary: '',
stuffs: []
}
interface Stuff {
stuffId: number;
stuffName: string;
amount: number;
}
// すべての料理リスト
const [allDish, setAllDish] = useState<Dish[]>(testdish);
const [allDish, setAllDish] = useState<Dish[]>();
// 食材リスト
const [stuff, setStuff] = useState<Stuff[]>([]);
// 料理名
const [selectedDishName, setSelectedDishName] = useState('');
// 料理情報
const [selectedrecipeInfo, setSelectedRecipeInfo] = useState<RecipeInfo>({recipeId: 0,
recipeName: '',
summary: '',
stuffs: []});
// 画面表示用の食材情報配列
const [testDishArray, setTestDishArray] = useState<Dish[]>(testdish);
// エラーメッセージの状態管理
@ -76,7 +101,16 @@ const DishList: React.FC = () => {
// 料理リストの食材の個数編集ダイアログの表示状態
const [openChangeAmountDialog, setOpenChangeAmountDialog] = useState(false);
// 編集する食材の状態
const [selectedEditingStuff, setSelectedEditingStuff] = useState<Dish>();
const [selectedEditingStuff, setSelectedEditingStuff] = useState<Stuff>();
// 食材表示ダイアログを表示
const openStuffDialog = (recipeId : number) => {
setOpenDialog(true)
selectedDishId = recipeId;
fetchstuff(selectedDishId);
console.log(selectedDishId);
}
const changeDialog = () => {
setOpenDialog(false);
@ -88,26 +122,43 @@ const DishList: React.FC = () => {
setOpenDialog(true);
setOpenChangeDialog(false);
};
// 選択された料理のID
let selectedDishId : number = 0;
// 選択された食材
let selectedItem : string = '';
// 選択された食材の個数
let selectedItemAmount : number = 0;
// 選択された食材の個数を変更するダイアログを開く処理
const changeSelectedItemAmount = (dish : Dish, stuff_name : string, stuff_amount : number) => {
const changeSelectedItemAmount = (stuff : Stuff, stuff_name : string, stuff_amount : number) => {
setOpenChangeAmountDialog(true)
setSelectedEditingStuff(dish);
setSelectedEditingStuff(stuff);
selectedItem = stuff_name;
selectedItemAmount = stuff_amount;
};
const fetchstuff = async (recipeId : number) => {
try {
const dish = await recipeApi.getById(recipeId);
console.log(dish);
// 情報の取得にラグがあるバグあり。変数に格納するのは諦めるべきか
// setSelectedRecipeInfo(dish);
console.log(selectedrecipeInfo);
setSelectedDishName(dish.recipeName);
setStuff(dish.stuffs);
console.log(selectedDishName);
} catch (error) {
alert("取得失敗");
// console.error(`${TASK_ERRORS.FETCH_FAILED}:`, error);
}
};
/**
*
* IDのタスクをAPIを通じて削除
*/
const handleDeleteStuffTemp = async (dish_id: number) => {
const handleDeleteStuffTemp = async (stuff_id: number) => {
try {
let testDishArrayCopy = [...testDishArray]; // 配列をコピー
testDishArrayCopy.splice(dish_id, 1);
setTestDishArray(testDishArrayCopy);
let stuffCopy = [...stuff]; // 配列をコピー
stuffCopy.splice(stuff_id, 1);
setStuff(stuff);
// fetchTasks(); // 削除後のタスク一覧を再取得
} catch (error) {
// console.error(`${TASK_ERRORS.DELETE_FAILED}:`, error);
@ -133,8 +184,8 @@ const DishList: React.FC = () => {
const fetchTasks = async () => {
try {
// const dishlistfirst = await recipeApi.getAllRecipes();
// setAllDish(dishlistfirst);
const dishlistfirst = await recipeApi.getAllRecipes();
setAllDish(dishlistfirst);
} catch (error) {
alert("取得失敗");
// console.error(`${TASK_ERRORS.FETCH_FAILED}:`, error);
@ -164,7 +215,7 @@ const DishList: React.FC = () => {
boxShadow: 1,
fontSize: '40px'
}}
onClick={() => setOpenDialog(true)}
onClick={() => openStuffDialog(allDish.recipeId)}
>
{allDish.recipeName}
{/* タスクのタイトルと説明 - 完了状態に応じて取り消し線を表示 */}
@ -207,34 +258,31 @@ const DishList: React.FC = () => {
fullWidth
>
<Box display="flex" alignItems="center">
<DialogTitle sx={{ flexGrow: 1 }}></DialogTitle>
<DialogTitle sx={{ flexGrow: 1 }}>{selectedDishName}</DialogTitle>
</Box>
<FormGroup>
{/* <List> */}
{/* タスク一覧をマップして各タスクをリストアイテムとして表示 */}
{test.map((test, index) => (
<Button onClick={() => setOpenDialog(true)} key = {index}>
{test}
</Button>
// <ListItem
// key={index}
// sx={{
// bgcolor: 'background.paper',
// mb: 1,
// borderRadius: 1,
// boxShadow: 2,
// }}
// >
// {/* タスクのタイトルと説明 - 完了状態に応じて取り消し線を表示 */}
// <ListItemText
// primary={test}
// //secondary={tobuy.amount}
// primaryTypographyProps={{ fontSize: '40px', margin: '10px' }}
// sx={{
// textDecoration: false ? 'line-through' : 'none',
// }}
// />
// </ListItem>
{/* 食材をマップして各食材をリストアイテムとして表示 */}
{stuff.map((stuff, stuffId) => (
<ListItem
key = {stuffId}
sx={{
bgcolor: 'background.paper',
mb: 1,
borderRadius: 1,
boxShadow: 2,
}}
>
{/* タスクのタイトルと説明 - 完了状態に応じて取り消し線を表示 */}
<ListItemText
primary={stuff.stuffName}
//secondary={tobuy.amount}
primaryTypographyProps={{ fontSize: '40px', margin: '10px' }}
sx={{
textDecoration: false ? 'line-through' : 'none',
}}
/>
</ListItem>
))}
{/* </List> */}
</FormGroup>
@ -253,14 +301,14 @@ const DishList: React.FC = () => {
fullWidth
>
<Box display="flex" alignItems="center">
<DialogTitle sx={{ flexGrow: 1 }}></DialogTitle>
<DialogTitle sx={{ flexGrow: 1 }}>{selectedDishName}</DialogTitle>
</Box>
<FormGroup>
{/* <List> */}
{/* タスク一覧をマップして各タスクをリストアイテムとして表示 */}
{testDishArray.map((test, id) => (
{stuff.map((stuff, stuffId) => (
<ListItem
key={id}
key={stuffId}
sx={{
bgcolor: 'background.paper',
mb: 1,
@ -275,14 +323,14 @@ const DishList: React.FC = () => {
/> */}
{/* タスクのタイトルと説明 - 完了状態に応じて取り消し線を表示 */}
<ListItemText
primary={test.recipeName}
primary={stuff.stuffName}
// secondary={task.description}
sx={{width: '80%'}}
/>
{/* 食材の個数を表示 */}
<ListItemText
primary={<Typography style={{textAlign:"center"}}><br />
{test.summary}
{stuff.amount}
</Typography>
}
// secondary={task.description}
@ -294,7 +342,7 @@ const DishList: React.FC = () => {
<IconButton
edge="end"
aria-label="個数を編集"
onClick={() => changeSelectedItemAmount(test, test.recipeName, test.summary)}
onClick={() => changeSelectedItemAmount(stuff, stuff.stuffName, stuff.amount)}
>
<ShoppingBasketIcon />
</IconButton>
@ -317,7 +365,7 @@ const DishList: React.FC = () => {
<IconButton
edge="end"
aria-label="delete"
onClick={() => handleDeleteStuffTemp(id)}
onClick={() => handleDeleteStuffTemp(stuffId)}
>
<DeleteIcon />
</IconButton>

Loading…
Cancel
Save