Skip to content

Commit

Permalink
Allow users to delete a recipe
Browse files Browse the repository at this point in the history
  • Loading branch information
alex9smith committed Nov 25, 2024
1 parent f66df6c commit 19b67cb
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 5 deletions.
58 changes: 53 additions & 5 deletions frontend/src/components/RecipeDetails/RecipeDetails.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { useLoaderData } from "react-router";
import { useLoaderData, useNavigate } from "react-router";
import { useState } from "react";
import { LabelGroup, Heading, Box, Button } from "@primer/react";
import {
LabelGroup,
Heading,
Box,
Button,
Dialog,
ButtonGroup,
Spinner,
} from "@primer/react";
import FullWidthPage from "../FullWidthPage/FullWidthPage";
import BorderBox from "../BorderBox/BorderBox";
import CategoryLabel from "../CategoryLabel/CategoryLabel";
Expand All @@ -9,10 +17,20 @@ import LengthLabel from "../LengthLabel/LengthLabel";
import Source from "../Source/Source";
import Ingredients from "../Ingredients/Ingredients";
import AddOrEditRecipe from "../AddNewOrEditRecipe/AddNewOrEditRecipe";
import { apiClient } from "../../services/apiClient";

function RecipeDetails() {
const navigate = useNavigate();
const recipe = useLoaderData().recipe;
const [isEditing, setIsEditing] = useState(false);
const [isDeleting, setIsDeleting] = useState(false);
const [showDeletePopup, setShowDeletePopup] = useState(false);

async function deleteRecipe() {
setIsDeleting(true);
await apiClient.deleteRecipe(recipe.id);
navigate("/");
}

const displayRecipe = (
<Box>
Expand All @@ -28,9 +46,39 @@ function RecipeDetails() {
<Source source={recipe.source} />
<Ingredients recipe={recipe} />
</BorderBox>
<Button variant="primary" onClick={() => setIsEditing(true)}>
Edit recipe
</Button>
<ButtonGroup>
<Button variant="primary" onClick={() => setIsEditing(true)}>
Edit recipe
</Button>
<Button variant="danger" onClick={() => setShowDeletePopup(true)}>
Delete recipe
</Button>
</ButtonGroup>

{showDeletePopup && (
<Dialog
title="Are you sure?"
onClose={() => setShowDeletePopup(false)}
footerButtons={[
{
buttonType: "primary",
content: "Close",
onClick: () => setShowDeletePopup(false),
},
{
buttonType: "danger",
content: "Delete recipe",
onClick: deleteRecipe,
},
]}
>
{isDeleting ? (
<Spinner />
) : (
"Are you sure you want to delete this recipe?"
)}
</Dialog>
)}
</Box>
);

Expand Down
19 changes: 19 additions & 0 deletions frontend/src/services/apiClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,25 @@ class ApiClient {
}
this.#cacheItem("plan", plan);
}

async deleteRecipe(id) {
const response = await fetch(
this.baseUrl + this.prefix + `/recipes/${id}`,
{
method: "DELETE",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
}
);

if (!response.ok) {
throw new Error(`Error deleting recipe - status: ${response.status}`);
}
const allRecipes = await this.getAllRecipes();
this.#cacheItem("recipes", allRecipes);
}
}

export const apiClient = new ApiClient(getApiBaseUrl());

0 comments on commit 19b67cb

Please sign in to comment.