Acreated ddByRecipeDialog

feature_frontend_dishList_ui
Amagasu 4 months ago
parent 21bdbace3c
commit c3594a7575
  1. 78
      frontend/src/components/AddByRecipeDialog.tsx
  2. 10
      frontend/src/pages/RecipeList.tsx

@ -1,4 +1,4 @@
import React, { useState } from 'react'; import React, { useEffect, useState } from 'react';
import { import {
Dialog, Dialog,
DialogTitle, DialogTitle,
@ -8,23 +8,45 @@ import {
Box, Box,
} from '@mui/material'; } from '@mui/material';
import { toBuyApi } from '../services/api';
import { recipeApi } from '../services/api'; import { recipeApi } from '../services/api';
import CloseIcon from '@mui/icons-material/Close'; import CloseIcon from '@mui/icons-material/Close';
import { StuffNameAndAmount } from '../types/types'; import { RecipeDetailWithId, StuffNameAndAmount } from '../types/types';
const AddByRecipeDialog = async ({ const AddByRecipeDialog = ({
openDialog, openDialog,
setOpenDialog, setOpenDialog,
recipeId recipeId,
numOfPeople,
setNumOfPeaple,
checked,
setChecked
}: { }: {
openDialog: boolean, openDialog: boolean,
setOpenDialog: (open: boolean) => void, setOpenDialog: (open: boolean) => void,
recipeId: number, recipeId: number,
numOfPeople: number,
setNumOfPeaple: (num: number) => void,
checked: boolean,
setChecked: (checked: boolean) => void
}) => { }) => {
const recipe = await recipeApi.getById(recipeId); const [recipe, setRecipe] = useState<RecipeDetailWithId>();
useEffect(() => {
console.log("called AddByRecipeDialog useEffect recipeId: ", recipeId);
if (recipeId) {
const fetchRecipe = async () => {
console.log("Fetching recipe with ID:", recipeId);
setRecipe(await recipeApi.getById(recipeId));
};
fetchRecipe();
}
}, [recipeId]);
return ( return (
recipe ?
<Dialog open={openDialog} onClose={() => setOpenDialog(false)} disableScrollLock={true} PaperProps={{ sx: { minWidth: '300px', maxHeight: '80vh' } }}> <Dialog open={openDialog} onClose={() => setOpenDialog(false)} disableScrollLock={true} PaperProps={{ sx: { minWidth: '300px', maxHeight: '80vh' } }}>
<DialogTitle sx={{ m: 0, p: 2, display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}> <DialogTitle sx={{ m: 0, p: 2, display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
@ -42,12 +64,12 @@ const AddByRecipeDialog = async ({
<strong>:</strong> {recipe.recipeName} <strong>:</strong> {recipe.recipeName}
</div> </div>
<div> <div>
({recipe.maxservings}) ({recipe.maxServings})
</div> </div>
<div> <div>
<strong>:</strong> <strong>1:</strong>
<ul> <ul>
{recipe.stuffNameAndAmountList.map((item, index) => ( {recipe.stuffAndAmountArray.map((item, index) => (
<li key={index}> <li key={index}>
{item.stuffName} - {item.amount} {item.stuffName} - {item.amount}
</li> </li>
@ -55,17 +77,47 @@ const AddByRecipeDialog = async ({
</ul> </ul>
</div> </div>
{/* レシピを追加する人数分を入力 */} {/* 在庫との差分を取るかのチェックボックス */}
<div>
<label>
<input
type="checkbox"
checked={checked}
onChange={(e) => setChecked(e.target.checked)}
/>
</label>
</div>
{/* レシピを追加する人数分を入力 */}
<div> <div>
<strong></strong> <strong>:</strong>
<input
type="number"
min="1"
defaultValue={1}
onChange={(e) => {
setNumOfPeaple(parseInt(e.target.value, 10));
}}
/>
</div> </div>
{/* 買うものリストに追加するボタン */}
<Box sx={{ mt: 2 }}>
<Button
variant="contained"
color="primary"
onClick={() => {
toBuyApi.addByRecipe(recipe.recipeId, numOfPeople, checked);
setOpenDialog(false);
}}
>
</Button>
</Box>
</DialogContent> </DialogContent>
<DialogActions>
<button onClick={() => setOpenDialog(false)}></button>
</DialogActions>
</Dialog> </Dialog>
: <></>
); );
} }

@ -52,6 +52,8 @@ const RecipeList: React.FC = () => {
const [openAddByRecipeDialog, setOpenAddByRecipeDialog] = useState(false); const [openAddByRecipeDialog, setOpenAddByRecipeDialog] = useState(false);
const [addByRecipeId, setAddByRecipeId] = useState(0); const [addByRecipeId, setAddByRecipeId] = useState(0);
const [numOfPeople, setNumOfPeople] = useState(1);
const [checked, setChecked] = useState(false);
const openRecipeById = (recipeId: number) => { const openRecipeById = (recipeId: number) => {
navigate('/addRecipe/' + recipeId); navigate('/addRecipe/' + recipeId);
@ -102,7 +104,9 @@ const RecipeList: React.FC = () => {
fontSize: '40px' fontSize: '40px'
}} }}
onClick = {() => onClick = {() =>
{setAddByRecipeId(recipe.recipeId) {
console.log("recipeId: ", recipe.recipeId);
setAddByRecipeId(recipe.recipeId)
setOpenAddByRecipeDialog(true)} setOpenAddByRecipeDialog(true)}
} }
> >
@ -176,7 +180,9 @@ const RecipeList: React.FC = () => {
{/* 買うものリストへ追加ダイアログ */} {/* 買うものリストへ追加ダイアログ */}
{ {
<AddByRecipeDialog openDialog={openAddByRecipeDialog} setOpenDialog={setOpenAddByRecipeDialog} <AddByRecipeDialog openDialog={openAddByRecipeDialog} setOpenDialog={setOpenAddByRecipeDialog}
recipeId = { addByRecipeId}/> recipeId = {addByRecipeId} numOfPeople={numOfPeople} setNumOfPeaple={setNumOfPeople}
checked = {checked} setChecked={setChecked}
/>
} }
</> </>
); );

Loading…
Cancel
Save