Skip to content

Commit

Permalink
게시글 좋아요시 낙관적 업데이트 적용 (12.06)
Browse files Browse the repository at this point in the history
 게시글 좋아요시 낙관적 업데이트 적용 (12.06)
  • Loading branch information
seoko97 authored Dec 5, 2023
2 parents d20c29a + 368237c commit 1c6b79e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
9 changes: 5 additions & 4 deletions src/apis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ const getToken = async (tokeName: string) => {
const setForwardedForByServer = async (config: AxiosRequestConfig) => {
if (!isServer || !config.headers) return;

config.headers["x-forwarded-for"] = (await import("next/headers"))
.headers()
.get("x-forwarded-for");
const ips = (await import("next/headers")).headers().get("x-forwarded-for")?.split(", ") ?? [];

config.headers["x-real-ip"] = (await import("next/headers")).headers().get("x-real-ip");
if (ips.length === 0) return;

config.headers["x-forwarded-for"] = ips[0];
config.headers["x-real-ip"] = ips[0];
};

api.interceptors.request.use(async (config) => {
Expand Down
44 changes: 38 additions & 6 deletions src/hooks/query/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,34 @@ const useLikePostMutation = (nid: number) => {
onMutate: () => {
queryClient.cancelQueries(["post", nid]);

const prev = queryClient.getQueryData<IPost>(["post", nid]);
const post = queryClient.getQueryData<IPost>(["post", nid]);
const posts = queryClient.getQueryData<IPost[]>(["posts"]);

queryClient.setQueryData<IPost | undefined>(["post", nid], (prev) => {
if (!prev) return prev;

return { ...prev, isLiked: true, likeCount: prev.likeCount + 1 };
});

return prev;
queryClient.setQueryData<IPost[] | undefined>(["posts"], (prev) => {
if (!prev) return prev;

return prev.map((post) => {
if (post.nid !== nid) return post;

return { ...post, isLiked: true, likeCount: post.likeCount + 1 };
});
});

return { post, posts };
},
onError: (err, _, prev) => {
queryClient.setQueryData<IPost | undefined>(["post", nid], prev);
if (!prev) return;

const { post, posts } = prev;

queryClient.setQueryData<IPost | undefined>(["post", nid], post);
queryClient.setQueryData<IPost[] | undefined>(["posts"], posts);
},
onSettled: () => {
queryClient.invalidateQueries(["post", nid]);
Expand All @@ -128,18 +144,34 @@ const useUnlikePostMutation = (nid: number) => {
onMutate: () => {
queryClient.cancelQueries(["post", nid]);

const prev = queryClient.getQueryData<IPost>(["post", nid]);
const post = queryClient.getQueryData<IPost>(["post", nid]);
const posts = queryClient.getQueryData<IPost[]>(["posts"]);

queryClient.setQueryData<IPost | undefined>(["post", nid], (prev) => {
if (!prev) return prev;

return { ...prev, isLiked: false, likeCount: prev.likeCount - 1 };
});

return prev;
queryClient.setQueryData<IPost[] | undefined>(["posts"], (prev) => {
if (!prev) return prev;

return prev.map((post) => {
if (post.nid !== nid) return post;

return { ...post, isLiked: false, likeCount: post.likeCount - 1 };
});
});

return { post, posts };
},
onError: (err, _, prev) => {
queryClient.setQueryData<IPost | undefined>(["post", nid], prev);
if (!prev) return;

const { post, posts } = prev;

queryClient.setQueryData<IPost | undefined>(["post", nid], post);
queryClient.setQueryData<IPost[] | undefined>(["posts"], posts);
},
onSettled: () => {
queryClient.invalidateQueries(["post", nid]);
Expand Down
6 changes: 3 additions & 3 deletions src/styles/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
body {
overflow: overlay;

@apply m-0 bg-primary text-base transition-[background-color];
@apply m-0 bg-primary text-base;
}

a {
Expand Down Expand Up @@ -119,11 +119,11 @@

::-webkit-scrollbar-track {
box-shadow: none;
background-color: none;
background: none;
}

::-webkit-scrollbar-thumb {
background-color: theme(textColor.secondary);
background: theme(textColor.secondary);

@apply rounded-lg;
}
Expand Down

0 comments on commit 1c6b79e

Please sign in to comment.