source: CookCraft-FrontEnd/CookCraft-FrontEnd-master/cookcraft-app/src/components/ProfileComponents/FavoriteRecipesSection.jsx

Last change on this file was d7b7f00, checked in by Gorazd Biskoski <gorazdbiskoskii@…>, 4 weeks ago

Add project

  • Property mode set to 100644
File size: 2.1 KB
Line 
1import styles from '../../css/ProfileCss/profile-view-style.module.css';
2import FavoriteCard from "./FavoriteCard";
3import {useEffect, useState} from "react";
4
5const FavoriteRecipesSection = () => {
6
7 const [favoriteRecipes, setFavoriteRecipes] = useState([])
8
9 useEffect(() => {
10
11 const fetchFavoriteRecipes = async () => {
12 const token = localStorage.getItem("token")
13 if (!token) {
14 alert("User not authorized.")
15 return;
16 }
17
18 try {
19 const response = await fetch(`http://localhost:8080/api/favorite/user`, {
20 method: 'GET',
21 headers: {
22 'Authorization': `Bearer ${token}}`,
23 'Content-Type': 'application/json'
24 }
25 })
26
27 if(response.ok)
28 {
29 const data = await response.json()
30 const sortedData = data.sort((a, b) => b.id - a.id);
31 setFavoriteRecipes(sortedData);
32 }
33 else
34 {
35 console.error(response.status)
36 }
37 }
38 catch (error) {
39 console.error(error);
40 }
41 }
42
43 fetchFavoriteRecipes()
44 }, []);
45
46 return (
47 <div className={styles.profileSection}>
48 {
49 favoriteRecipes.length === 0 ?
50 (<p>No recipes found.</p>)
51 :
52 favoriteRecipes.map((recipe, index) => (
53 <FavoriteCard
54 key = {index}
55 recipeId = {recipe.id}
56 name = {recipe.strMeal}
57 description = {recipe.strInstructions}
58 category = {recipe.strCategory}
59 origin = {recipe.strArea}
60 imageURL = {recipe.strMealThumb}
61 />
62 ))
63
64 }
65 </div>
66 );
67}
68
69export default FavoriteRecipesSection;
Note: See TracBrowser for help on using the repository browser.