Skip to content

Commit

Permalink
implement delete recipe frontend b00tc4mp#407
Browse files Browse the repository at this point in the history
  • Loading branch information
berlem committed Apr 18, 2024
1 parent 9398ac4 commit a18b536
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 10 deletions.
6 changes: 4 additions & 2 deletions staff/belen-ivars/project/api/handlers/deleteRecipeHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ContentError, NotFoundError } from "com/errors.js"
import logic from "../logic/index.js"
import jwt from 'jsonwebtoken'

export default async (req, res) => {
const deleteRecipeHandler = async (req, res) => {
const recipeId = req.params.recipeId
const token = req.headers.authorization.substring(7)

Expand All @@ -21,4 +21,6 @@ export default async (req, res) => {

res.status(status).json({ error: error.constructor.name, message: error.message })
}
}
}

export default deleteRecipeHandler
2 changes: 0 additions & 2 deletions staff/belen-ivars/project/api/logic/editRecipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ async function editRecipe(userId, recipeId, title, description, image) {
const user = await User.findById(userId)
if (!user)
throw new NotFoundError('user not found')
console.log('user founded')

const recipe = await Recipe.findById(recipeId)
if (!recipe)
throw new NotFoundError('recipe not found')
console.log('recipe founded')

let recipeUpdated

Expand Down
25 changes: 22 additions & 3 deletions staff/belen-ivars/project/app/src/components/Recipe.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useNavigate } from 'react-router-dom'

import logic from "../logic"

import { Button, Field, Form, Link } from "../library"
import { Button, Container, Field, Form, Link } from "../library"
import { useContext } from '../hooks'

import session from '../logic/session'
Expand Down Expand Up @@ -35,6 +35,18 @@ function Recipe(props) {
}
}

const handleDeleteClick = async (event) => {
event.preventDefault()

try {
await logic.deleteRecipe(props.recipe._id)
props.onSuccess()

} catch (error) {
console.log(error)
}
}

useEffect(() => {

}, [handleSubmit])
Expand All @@ -46,9 +58,13 @@ function Recipe(props) {
<img className="recipe-image" src={props.recipe.image} />
<p>{props.recipe.description}</p>

{/* Ací estic configurant el botó d'edició*/}

{session.sessionUserId === props.recipe.author && view === null && <Button onClick={handleDeleteClick}>🗑️</Button>}
{session.sessionUserId === props.recipe.author && view === null && <Button className="edit-recipe" onClick={() => setView('edit')}>Edit</Button>}

{view === 'edit' && <Button onClick={() => setView(null)}>Cancel</Button>}

{view === 'edit' ? <Button onClick={() => setView(null)}>Cancel</Button> : <Button className="edit-recipe" onClick={() => setView('edit')}>Edit</Button>
}
{view === 'edit' && <Form id='edit-form' onSubmit={handleSubmit}>
<Field type='text' id='title' placeholder={props.recipe.title} />
<Field type='text' id='description' placeholder={props.recipe.description} />
Expand All @@ -57,6 +73,9 @@ function Recipe(props) {

</Form>}




</article>
}

Expand Down
35 changes: 35 additions & 0 deletions staff/belen-ivars/project/app/src/logic/deleteRecipe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { SystemError } from "com/errors"
import { API_URL } from "../utils/constants"
import session from "./session"
import { errors } from 'com'


export default function deleteRecipe(recipeId) {
return (async () => {
const req = {
method: 'DELETE',
headers: {
Authorization: `Bearer ${session.token}`,
}
}

let res
try {
res = await fetch(`${API_URL}/recipes/${recipeId}`, req)
} catch (error) {
throw new SystemError(error.message)
}

if (!res.ok) {
let body

try {
body = await res.json()
} catch (error) {
throw new SystemError(errors.message)
}

throw new errors[body.error](body.message)
}
})()
}
4 changes: 3 additions & 1 deletion staff/belen-ivars/project/app/src/logic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import isUserLoggedIn from './isUserLoggedIn'
import createRecipe from './createRecipe'
import retrieveRecipes from './retrieveRecipes'
import editRecipe from './editRecipe'
import deleteRecipe from './deleteRecipe'

const logic = {
registerUser,
Expand All @@ -15,7 +16,8 @@ const logic = {
isUserLoggedIn,
createRecipe,
retrieveRecipes,
editRecipe
editRecipe,
deleteRecipe
}

export default logic
3 changes: 1 addition & 2 deletions staff/belen-ivars/project/app/src/pages/Home.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,9 @@ function Home(props) {
<Route path='/favs' element={<FavsUser />} />
<Route path='/new-recipe' element={<NewRecipe />} />
<Route path='/' element={<Recipes stamp={stamp} />} />
</Routes>



</Routes>
<footer className="footer">
<Container className="footer-menu">
{view === 'new-recipe' && <NewRecipe onPublish={handleNewRecipePublish} onCancel={handleNewRecipeCancel} />}
Expand Down

0 comments on commit a18b536

Please sign in to comment.