From 13d3367adb0ef540752369effcb440186fbdef2f Mon Sep 17 00:00:00 2001 From: Alejandro Guzman Date: Thu, 14 Dec 2023 22:11:15 +0100 Subject: [PATCH] Some changes requested by po --- src/components/UserFeed.jsx | 78 +++++++++++++++++------ src/components/UserProfile.jsx | 110 +++++++++++++++++---------------- 2 files changed, 115 insertions(+), 73 deletions(-) diff --git a/src/components/UserFeed.jsx b/src/components/UserFeed.jsx index ef8f739..859df4f 100644 --- a/src/components/UserFeed.jsx +++ b/src/components/UserFeed.jsx @@ -13,6 +13,8 @@ function UserFeed() { const [loading, setLoading] = useState(true); const [feedType, setFeedType] = useState(localStorage.getItem('feedType') || 'foryou'); const [showLoginRedirectModal, setShowLoginRedirectModal] = useState(false); + const [myFollowing, setMyFollowing] = useState(0); + const hasFollowings = myFollowing.length > 0; const loggedOutFilters = { sortBy: "average_rating", @@ -29,14 +31,35 @@ function UserFeed() { setPage(0); setFilters(loggedOutFilters); setRecipeName(null); + console.error("hola") getRecipes(loggedOutFilters, null, 0, 9, true, feedType); } + fetchMyUserData() }, [isLogged]); useEffect(() => { getRecipes(filters, recipeName, page, numRecipes, true, feedType); }, [feedType]); + const fetchMyUserData = async () => { + try { + const response = await fetch(process.env.REACT_APP_API_URL + '/user/me', { + method: 'GET', + headers: { + 'Authorization': `Bearer ${token}`, + 'Content-Type': 'application/json' + }, + }); + if (!response.ok) { + throw new Error('Failed to fetch user data'); + } + const data = await response.json(); + setMyFollowing(data.following) + } catch (error) { + console.error('Error fetching user data:', error); + } + }; + const getRecipes = async (filters, recipeName, page, numRecipes, reset, feedType) => { setLoading(true); let url = buildRequestUrl(filters, recipeName, page, numRecipes, feedType); @@ -139,30 +162,47 @@ function UserFeed() { }}/> )} - {loading && recipes.length === 0 ? ( - - - - ) : ( - { - setPage(page + 1); - if (feedType === "foryou") { - getRecipes(filters, recipeName, page+1, numRecipes, false, 'foryou'); - } else { - getRecipes(filters, recipeName, page+1, numRecipes, false, 'following'); - } - }} - finished={finished} - /> + { + feedType === 'foryou' || (feedType === "following" && hasFollowings) ? ( + loading && recipes.length === 0 ? ( + + + + ) : ( + isLogged && ( + { + setPage(page + 1); + if (feedType === "foryou") { + getRecipes(filters, recipeName, page+1, numRecipes, false, 'foryou'); + } else { + getRecipes(filters, recipeName, page+1, numRecipes, false, 'following'); + } + }} + finished={finished} + /> + ) + ) + ) : ( +
+ You still don't follow anyone! +
+ ) + } + + {!isLogged() && ( +
+ This is as far as you can go. Please, login or register to see more recipes. +
)} + setShowLoginRedirectModal(false)}> - + Required log in - You need to log in to view the recipes of the people you follow. + You need to log in to view the recipes of the people you follow. - )} + {(token !== null) && (follower.username !== myUserName) && ( + + )} + @@ -908,10 +909,10 @@ const UserProfile = () => { - + Following - + {userFollowing.length > 0 ? ( followingDetails.map((following, index) => ( @@ -929,17 +930,17 @@ const UserProfile = () => { {following.username} - {following.username !== myUserName && !token==null && ( - - )} + {(token !== null) && (following.username !== myUserName) && ( + + )} @@ -951,29 +952,30 @@ const UserProfile = () => { <>

You're not following anyone. Discover creators that match your taste!

{suggestedUsers.length > 0 ? ( - suggestedUsers.map((user, index) => ( - - handleNavigate(user._id)}> - - - - - - - {user.username} - - - - - + suggestedUsers.slice(0, 5).map((user, index) => ( + + handleNavigate(user._id)}> + + + + + + + {user.username} + + + + + )) ) : (

Loading...

)} + ) : (

You are not following anyone yet.

@@ -983,11 +985,11 @@ const UserProfile = () => {
setShowUnfollowModal(false)}> - + Unfollow User - Do you want to unfollow this user? - + Do you want to unfollow this user? +