|
|
|
@ -29,15 +29,23 @@ import { |
|
|
|
|
} from '@mui/material'; |
|
|
|
|
import { |
|
|
|
|
Add as AddIcon, Delete as DeleteIcon, ShoppingBasket as ShoppingBasketIcon, |
|
|
|
|
SoupKitchen as SoupKitchenIcon, Edit as EditIcon, ListAlt as ListAltIcon |
|
|
|
|
SoupKitchen as SoupKitchenIcon, Edit as EditIcon, Save as SaveIcon, ListAlt as ListAltIcon |
|
|
|
|
} from '@mui/icons-material'; |
|
|
|
|
import AddStuffAmountDialog from '../components/AddStuffAmountDialog'; |
|
|
|
|
import { StuffAndCategoryAndAmount, StuffNameAndAmount } from '../types/types'; |
|
|
|
|
import { StuffAndCategoryAndAmount } from '../types/types'; |
|
|
|
|
import EditAmountDialog from '../components/EditAmountDialog'; |
|
|
|
|
import { recipeApi, toBuyApi } from '../services/api'; |
|
|
|
|
import { useNavigate, useParams } from 'react-router-dom'; |
|
|
|
|
|
|
|
|
|
const AddDishes2: React.FC = () => { |
|
|
|
|
// 料理名
|
|
|
|
|
const AddRecipe: React.FC = () => { |
|
|
|
|
const { recipeId: recipeIdStr } = useParams(); |
|
|
|
|
const recipeId = recipeIdStr ? parseInt(recipeIdStr) : null |
|
|
|
|
|
|
|
|
|
const navigate = useNavigate(); |
|
|
|
|
|
|
|
|
|
// 料理名,説明
|
|
|
|
|
const [recipeName, setRecipeName] = useState<string>(''); |
|
|
|
|
const [recipeSummary, setRecipeSummary] = useState<string>(''); |
|
|
|
|
// 材料リスト
|
|
|
|
|
const [items, setItems] = useState<StuffAndCategoryAndAmount[]>([]); |
|
|
|
|
// 材料追加作成ダイアログの表示状態
|
|
|
|
@ -51,21 +59,71 @@ const AddDishes2: React.FC = () => { |
|
|
|
|
const [editingItem, setEditingItem] = useState<StuffAndCategoryAndAmount>(emptyItem); |
|
|
|
|
const [editingItemIdx, setEditingItemIdx] = useState(0); |
|
|
|
|
|
|
|
|
|
const handleAddRecipeToBuy = () => { |
|
|
|
|
console.log('追加された材料:', items); |
|
|
|
|
const loadRecipe = async () => { |
|
|
|
|
if (recipeId && !recipeName) { |
|
|
|
|
const recipe = await recipeApi.getById(recipeId); |
|
|
|
|
console.log('loaded recipe=', recipe) |
|
|
|
|
setRecipeName(recipe.recipeName) |
|
|
|
|
setRecipeSummary(recipe.summary) |
|
|
|
|
setItems(recipe.stuffAndAmountArray) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
loadRecipe(); |
|
|
|
|
|
|
|
|
|
const handleSaveRecipe = async () => { |
|
|
|
|
|
|
|
|
|
if (!recipeId) { |
|
|
|
|
// 新規追加
|
|
|
|
|
const response = await recipeApi.addRecipe({ |
|
|
|
|
recipeName, |
|
|
|
|
summary: recipeSummary, |
|
|
|
|
stuffAndAmountArray: items, |
|
|
|
|
}) |
|
|
|
|
return response.recipeId; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const response = await recipeApi.updateRecipe({ |
|
|
|
|
recipeId, |
|
|
|
|
recipeName, |
|
|
|
|
summary: recipeSummary, |
|
|
|
|
stuffAndAmountArray: items, |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
return recipeId; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const handleSubmit = async () => { |
|
|
|
|
const recipeId = await handleSaveRecipe() |
|
|
|
|
alert('レシピが保存されました!') // 仮メッセージ
|
|
|
|
|
// navigate('/tasks');
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const handleSubmitAndAddToBuy = async () => { |
|
|
|
|
const recipeId = await handleSaveRecipe(); |
|
|
|
|
await toBuyApi.addByRecipe(recipeId) |
|
|
|
|
navigate('/tasks') |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return ( |
|
|
|
|
(recipeId && !recipeName) |
|
|
|
|
? <p>読み込み中...</p> |
|
|
|
|
: |
|
|
|
|
<Box> |
|
|
|
|
<div> |
|
|
|
|
<h1>料理の追加</h1> |
|
|
|
|
<h1> |
|
|
|
|
<SoupKitchenIcon sx={{ marginRight: "0.5em" }} /> |
|
|
|
|
{!recipeId ? '料理の追加' : '料理の編集'} |
|
|
|
|
</h1> |
|
|
|
|
<TextField autoFocus margin="dense" label="料理名" fullWidth |
|
|
|
|
value={recipeName} onChange={(e) => setRecipeName(e.target.value)} |
|
|
|
|
/> |
|
|
|
|
<TextField autoFocus margin="dense" label="説明" fullWidth |
|
|
|
|
value={recipeSummary} onChange={(e) => setRecipeSummary(e.target.value)} |
|
|
|
|
/> |
|
|
|
|
</div> |
|
|
|
|
<h2>材料リスト</h2> |
|
|
|
|
{/* すべての材料情報を表示 */} |
|
|
|
|
{!items.length |
|
|
|
|
{(!items || !items.length) |
|
|
|
|
? (<p>+ボタンで材料を追加してください</p>) |
|
|
|
|
: (<List>{items.map((item, index) => ( |
|
|
|
|
|
|
|
|
@ -124,10 +182,13 @@ const AddDishes2: React.FC = () => { |
|
|
|
|
</div> |
|
|
|
|
|
|
|
|
|
<div style={{ position: "fixed", left: "50%", transform: 'translateX(-50%)', bottom: "2%" }}> |
|
|
|
|
<Button variant='contained' sx={{ fontSize: "1.0rem" }} |
|
|
|
|
color="primary" onClick={handleAddRecipeToBuy}> |
|
|
|
|
<Button variant='contained' color="primary" onClick={handleSubmit} sx={{ marginRight: "1rem" }}> |
|
|
|
|
<SaveIcon sx={{ fontSize: "1.5rem", marginRight: "0.5rem" }} /> |
|
|
|
|
レシピを保存 |
|
|
|
|
</Button> |
|
|
|
|
<Button variant='contained' color="primary" onClick={handleSubmitAndAddToBuy}> |
|
|
|
|
<ListAltIcon sx={{ fontSize: "1.5rem", marginRight: "0.5rem" }} /> |
|
|
|
|
料理リストへ追加 |
|
|
|
|
保存して買うものリストへ追加 |
|
|
|
|
</Button> |
|
|
|
|
</div> |
|
|
|
|
|
|
|
|
@ -152,4 +213,4 @@ const AddDishes2: React.FC = () => { |
|
|
|
|
); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
export default AddDishes2; |
|
|
|
|
export default AddRecipe; |