1 | import styles from '../../css/ProfileCss/profile-view-style.module.css';
|
---|
2 | import FavoriteCard from "./FavoriteCard";
|
---|
3 | import {useEffect, useState} from "react";
|
---|
4 |
|
---|
5 | const 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 |
|
---|
69 | export default FavoriteRecipesSection; |
---|