Calendar get likes via SWR

This commit is contained in:
Kalle (Sendou)
2021-08-09 00:35:15 +03:00
parent 4d55f231a9
commit 2bdaa405ef
5 changed files with 33 additions and 18 deletions

View File

@@ -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"));

View File

@@ -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,

View 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;

View File

@@ -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 }) {

View File

@@ -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 } },