import { use, useEffect, useState } from "react"; import type { User } from "../types/user"; import type { Recipe } from "../types/recipe"; import RecipeListItem from "../components/results/RecipeListItem"; import type { Engagement } from "../types/engagement"; import ActivityListItem from "../components/results/ActivityListItem"; import { AuthContext } from "../context/AuthContext"; import { GetAuthenticatedUser, GetAuthenticatedUserEngagement, GetAuthenticatedUserFavorites, GetAuthenticatedUserRecipes } from "../services/UserService"; import { isApiError, type ApiError } from "../types/api/error"; import { Logout } from "../services/AuthService"; import { useNavigate } from "react-router-dom"; // TODO: Need to cache the image, it returns nothing because it fails with 429 (rate limits) // Though this might just be a development issue, it seems to work in production. export default function Profile() { // Context const { getJwt } = use(AuthContext); const navigate = useNavigate(); // Page state const [error, setError] = useState(""); const [user, setUser] = useState(null); const [dummyProfileSrc, setDummyProfileSrc] = useState(""); const [recipes, setRecipes] = useState([]); const [favorites, setFavorites] = useState([]); const [activity, setActivity] = useState([]); const [jwt, setJwt] = useState(""); // Log the user out and direct to the home page const logoutHandler = (): void => { void Logout(); void navigate("/v2/web/home"); } const seeAllRecipesHandler = (): void => void navigate("/v2/web/404"); const seeAllFavoritesHandler = (): void => void navigate("/v2/web/favorites"); const seeAllEngagementHandler = (): void => void navigate("/v2/web/404"); const fetchProfileData = async (): Promise => { const result_user: User | ApiError = await GetAuthenticatedUser(); if (isApiError(result_user)) { setError(result_user.message); } else { setUser(result_user); } const result_recipes: Recipe[] | ApiError = await GetAuthenticatedUserRecipes(); if (isApiError(result_recipes)) { setError(result_recipes.message); } else { setRecipes(result_recipes); } const result_favorites: Recipe[] | ApiError = await GetAuthenticatedUserFavorites(); if (isApiError(result_favorites)) { setError(result_favorites.message); } else { setFavorites(result_favorites); } const result_engagement: Engagement[] | ApiError = await GetAuthenticatedUserEngagement(); if (isApiError(result_engagement)) { setError(result_engagement.message); } else { setActivity(result_engagement); } } // Get the JWT from the cookies useEffect(() => { setJwt(getJwt()); }, [getJwt]); // Get the user when the JWTS change useEffect(() => { // No jwt, we can't get user data if (jwt) void fetchProfileData(); }, [jwt]); useEffect(() => { if (error) console.log("@error", error); }, [error]); useEffect(() => { if (user) setDummyProfileSrc(`https://ui-avatars.com/api/?name=${user?.Name.split(" ")[0]}+${user?.Name.split(" ")[1]}&size=150`); }, [user]); return ( <> {/* User Details Section */}
{user?.ImageUrl != "" ? ( { e.currentTarget.onerror = null; // prevent infinite loop e.currentTarget.src = dummyProfileSrc; }} /> ) : ( )}

{user?.Name}

{user?.Email}

{recipes.length} recipes

{favorites.length} favorites

{/* Recipe Section */}

My Recipes

    {recipes.length <= 4 ? ( recipes.map(recipe => ) ) : ( recipes.slice(0, 4).map(recipe => ) )}
{/* Favorites Section */}

My Favorites

    {favorites.length <= 4 ? ( favorites.map(recipe => ) ) : ( favorites.slice(0, 4).map(recipe => ) )}
{/* Activity Section */}

Recent Activity

    {activity?.map(act => )}
{/* Logout Section TODO: Click event*/}
); }