-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kyle Zarazan
committed
Nov 21, 2024
1 parent
c8ed5cb
commit 5e2d735
Showing
7 changed files
with
189 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import { useAppDispatch, useAppSelector } from '../../store/hooks'; | ||
import { updateRecipe } from '../../store/recipesSlice'; | ||
import { RecipeFormData } from './RecipeNew'; | ||
import useCsrfToken from '../../hooks/useCsrfToken'; | ||
import { useNavigate, useParams } from 'react-router-dom'; | ||
import RecipeForm from './RecipeForm'; | ||
import { fetchFoods } from '../../store/foodsSlice'; | ||
|
||
const EditRecipeForm: React.FC = () => { | ||
const dispatch = useAppDispatch(); | ||
const csrfToken = useCsrfToken(); | ||
const navigate = useNavigate(); | ||
const { id } = useParams<{ id: string }>(); | ||
|
||
const recipe = useAppSelector(state => | ||
state.recipes.items.find(recipe => recipe.id === Number(id)) | ||
); | ||
|
||
const [formData, setFormData] = useState<RecipeFormData>({ | ||
name: '', | ||
description: '', | ||
ingredients_attributes: [] | ||
}); | ||
|
||
useEffect(() => { | ||
dispatch(fetchFoods()); | ||
|
||
if (recipe) { | ||
setFormData({ | ||
name: recipe.name, | ||
description: recipe.description, | ||
ingredients_attributes: recipe.ingredients | ||
}); | ||
} | ||
}, [dispatch, recipe]); | ||
|
||
const handleSubmit = (e: React.FormEvent) => { | ||
e.preventDefault(); | ||
dispatch(updateRecipe({ | ||
id: Number(id), | ||
data: formData, | ||
csrfToken | ||
})) | ||
.unwrap() | ||
.then(() => { | ||
navigate('/recipes'); | ||
}) | ||
.catch((error: Error) => { | ||
alert('Failed to update recipe: ' + error.message); | ||
}); | ||
}; | ||
|
||
if (!recipe) { | ||
return <div>Recipe not found</div>; | ||
} | ||
|
||
return ( | ||
<RecipeForm | ||
formData={formData} | ||
setFormData={setFormData} | ||
onSubmit={handleSubmit} | ||
submitButtonText="Update Recipe" | ||
/> | ||
); | ||
}; | ||
|
||
export default EditRecipeForm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import { useAppDispatch } from '../../store/hooks'; | ||
import { createRecipe } from '../../store/recipesSlice'; | ||
import { fetchFoods } from '../../store/foodsSlice'; | ||
import useCsrfToken from '../../hooks/useCsrfToken'; | ||
import { useNavigate } from 'react-router-dom'; | ||
import RecipeForm from './RecipeForm'; | ||
import { Ingredient } from '../../types/types'; | ||
|
||
export interface RecipeFormData { | ||
name: string; | ||
description: string; | ||
ingredients_attributes: Ingredient[]; | ||
} | ||
|
||
const NewRecipeForm: React.FC = () => { | ||
const dispatch = useAppDispatch(); | ||
const csrfToken = useCsrfToken(); | ||
const navigate = useNavigate(); | ||
|
||
useEffect(() => { | ||
clearForm(); | ||
dispatch(fetchFoods()); | ||
}, [dispatch]); | ||
|
||
const [formData, setFormData] = useState<RecipeFormData>({ | ||
name: '', | ||
description: '', | ||
ingredients_attributes: [] | ||
}); | ||
|
||
const clearForm = () => { | ||
setFormData({ | ||
name: '', | ||
description: '', | ||
ingredients_attributes: [] | ||
}); | ||
}; | ||
|
||
const handleSubmit = (e: React.FormEvent) => { | ||
e.preventDefault(); | ||
dispatch(createRecipe({ | ||
data: formData, | ||
csrfToken | ||
})) | ||
.unwrap() | ||
.then(() => { | ||
navigate('/recipes'); | ||
}) | ||
.catch((error: Error) => { | ||
alert('Failed to create recipe: ' + error.message); | ||
}); | ||
}; | ||
|
||
return ( | ||
<RecipeForm | ||
formData={formData} | ||
setFormData={setFormData} | ||
onSubmit={handleSubmit} | ||
submitButtonText="Create Recipe" | ||
/> | ||
); | ||
}; | ||
|
||
export default NewRecipeForm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters