mirror of
https://github.com/trafficlunar/tomodachi-share.git
synced 2026-05-13 13:17:45 +00:00
temp disable likes
This commit is contained in:
parent
c72dab1962
commit
b66fbd305a
5 changed files with 90 additions and 91 deletions
|
|
@ -6,54 +6,54 @@ import { idSchema } from "@/lib/schemas";
|
||||||
import { RateLimit } from "@/lib/rate-limit";
|
import { RateLimit } from "@/lib/rate-limit";
|
||||||
|
|
||||||
export async function PATCH(request: NextRequest, { params }: { params: Promise<{ id: string }> }) {
|
export async function PATCH(request: NextRequest, { params }: { params: Promise<{ id: string }> }) {
|
||||||
const session = await auth();
|
// const session = await auth();
|
||||||
if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
// if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||||
|
|
||||||
const rateLimit = new RateLimit(request, 100, "/api/mii/like");
|
// const rateLimit = new RateLimit(request, 100, "/api/mii/like");
|
||||||
const check = await rateLimit.handle();
|
// const check = await rateLimit.handle();
|
||||||
if (check) return check;
|
// if (check) return check;
|
||||||
|
|
||||||
const { id: slugId } = await params;
|
// const { id: slugId } = await params;
|
||||||
const parsed = idSchema.safeParse(slugId);
|
// const parsed = idSchema.safeParse(slugId);
|
||||||
if (!parsed.success) return rateLimit.sendResponse({ error: parsed.error.issues[0].message }, 400);
|
// if (!parsed.success) return rateLimit.sendResponse({ error: parsed.error.issues[0].message }, 400);
|
||||||
const miiId = parsed.data;
|
// const miiId = parsed.data;
|
||||||
|
|
||||||
const result = await prisma.$transaction(async (tx) => {
|
// const result = await prisma.$transaction(async (tx) => {
|
||||||
const existingLike = await tx.like.findUnique({
|
// const existingLike = await tx.like.findUnique({
|
||||||
where: {
|
// where: {
|
||||||
userId_miiId: {
|
// userId_miiId: {
|
||||||
userId: Number(session.user?.id),
|
// userId: Number(session.user?.id),
|
||||||
miiId,
|
// miiId,
|
||||||
},
|
// },
|
||||||
},
|
// },
|
||||||
});
|
// });
|
||||||
|
|
||||||
if (existingLike) {
|
// if (existingLike) {
|
||||||
// Remove the like if it exists
|
// // Remove the like if it exists
|
||||||
await tx.like.delete({
|
// await tx.like.delete({
|
||||||
where: {
|
// where: {
|
||||||
userId_miiId: {
|
// userId_miiId: {
|
||||||
userId: Number(session.user?.id),
|
// userId: Number(session.user?.id),
|
||||||
miiId,
|
// miiId,
|
||||||
},
|
// },
|
||||||
},
|
// },
|
||||||
});
|
// });
|
||||||
} else {
|
// } else {
|
||||||
// Add a like if it doesn't exist
|
// // Add a like if it doesn't exist
|
||||||
await tx.like.create({
|
// await tx.like.create({
|
||||||
data: {
|
// data: {
|
||||||
userId: Number(session.user?.id),
|
// userId: Number(session.user?.id),
|
||||||
miiId,
|
// miiId,
|
||||||
},
|
// },
|
||||||
});
|
// });
|
||||||
}
|
// }
|
||||||
|
|
||||||
const likeCount = await tx.like.count({
|
// const likeCount = await tx.like.count({
|
||||||
where: { miiId },
|
// where: { miiId },
|
||||||
});
|
// });
|
||||||
|
|
||||||
return { liked: !existingLike, count: likeCount };
|
// return { liked: !existingLike, count: likeCount };
|
||||||
});
|
// });
|
||||||
|
|
||||||
return rateLimit.sendResponse({ success: true, liked: result.liked, count: result.count });
|
return rateLimit.sendResponse({ success: false });
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,25 +4,26 @@ import { prisma } from "@/lib/prisma";
|
||||||
import { RateLimit } from "@/lib/rate-limit";
|
import { RateLimit } from "@/lib/rate-limit";
|
||||||
|
|
||||||
export async function GET(request: NextRequest) {
|
export async function GET(request: NextRequest) {
|
||||||
const session = await auth();
|
// const session = await auth();
|
||||||
if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
// if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||||
|
|
||||||
const rateLimit = new RateLimit(request, 50, "/api/mii/like_get");
|
// const rateLimit = new RateLimit(request, 50, "/api/mii/like_get");
|
||||||
const check = await rateLimit.handle();
|
// const check = await rateLimit.handle();
|
||||||
if (check) return check;
|
// if (check) return check;
|
||||||
|
|
||||||
const idsParam = new URL(request.url).searchParams.get("ids");
|
// const idsParam = new URL(request.url).searchParams.get("ids");
|
||||||
if (!idsParam) return NextResponse.json({ error: "Missing IDs parameter" }, { status: 400 });
|
// if (!idsParam) return NextResponse.json({ error: "Missing IDs parameter" }, { status: 400 });
|
||||||
|
|
||||||
const ids = idsParam.split(",").map(Number).filter(Boolean);
|
// const ids = idsParam.split(",").map(Number).filter(Boolean);
|
||||||
if (!ids.length) return NextResponse.json({ error: "No valid IDs provided" }, { status: 400 });
|
// if (!ids.length) return NextResponse.json({ error: "No valid IDs provided" }, { status: 400 });
|
||||||
if (ids.length > 100) return NextResponse.json({ error: "Too many IDs, maximum is 100" }, { status: 400 });
|
// if (ids.length > 100) return NextResponse.json({ error: "Too many IDs, maximum is 100" }, { status: 400 });
|
||||||
|
|
||||||
const liked = await prisma.like.findMany({
|
// const liked = await prisma.like.findMany({
|
||||||
where: { userId: Number(session.user?.id), miiId: { in: ids } },
|
// where: { userId: Number(session.user?.id), miiId: { in: ids } },
|
||||||
select: { miiId: true },
|
// select: { miiId: true },
|
||||||
});
|
// });
|
||||||
|
|
||||||
// Return only Miis that are liked
|
// // Return only Miis that are liked
|
||||||
return NextResponse.json(liked.map((l) => l.miiId));
|
// return NextResponse.json(liked.map((l) => l.miiId));
|
||||||
|
return NextResponse.json({ success: false }, { status: 500 });
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,31 +24,31 @@ export default function LikeButton({ likes, isLiked, miiId, disabled, abbreviate
|
||||||
const [isAnimating, setIsAnimating] = useState(false);
|
const [isAnimating, setIsAnimating] = useState(false);
|
||||||
|
|
||||||
const onClick = async () => {
|
const onClick = async () => {
|
||||||
if (disabled) return;
|
// if (disabled) return;
|
||||||
if (!session.data?.user) {
|
// if (!session.data?.user) {
|
||||||
router.push("/login");
|
// router.push("/login");
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
|
|
||||||
setIsLikedState(!isLikedState);
|
// setIsLikedState(!isLikedState);
|
||||||
setLikesState(isLikedState ? likesState - 1 : likesState + 1);
|
// setLikesState(isLikedState ? likesState - 1 : likesState + 1);
|
||||||
|
|
||||||
// Trigger animation
|
// // Trigger animation
|
||||||
if (!isLikedState) {
|
// if (!isLikedState) {
|
||||||
setIsAnimating(true);
|
// setIsAnimating(true);
|
||||||
setTimeout(() => setIsAnimating(false), 1000); // match animation duration
|
// setTimeout(() => setIsAnimating(false), 1000); // match animation duration
|
||||||
}
|
// }
|
||||||
|
|
||||||
const response = await fetch(`/api/mii/${miiId}/like`, { method: "PATCH" });
|
// const response = await fetch(`/api/mii/${miiId}/like`, { method: "PATCH" });
|
||||||
|
|
||||||
if (response.ok) {
|
// if (response.ok) {
|
||||||
const { liked, count } = await response.json();
|
// const { liked, count } = await response.json();
|
||||||
setIsLikedState(liked);
|
// setIsLikedState(liked);
|
||||||
setLikesState(count);
|
// setLikesState(count);
|
||||||
} else {
|
// } else {
|
||||||
setIsLikedState(isLikedState);
|
// setIsLikedState(isLikedState);
|
||||||
setLikesState(likesState);
|
// setLikesState(likesState);
|
||||||
}
|
// }
|
||||||
};
|
};
|
||||||
|
|
||||||
// Preload like button icons
|
// Preload like button icons
|
||||||
|
|
@ -56,9 +56,9 @@ export default function LikeButton({ likes, isLiked, miiId, disabled, abbreviate
|
||||||
loadIcons(["icon-park-solid:like", "icon-park-outline:like"]);
|
loadIcons(["icon-park-solid:like", "icon-park-outline:like"]);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
// useEffect(() => {
|
||||||
setIsLikedState(isLiked);
|
// setIsLikedState(isLiked);
|
||||||
}, [isLiked]);
|
// }, [isLiked]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<button
|
<button
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,5 @@
|
||||||
import { Prisma } from "@prisma/client";
|
import { Prisma } from "@prisma/client";
|
||||||
|
|
||||||
import crypto from "crypto";
|
|
||||||
|
|
||||||
import { searchSchema } from "@/lib/schemas";
|
import { searchSchema } from "@/lib/schemas";
|
||||||
import { auth } from "@/lib/auth";
|
import { auth } from "@/lib/auth";
|
||||||
import { prisma } from "@/lib/prisma";
|
import { prisma } from "@/lib/prisma";
|
||||||
|
|
|
||||||
|
|
@ -23,12 +23,12 @@ export default function MiiGrid({ miis, userId, parentPage }: Props) {
|
||||||
const session = useSession();
|
const session = useSession();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
const ids = miis.map((m) => m.id).join(",");
|
// const ids = miis.map((m) => m.id).join(",");
|
||||||
const { data } = useSWR<number[]>(session.data?.user && miis.length > 0 ? `/api/mii/has-liked?ids=${ids}` : null, fetcher, {
|
// const { data } = useSWR<number[]>(session.data?.user && miis.length > 0 ? `/api/mii/has-liked?ids=${ids}` : null, fetcher, {
|
||||||
revalidateOnFocus: false,
|
// revalidateOnFocus: false,
|
||||||
revalidateOnReconnect: false,
|
// revalidateOnReconnect: false,
|
||||||
});
|
// });
|
||||||
const likedIds = new Set(data ?? []);
|
const likedIds = new Set([]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="grid grid-cols-4 gap-4 max-lg:grid-cols-3 max-md:grid-cols-2 max-[30rem]:grid-cols-1">
|
<div className="grid grid-cols-4 gap-4 max-lg:grid-cols-3 max-md:grid-cols-2 max-[30rem]:grid-cols-1">
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue