Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(react-query): react-query version up #384

Merged
merged 3 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"eslint.validate": ["typescript", "typescriptreact"],
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true,
"source.fixAll.stylelint": true
"source.fixAll.eslint": "explicit",
"source.fixAll.stylelint": "explicit"
},
"editor.formatOnSave": true
}
2 changes: 1 addition & 1 deletion next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ const nextConfig = {
images: {
domains: [
'source.unsplash.com',
'toks-api-quiz-image-dev-an2.s3.ap-northeast-2.amazonaws.com',
'k.kakaocdn.net',
'image.tokstudy.com',
'asset.tokstudy.com',
],
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"deploy-storybook": "storybook-to-ghpages"
},
"dependencies": {
"@tanstack/react-query": "^4.29.5",
"@tanstack/react-query": "^5.17.9",
"@tanstack/react-query-devtools": "^4.32.6",
"@types/node": "20.4.1",
"@types/react": "18.2.14",
Expand Down
37 changes: 14 additions & 23 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 31 additions & 29 deletions src/app/nickname/hooks/useCreateNicknameForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,38 +58,40 @@ export const useCreateNicknameForm = ({
};
}, []);

const { mutate: nicknameMutation } = useMutation(async () => {
const nickname = getValues('nickname');
// ๋จผ์ € ์ฟ ํ‚ค ์„ธํŒ…์„ ํ•ด์ค˜์•ผ nickname patch ๊ฐ€๋Šฅ
if (accessToken && refreshToken) {
setCookie('accessToken', accessToken);
setCookie('refreshToken', refreshToken);
}
const { mutate: nicknameMutation } = useMutation({
mutationFn: async () => {
const nickname = getValues('nickname');
// ๋จผ์ € ์ฟ ํ‚ค ์„ธํŒ…์„ ํ•ด์ค˜์•ผ nickname patch ๊ฐ€๋Šฅ
if (accessToken && refreshToken) {
setCookie('accessToken', accessToken);
setCookie('refreshToken', refreshToken);
}

try {
const res = await patchNickname(nickname);
if (res !== null) {
clearSavedToast();
saveToastInfo({
showOnNextPage: true,
isShow: true,
direction: 'bottom',
type: 'success',
title: '๋‹‰๋„ค์ž„ ์„ค์ •์„ ์™„๋ฃŒํ•˜์˜€์–ด์š”.',
});
try {
const res = await patchNickname(nickname);
if (res !== null) {
clearSavedToast();
saveToastInfo({
showOnNextPage: true,
isShow: true,
direction: 'bottom',
type: 'success',
title: '๋‹‰๋„ค์ž„ ์„ค์ •์„ ์™„๋ฃŒํ•˜์˜€์–ด์š”.',
});

pathName === '/nickname/update'
? router.replace('/my-page')
: router.replace('/toks-main');
}
} catch (err: unknown) {
if (isToksError(err) && err.errorCode === 'DUPLICATED_NICKNAME') {
setError('nickname', { message: '์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๋‹‰๋„ค์ž„์ž…๋‹ˆ๋‹ค.' });
pathName === '/nickname/update'
? router.replace('/my-page')
: router.replace('/toks-main');
}
} catch (err: unknown) {
if (isToksError(err) && err.errorCode === 'DUPLICATED_NICKNAME') {
setError('nickname', { message: '์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๋‹‰๋„ค์ž„์ž…๋‹ˆ๋‹ค.' });
}
// ์—๋Ÿฌ ๋ฐœ์ƒ์‹œ ํ† ํฐ ์‚ญ์ œ
deleteCookie('accessToken');
deleteCookie('refreshToken');
}
// ์—๋Ÿฌ ๋ฐœ์ƒ์‹œ ํ† ํฐ ์‚ญ์ œ
deleteCookie('accessToken');
deleteCookie('refreshToken');
}
},
});

const isRequiredText = useCallback(() => '๋‹‰๋„ค์ž„์€ 2-6๊ธ€์ž์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.', []);
Expand Down
7 changes: 5 additions & 2 deletions src/app/nickname/update/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ const UpdateNickname = () => {
data: user,
isSuccess,
isLoading,
} = useQuery(QUERY_KEYS.GET_USER_INFO(accessToken), async () => {
return await getUserInfo();
} = useQuery({
queryKey: QUERY_KEYS.GET_USER_INFO(accessToken),
queryFn: async () => {
return await getUserInfo();
},
});

const {
Expand Down
12 changes: 5 additions & 7 deletions src/app/quiz/hooks/useGetCommentListQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@ import { QUERY_KEYS } from '@/app/quiz/constants/queryKeys';
import { getCommentsByQuizId } from '@/app/quiz/remotes/comment';

export const useGetCommentListQuery = (quizId: string) => {
return useQuery(
QUERY_KEYS.GET_COMMENT_LIST(quizId),
async () => {
return useQuery({
queryKey: QUERY_KEYS.GET_COMMENT_LIST(quizId),
queryFn: async () => {
try {
return await getCommentsByQuizId(quizId);
} catch {
throw new Error('ํ€ด์ฆˆ ๋Œ“๊ธ€์„ ๊ฐ€์ ธ์˜ค๋Š”๋ฐ ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.');
}
},
{
select: ({ content }) => content,
}
);
select: ({ content }) => content,
});
};
98 changes: 48 additions & 50 deletions src/app/quiz/hooks/useGetQuizDetailQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,62 @@ import { QUERY_KEYS } from '@/app/quiz/constants/queryKeys';
import { getQuizDetailByQuizId } from '@/app/quiz/remotes/quiz';

export const useGetQuizDetailQuery = (quizId: string) => {
return useQuery(
QUERY_KEYS.GET_QUIZ_DETAIL(quizId),
async () => {
return useQuery({
queryKey: QUERY_KEYS.GET_QUIZ_DETAIL(quizId),
queryFn: async () => {
try {
return await getQuizDetailByQuizId(quizId);
} catch {
throw new Error('ํ€ด์ฆˆ ์ƒ์„ธ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š”๋ฐ ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.');
}
},
{
select: ({
quiz: {
title: quizTitle,
tags,
question: { imageUrl: oxImageUrl, buttons },
quizType,
description: oxDescription,
answer: oxAnswer,
},
category: { name: categoryName },
quizReply,
quizReplyCount: { totalCount, replyCount },
isSubmitted,
}) => {
const buttonLeft = buttons.A ?? buttons.O;
const buttonRight = buttons.B ?? buttons.X;
select: ({
quiz: {
title: quizTitle,
tags,
question: { imageUrl: oxImageUrl, buttons },
quizType,
description: oxDescription,
answer: oxAnswer,
},
category: { name: categoryName },
quizReply,
quizReplyCount: { totalCount, replyCount },
isSubmitted,
}) => {
const buttonLeft = buttons.A ?? buttons.O;
const buttonRight = buttons.B ?? buttons.X;

const answerCount = {
left: replyCount?.A?.count ?? replyCount?.O?.count ?? 0,
right: replyCount?.B?.count ?? replyCount?.X?.count ?? 0,
};
const answerCount = {
left: replyCount?.A?.count ?? replyCount?.O?.count ?? 0,
right: replyCount?.B?.count ?? replyCount?.X?.count ?? 0,
};

const answerPercentage = {
left: Math.floor((answerCount.left / totalCount) * 100),
right: Math.floor((answerCount.right / totalCount) * 100),
};
const answerPercentage = {
left: Math.floor((answerCount.left / totalCount) * 100),
right: Math.floor((answerCount.right / totalCount) * 100),
};

const answerParticipationLabel = {
left: `${answerPercentage.left}% (${answerCount.left}๋ช…)`,
right: `${answerPercentage.right}% (${answerCount.right}๋ช…)`,
};
const answerParticipationLabel = {
left: `${answerPercentage.left}% (${answerCount.left}๋ช…)`,
right: `${answerPercentage.right}% (${answerCount.right}๋ช…)`,
};

return {
quizTitle,
tags,
oxImageUrl,
buttonLeft,
buttonRight,
quizType,
oxDescription,
oxAnswer,
categoryName,
quizReply,
answerPercentage,
answerParticipationLabel,
isSubmitted,
};
},
}
);
return {
quizTitle,
tags,
oxImageUrl,
buttonLeft,
buttonRight,
quizType,
oxDescription,
oxAnswer,
categoryName,
quizReply,
answerPercentage,
answerParticipationLabel,
isSubmitted,
};
},
});
};
Loading
Loading