From 2ab1f7463108ca8f2dcac299428ccd603fdfbb07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20K=C3=B6rner?= Date: Wed, 21 Aug 2024 14:46:25 +0200 Subject: [PATCH] Update [user] route, add separate UserProfile Component, add function to get users favorite tv series + total by username instead of active session. --- src/app/actions/actions.ts | 1 - src/app/actions/tvSeriesActions.ts | 30 ++++++++- src/app/api/memberApi.ts | 6 +- src/app/dashboard/anime/favorites/page.tsx | 5 +- src/app/dashboard/users/[user]/page.tsx | 21 +++--- src/components/ui/UserProfile.tsx | 77 ++++++++++++++++++++++ 6 files changed, 118 insertions(+), 22 deletions(-) create mode 100644 src/components/ui/UserProfile.tsx diff --git a/src/app/actions/actions.ts b/src/app/actions/actions.ts index fe06b2e..4e7c3f9 100644 --- a/src/app/actions/actions.ts +++ b/src/app/actions/actions.ts @@ -76,7 +76,6 @@ export const createUserToDB = async (userData: { }): Promise => { try { const hashedPassword = await bcrypt.hash(userData.password, 10); - console.log(`Hashed Password: ${hashedPassword}`); const user = await prisma.users.create({ data: { username: userData.username.toLowerCase(), diff --git a/src/app/actions/tvSeriesActions.ts b/src/app/actions/tvSeriesActions.ts index bcac24d..888a200 100644 --- a/src/app/actions/tvSeriesActions.ts +++ b/src/app/actions/tvSeriesActions.ts @@ -42,8 +42,8 @@ export async function createTVSeries(anime: Anime) { } } -// ADD SERIES TO USER FAVORITES -export async function getFavoriteTVSeries() { +// GET FAVORITE TV SERIES BY ACTIVE SESSION +export async function getFavoriteTVSeriesBySession() { const session = await getSession(); if (!session || !session.userId) return { success: false, reason: "not_logged_in" }; @@ -67,3 +67,29 @@ export async function getFavoriteTVSeries() { throw error; // Re-throw the error for further handling in the component } } + +// GET FAVORITE TV SERIES BY ACTIVE SESSION +export async function getFavoriteTVSeriesByUsername(username: string) { + try { + // Fetch the subscribed TV series for a user by their username + const favoriteTVSeries = await prisma.subscribed_TVSeries.findMany({ + where: { + user: { + username: username, // Filter based on the username in the related Users table + }, + }, + }); + + const totalSeries = favoriteTVSeries.length; + + if (totalSeries === 0) { + // If no favorite series found, return a specific object + return { success: false, reason: "No favorites found.", totalSeries: 0 }; + } + + return { success: true, data: favoriteTVSeries, totalSeries }; + } catch (error) { + console.error("Error finding favorite series:", error); + throw error; // Re-throw the error for further handling in the component + } +} diff --git a/src/app/api/memberApi.ts b/src/app/api/memberApi.ts index e8fc7ea..4f7c1e7 100644 --- a/src/app/api/memberApi.ts +++ b/src/app/api/memberApi.ts @@ -22,13 +22,9 @@ export async function getPublicUsers(boolean: boolean): Promise { // GET CURRENT USER export async function getUserByName(username: string): Promise { - const session = await getSession(); - if (!session.userId) return redirect("/login"); - const currentUser = await prisma.users.findFirst({ where: { username: username }, }); - if (!currentUser) return redirect("/dashboard"); - return currentUser; + return currentUser as Users; } diff --git a/src/app/dashboard/anime/favorites/page.tsx b/src/app/dashboard/anime/favorites/page.tsx index 4c8d9cc..80b23e7 100644 --- a/src/app/dashboard/anime/favorites/page.tsx +++ b/src/app/dashboard/anime/favorites/page.tsx @@ -1,11 +1,12 @@ import HeroSection from "@/components/ui/HeroSection"; import React from "react"; import SeriesCards from "./SeriesCards"; -import { getFavoriteTVSeries } from "@/app/actions/tvSeriesActions"; +import { getFavoriteTVSeriesBySession } from "@/app/actions/tvSeriesActions"; import { Subscribed_TVSeries } from "@prisma/client"; const FavoriteSeries = async () => { - const series = (await getFavoriteTVSeries()) as Subscribed_TVSeries[]; + const series = + (await getFavoriteTVSeriesBySession()) as Subscribed_TVSeries[]; return (
-
You are on the profile page of user: {userData.username}
-
+
+ }> + + +
); } diff --git a/src/components/ui/UserProfile.tsx b/src/components/ui/UserProfile.tsx new file mode 100644 index 0000000..cfdb422 --- /dev/null +++ b/src/components/ui/UserProfile.tsx @@ -0,0 +1,77 @@ +import { Button } from "@/components/ui/shad-cn/button"; +import { + Avatar, + AvatarFallback, + AvatarImage, +} from "@/components/ui/shad-cn/avatar"; +import { + Card, + CardHeader, + CardContent, + CardFooter, +} from "@/components/ui/shad-cn/card"; +import { Badge } from "@/components/ui/shad-cn/badge"; +import { + CalendarIcon, + MapPinIcon, + BriefcaseIcon, + MailIcon, +} from "lucide-react"; +import { getUserByName } from "@/app/api/memberApi"; +import { getFavoriteTVSeriesByUsername } from "@/app/actions/tvSeriesActions"; + +interface UserProfileProps { + username: string; +} + +export default async function UserProfile({ username }: UserProfileProps) { + const userData = await getUserByName(username); + const usernameShort = userData?.username.slice(0, 2); + const FavoriteSeries = await getFavoriteTVSeriesByUsername(username); + + return ( + <> + {!userData ? ( +
No user found.
+ ) : ( + + + + + {usernameShort} + +
+

+

{userData?.username}

+
+ +
+ +
+ + Works at TechCorp +
+
+ + San Francisco, CA +
+
+ + Joined January 2020 +
+
+ + jane.doe@example.com +
+
+
+ )} + + ); +}