Skip to content

Commit

Permalink
feat: add likes count
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdibha committed Jan 11, 2024
1 parent 112c243 commit 2b72c4e
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 15 deletions.
4 changes: 2 additions & 2 deletions apps/web/src/app/(app)/(dashboard)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ export default function DashboardLayout(props: DashboardLayoutProps) {
disabled: false,
},
{
href: "/saved",
href: "/liked",
label: "Likes",
disabled: true,
disabled: false,
},
{
href: "/account",
Expand Down
13 changes: 10 additions & 3 deletions apps/web/src/app/(app)/(dashboard)/liked/page.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
export default function Page() {
import { ThemesList } from "@/modules/themes/components/themes-list";
import { getUserLikedThemes, getUserLikes } from "@/modules/themes/services";

export default async function Page() {
const userLikedThemes = await getUserLikedThemes();
const userLikes = await getUserLikes();

return (
<div>
<h2 className="pl-2 text-2xl font-bold">Account</h2>
<h2 className="pl-2 text-2xl font-bold">Liked themes</h2>
<p className="text-muted-foreground pl-2">
Manage your account settings and preferences
Here you can find all the themes you liked
</p>
<ThemesList themes={userLikedThemes} userLikes={userLikes} />
</div>
);
}
10 changes: 7 additions & 3 deletions apps/web/src/app/(app)/(dashboard)/my-themes/page.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import { ThemeCard } from "@/modules/themes/components/theme-card";
import { ThemesList } from "@/modules/themes/components/themes-list";
import { getUserThemes } from "@/modules/themes/services";
import { getUserLikes, getUserThemes } from "@/modules/themes/services";
import { Features } from "@/modules/themes/types";

export default async function Page() {
const userThemes = await getUserThemes();
const userLikes = await getUserLikes();

return (
<div>
<h2 className="pl-2 text-2xl font-bold">My themes</h2>
<p className="text-muted-foreground pl-2">
Here you can find all the themes you created
</p>
<ThemesList themes={userThemes} features={[Features.Delete]} />
<ThemesList
themes={userThemes}
features={[Features.Delete]}
userLikes={userLikes}
/>
</div>
);
}
5 changes: 3 additions & 2 deletions apps/web/src/app/(app)/themes/page.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { ThemesList } from "@/modules/themes/components/themes-list";
import { getTrendingThemes } from "@/modules/themes/services";
import { getTrendingThemes, getUserLikes } from "@/modules/themes/services";

export default async function ThemesPage() {
const trendingThemes = await getTrendingThemes();
const userLikes = await getUserLikes();

return (
<div className="container pt-20">
<h1 className="font-display text-center text-5xl font-medium">Trending themes</h1>
<p className="text-muted-foreground mt-4 text-center">
Get inspired by thousands of themes and easily export for your popular ui library.
</p>
<ThemesList themes={trendingThemes} className="mt-8" />
<ThemesList themes={trendingThemes} userLikes={userLikes} className="mt-8" />
</div>
);
}
Expand Down
7 changes: 5 additions & 2 deletions apps/web/src/modules/themes/components/theme-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import React, { useTransition } from "react";
import Image from "next/image";
import Link from "next/link";
import { useRouter } from "next/navigation";
import Color from "color";
import { useSession } from "next-auth/react";
import {
Expand Down Expand Up @@ -48,8 +49,8 @@ interface ThemeCardProps {
themeId: string;
palette: Palette;
view?: "website" | "placeholder" | "palette";
isLiked?: boolean;
likesCount?: number;
isLiked: boolean;
likesCount: number;
features?: Features[];
}

Expand All @@ -65,6 +66,7 @@ export const ThemeCard = (props: ThemeCardProps) => {

const { background, primary, secondary, card } = palette;

const router = useRouter();
const [pending, startTransition] = useTransition();
const { toast } = useToast();
const [liked, setLiked] = React.useState(isLiked);
Expand All @@ -85,6 +87,7 @@ export const ThemeCard = (props: ThemeCardProps) => {
if (result?.error) {
setLiked((prev) => !prev);
}
router.refresh();
}
});
};
Expand Down
5 changes: 4 additions & 1 deletion apps/web/src/modules/themes/components/themes-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import { ThemeCard } from "./theme-card";

interface ThemesListProps {
themes: any[];
userLikes: string[];
className?: string;
features?: Features[];
}

export const ThemesList = (props: ThemesListProps) => {
const { themes, features, className } = props;
const { themes, userLikes, features, className } = props;
const [view, setView] = React.useState<"website" | "placeholder" | "palette">(
"placeholder"
);
Expand Down Expand Up @@ -75,6 +76,8 @@ export const ThemesList = (props: ThemesListProps) => {
border: palette.border,
}}
features={features}
isLiked={userLikes.includes(theme.id)}
likesCount={theme._count.likedBy}
/>
);
})}
Expand Down
45 changes: 43 additions & 2 deletions apps/web/src/modules/themes/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ export const getTrendingThemes = async () => {
border: true,
},
},
_count: {
select: {
likedBy: true,
},
},
},
orderBy: {
likedBy: {
_count: "desc",
},
},
});
return themes;
Expand Down Expand Up @@ -74,6 +84,11 @@ export const getUserThemes = async () => {
border: true,
},
},
_count: {
select: {
likedBy: true,
},
},
},
});
return themes;
Expand All @@ -85,8 +100,10 @@ export const getUserLikedThemes = async () => {

const themes = await prisma.theme.findMany({
where: {
user: {
id: session.user.id,
likedBy: {
some: {
id: session.user.id,
},
},
},
select: {
Expand All @@ -108,11 +125,35 @@ export const getUserLikedThemes = async () => {
border: true,
},
},
_count: {
select: {
likedBy: true,
},
},
},
});
return themes;
};

export const getUserLikes = async () => {
const session = await getSession();
if (!session) return [];
const userWithLikes = await prisma.user.findUnique({
where: {
id: session.user.id,
},
select: {
likes: {
select: {
id: true,
},
},
},
});
const likes = userWithLikes ? userWithLikes.likes.map((like) => like.id) : [];
return likes;
};

export const createTheme = async (data: any) => {
const session = await getSession();

Expand Down

0 comments on commit 2b72c4e

Please sign in to comment.