mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-10-01 15:48:23 -05:00
Calendar get likes via SWR
This commit is contained in:
@@ -22,6 +22,7 @@ import { getToastOptions } from "utils/objects";
|
||||
import { trpc } from "utils/trpc";
|
||||
import { Unpacked } from "utils/types";
|
||||
import { FreeAgentsGet } from "pages/api/free-agents";
|
||||
import { mutate } from "swr";
|
||||
|
||||
const playstyleToEmoji = {
|
||||
FRONTLINE: RiSwordLine,
|
||||
@@ -45,10 +46,9 @@ const FreeAgentSection = ({
|
||||
postRef?: RefObject<HTMLDivElement>;
|
||||
}) => {
|
||||
const toast = useToast();
|
||||
const utils = trpc.useQueryUtils();
|
||||
const addLikeMutation = trpc.useMutation("freeAgents.addLike", {
|
||||
onSuccess() {
|
||||
utils.invalidateQuery(["freeAgents.likes"]);
|
||||
mutate("/api/free-agents/likes");
|
||||
},
|
||||
onError(error) {
|
||||
toast(getToastOptions(error.message, "error"));
|
||||
@@ -56,7 +56,7 @@ const FreeAgentSection = ({
|
||||
});
|
||||
const deleteLikeMutation = trpc.useMutation("freeAgents.deleteLike", {
|
||||
onSuccess() {
|
||||
utils.invalidateQuery(["freeAgents.likes"]);
|
||||
mutate("/api/free-agents/likes");
|
||||
},
|
||||
onError(error) {
|
||||
toast(getToastOptions(error.message, "error"));
|
||||
|
||||
@@ -2,10 +2,10 @@ import { Playstyle } from "@prisma/client";
|
||||
import { countries } from "countries-list";
|
||||
import { useUser } from "hooks/common";
|
||||
import { FreeAgentsGet } from "pages/api/free-agents";
|
||||
import { LikesGet } from "pages/api/free-agents/likes";
|
||||
import { Dispatch, useMemo, useReducer } from "react";
|
||||
import useSWR from "swr";
|
||||
import { getBooleanFromString, getWeaponFromString } from "utils/strings";
|
||||
import { trpc } from "utils/trpc";
|
||||
import { isFreeAgentPlaystyle, isFreeAgentRegion } from "utils/typeGuards";
|
||||
import { useMyRouter } from "./useMyRouter";
|
||||
|
||||
@@ -70,9 +70,7 @@ export function useFreeAgents(postsInitialData: FreeAgentsGet) {
|
||||
(post) => post.user.discordId === user?.discordId
|
||||
);
|
||||
|
||||
const { data: likesData } = trpc.useQuery(["freeAgents.likes"], {
|
||||
enabled: !!usersPost,
|
||||
});
|
||||
const likes = useSWR<LikesGet>(usersPost ? "/api/free-agents/likes" : null);
|
||||
|
||||
const [state, dispatch] = useReducer(
|
||||
(oldState: UseFreeAgentsState, action: Action) => {
|
||||
@@ -177,10 +175,10 @@ export function useFreeAgents(postsInitialData: FreeAgentsGet) {
|
||||
postsData: filteredPostsData,
|
||||
refetchPosts: posts.mutate,
|
||||
allPostsCount: (posts.data ?? []).length,
|
||||
likesData,
|
||||
likesData: likes.data,
|
||||
isLoading: !posts.data,
|
||||
usersPost,
|
||||
matchedPosts: (likesData?.matchedPostIds ?? []).map((id) =>
|
||||
matchedPosts: (likes.data?.matchedPostIds ?? []).map((id) =>
|
||||
(filteredPostsData ?? []).find((post) => post.id === id)
|
||||
),
|
||||
state,
|
||||
|
||||
21
pages/api/free-agents/likes.ts
Normal file
21
pages/api/free-agents/likes.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { NextApiRequest, NextApiResponse } from "next";
|
||||
import { createHandler, getMySession } from "utils/api";
|
||||
import freeAgentsService, { LikesData } from "services/freeagents";
|
||||
import { Serialized } from "utils/types";
|
||||
|
||||
export type LikesGet = Serialized<LikesData>;
|
||||
|
||||
const GET = async (req: NextApiRequest, res: NextApiResponse<LikesData>) => {
|
||||
const user = await getMySession(req);
|
||||
if (!user) {
|
||||
return res.status(401).end();
|
||||
}
|
||||
|
||||
const likes = await freeAgentsService.likes({ user });
|
||||
res.status(200).json(likes);
|
||||
};
|
||||
|
||||
const likesHandler = (req: NextApiRequest, res: NextApiResponse) =>
|
||||
createHandler(req, res, { GET });
|
||||
|
||||
export default likesHandler;
|
||||
@@ -5,12 +5,6 @@ import { freeAgentPostSchema } from "utils/validators/fapost";
|
||||
import * as z from "zod";
|
||||
|
||||
const freeAgentsApi = createRouter()
|
||||
.query("likes", {
|
||||
resolve({ ctx }) {
|
||||
const user = throwIfNotLoggedIn(ctx.user);
|
||||
return service.likes(user.id);
|
||||
},
|
||||
})
|
||||
.mutation("upsertPost", {
|
||||
input: freeAgentPostSchema,
|
||||
resolve({ ctx, input }) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { Prisma, User } from "@prisma/client";
|
||||
import { httpError } from "@trpc/server";
|
||||
import prisma from "prisma/client";
|
||||
import { freeAgentPostSchema } from "utils/validators/fapost";
|
||||
@@ -78,9 +78,11 @@ const deletePost = async (userId: number) => {
|
||||
return prisma.freeAgentPost.delete({ where: { userId } });
|
||||
};
|
||||
|
||||
const likes = async (userId: number) => {
|
||||
export type LikesData = Prisma.PromiseReturnType<typeof likes>;
|
||||
|
||||
const likes = async ({ user }: { user: User }) => {
|
||||
const post = await prisma.freeAgentPost.findUnique({
|
||||
where: { userId },
|
||||
where: { userId: user.id },
|
||||
include: {
|
||||
likedPosts: { select: { id: true } },
|
||||
likersPosts: { select: { id: true, updatedAt: true } },
|
||||
|
||||
Reference in New Issue
Block a user