From b05d3cf476db63b6319fad6d180c569438d727df Mon Sep 17 00:00:00 2001 From: NAJEONG KIM <73640737+Najeong-Kim@users.noreply.github.com> Date: Sun, 20 Oct 2024 13:37:30 +0900 Subject: [PATCH] feat: change vote api (#37) * feat: change vote api * fix post error --- .../discovery/api/discovery.queries.ts | 8 ++--- src/entities/discovery/api/get-vote.ts | 4 +-- src/entities/discovery/api/post-vote.ts | 5 ++- src/entities/discovery/api/use-post-vote.ts | 6 ++-- src/entities/discovery/model/index.ts | 2 +- src/entities/discovery/model/model.d.ts | 12 +++---- .../discovery-detail/ui/vote-dialog.tsx | 29 +++++++++-------- src/pages/api/weski/[...path].ts | 14 +++++++-- src/shared/api/base.ts | 17 ++++++---- .../ui/discovery-detail-page.tsx | 31 +++++++++---------- 10 files changed, 67 insertions(+), 61 deletions(-) diff --git a/src/entities/discovery/api/discovery.queries.ts b/src/entities/discovery/api/discovery.queries.ts index 7fc53a8..7036ce2 100644 --- a/src/entities/discovery/api/discovery.queries.ts +++ b/src/entities/discovery/api/discovery.queries.ts @@ -11,10 +11,10 @@ export const discoveryQueries = { queryKey: [...discoveryQueries.listQueryKey()], queryFn: () => getDiscoveries(), }), - voteQueryKey: (key: string) => [...discoveryQueries.all(), 'vote', key], - vote: (key: string) => + voteQueryKey: (resortId: number) => [...discoveryQueries.all(), 'vote', resortId], + vote: (resortId: number) => queryOptions({ - queryKey: discoveryQueries.voteQueryKey(key), - queryFn: () => getVote(key), + queryKey: discoveryQueries.voteQueryKey(resortId), + queryFn: () => getVote(resortId), }), }; diff --git a/src/entities/discovery/api/get-vote.ts b/src/entities/discovery/api/get-vote.ts index 6cf244b..7739292 100644 --- a/src/entities/discovery/api/get-vote.ts +++ b/src/entities/discovery/api/get-vote.ts @@ -1,8 +1,8 @@ import { apiClient } from '@/shared/api/base'; import type { Vote } from '../model'; -export const getVote = async (key: string): Promise => { - const result = await apiClient.get(`/ski/${key}/snowmaking`); +export const getVote = async (resortId: number): Promise => { + const result = await apiClient.get(`/api/snow-maker/${resortId}`); return result; }; diff --git a/src/entities/discovery/api/post-vote.ts b/src/entities/discovery/api/post-vote.ts index 0d13cd4..d059aea 100644 --- a/src/entities/discovery/api/post-vote.ts +++ b/src/entities/discovery/api/post-vote.ts @@ -1,7 +1,6 @@ import { apiClient } from '@/shared/api/base'; -import type { PostVoteRequest } from '../model'; -export const postVote = async (key: string, body: PostVoteRequest) => { - const res = await apiClient.post(`/ski/${key}/snowmaking`, body); +export const postVote = async (resortId: number, {isPositive}: {isPositive: boolean}) => { + const res = await apiClient.post(`/api/snow-maker/${resortId}/vote`, {isPositive}); return res; }; diff --git a/src/entities/discovery/api/use-post-vote.ts b/src/entities/discovery/api/use-post-vote.ts index 4951404..5b19dd1 100644 --- a/src/entities/discovery/api/use-post-vote.ts +++ b/src/entities/discovery/api/use-post-vote.ts @@ -1,14 +1,14 @@ import { useMutation, useQueryClient } from '@tanstack/react-query'; import { discoveryApi } from '..'; -export const usePostVote = (key: string) => { +export const usePostVote = (resortId: number) => { const queryClient = useQueryClient(); return useMutation({ - mutationFn: ({ isLike }: { isLike: boolean }) => discoveryApi.postVote(key, { isLike }), + mutationFn: ({ isPositive }: { isPositive: boolean }) => discoveryApi.postVote(resortId, {isPositive}), async onSettled() { await queryClient.invalidateQueries({ - queryKey: discoveryApi.discoveryQueries.voteQueryKey(key), + queryKey: discoveryApi.discoveryQueries.voteQueryKey(resortId), }); }, }); diff --git a/src/entities/discovery/model/index.ts b/src/entities/discovery/model/index.ts index dad5cc9..de4f0bc 100644 --- a/src/entities/discovery/model/index.ts +++ b/src/entities/discovery/model/index.ts @@ -1,2 +1,2 @@ export { DiscoveryData } from './constants'; -export type { Weather, WeeklyWeather, Discovery, Vote, PostVoteRequest } from './model'; +export type { Weather, WeeklyWeather, Discovery, Vote } from './model'; diff --git a/src/entities/discovery/model/model.d.ts b/src/entities/discovery/model/model.d.ts index 30b2c42..5e5cc6c 100644 --- a/src/entities/discovery/model/model.d.ts +++ b/src/entities/discovery/model/model.d.ts @@ -26,10 +26,8 @@ export type Discovery = { }; export type Vote = { - totalNum: number; - likeNum: number; -}; - -export type PostVoteRequest = { - isLike: boolean; -}; + resortId: number; + totalVotes: number; + positiveVotes: number; + status: string; +}; \ No newline at end of file diff --git a/src/features/discovery-detail/ui/vote-dialog.tsx b/src/features/discovery-detail/ui/vote-dialog.tsx index bf02311..026ed66 100644 --- a/src/features/discovery-detail/ui/vote-dialog.tsx +++ b/src/features/discovery-detail/ui/vote-dialog.tsx @@ -16,7 +16,6 @@ import { DialogTrigger, } from '@/shared/ui/dialog'; import { formatDate } from '../lib/formatDate'; -import { getVoteText } from '../lib/getVoteText'; import { canVote, getVoteData, saveVoteData } from '../lib/vote'; interface VoteDialogProps { @@ -25,10 +24,10 @@ interface VoteDialogProps { } const VoteDialog = ({ id, trigger }: VoteDialogProps) => { - const [isGood, setIsGood] = useState(true); - const { data: voteData } = useQuery(discoveryApi.discoveryQueries.vote(id.toString())); + const [isPositive, setIsPositive] = useState(true); + const { data: voteData } = useQuery(discoveryApi.discoveryQueries.vote(id)); - const { mutateAsync } = usePostVote(id.toString()); + const { mutateAsync } = usePostVote(id); const handleVote = useCallback(async () => { if (!canVote(id.toString())) { @@ -36,7 +35,7 @@ const VoteDialog = ({ id, trigger }: VoteDialogProps) => { return; } try { - await mutateAsync({ isLike: isGood }); + await mutateAsync({ isPositive }); } catch (error) { console.log(error); } finally { @@ -45,7 +44,7 @@ const VoteDialog = ({ id, trigger }: VoteDialogProps) => { saveVoteData(voteData); toast.success('고마워요! 투표의 결과가 반영되었어요'); } - }, [id, isGood, mutateAsync]); + }, [id, isPositive, mutateAsync]); return ( @@ -57,10 +56,10 @@ const VoteDialog = ({ id, trigger }: VoteDialogProps) => {

오늘의 설질

- {getVoteText(voteData?.totalNum, voteData?.likeNum)} + {voteData?.status}

- {voteData?.totalNum}명 중{' '} - {voteData?.likeNum} + {voteData?.totalVotes}명 중{' '} + {voteData?.positiveVotes} 명이 긍정적으로 투표했어요.

@@ -71,22 +70,22 @@ const VoteDialog = ({ id, trigger }: VoteDialogProps) => {