Skip to content

Commit

Permalink
🚨 Fix(#252): λ’€λ‘œ κ°€κΈ° μ‹œ μ’‹μ•„μš” 반영 μ•ˆλ˜λŠ” ν˜„μƒ 낙관적 μ—…λ°μ΄νŠΈλ‘œ μˆ˜μ •
Browse files Browse the repository at this point in the history
  • Loading branch information
JIY00N2 committed Nov 24, 2023
1 parent 3ba123e commit 4156d69
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
13 changes: 4 additions & 9 deletions src/app/(steady)/steady/detail/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { format } from "date-fns";
import deleteApplication from "@/services/application/deleteApplication";
import getSteadyDetails from "@/services/steady/getSteadyDetails";
import getSteadyParticipants from "@/services/steady/getSteadyParticipants";
import likeSteady from "@/services/steady/likeSteady";
import promoteSteady from "@/services/steady/promoteSteady";
import type { SteadyDetailsType } from "@/services/types";
import Button, { buttonSize } from "@/components/_common/Button";
Expand All @@ -25,6 +24,7 @@ import LoginModal from "@/components/_common/Modal/LoginModal";
import UserItems from "@/components/_common/Modal/UserModal/UserItems";
import Spinner from "@/components/_common/Spinner";
import Tag from "@/components/_common/Tag";
import { useLikeSteadyMutation } from "@/hooks/mutation/useLikeSteadyMutation";
import { steadyCategoriesWithEmoji } from "@/constants/labelData";
import {
getSteadyDetailsKey,
Expand All @@ -50,12 +50,12 @@ const SteadyDetailPage = ({ params }: { params: { id: string } }) => {
queryKey: getSteadyParticipantsKey(steadyId),
queryFn: () => getSteadyParticipants(steadyId),
});
const { mutate } = useLikeSteadyMutation();

const router = useRouter();
const { toast } = useToast();
const { isAuth } = useAuthStore();
const [isClient, setIsClient] = useState(false);
const [isLiked, setIsLiked] = useState(steadyDetailsData.isLiked);

useEffect(() => {
setIsClient(true);
Expand Down Expand Up @@ -110,11 +110,6 @@ const SteadyDetailPage = ({ params }: { params: { id: string } }) => {
return match ? match.label : null;
};

const handleClickLike = async () => {
await likeSteady(steadyId);
setIsLiked((prev) => !prev);
};

return (
<div className="w-1000">
<div className="flex flex-col gap-20">
Expand All @@ -130,8 +125,8 @@ const SteadyDetailPage = ({ params }: { params: { id: string } }) => {
<Tag status={steadyDetailsData.status} />
<div className="text-35 font-bold">{steadyDetailsData.title}</div>
</div>
<button onClick={handleClickLike}>
{isLiked ? (
<button onClick={() => mutate(steadyId)}>
{steadyDetailsData.isLiked ? (
<Icon
name="heart"
size={30}
Expand Down
32 changes: 32 additions & 0 deletions src/hooks/mutation/useLikeSteadyMutation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import likeSteady from "@/services/steady/likeSteady";
import type { SteadyDetailsType } from "@/services/types";
import { getSteadyDetailsKey } from "@/constants/queryKeys";

export const useLikeSteadyMutation = () => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: likeSteady,
// mutateλ₯Ό ν˜ΈμΆœν•  λ•Œ λ„˜κ²¨μ£ΌλŠ” steadyIdλ₯Ό λ°›μ•„μ˜΄
// onMutate -> onSuccess -> onError
onMutate: async (steadyId) => {
await queryClient.cancelQueries({
queryKey: getSteadyDetailsKey(steadyId),
});
const previousData = queryClient.getQueryData<SteadyDetailsType>(
getSteadyDetailsKey(steadyId),
);
queryClient.setQueryData<SteadyDetailsType>(
getSteadyDetailsKey(steadyId),
(old) => old && { ...old, isLiked: !old.isLiked },
);
return previousData;
},
onError: (error, steadyId, context) => {
queryClient.setQueryData<SteadyDetailsType>(
getSteadyDetailsKey(steadyId),
context,
);
},
});
};

0 comments on commit 4156d69

Please sign in to comment.