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

[공통] React Query v3 -> v5 로 버전 업 #120

Merged
merged 5 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"search.exclude": {
"**/.node_modules": true,
"**/package-lock.json": true,
"**/yarn.lock": true,
},
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

갓 원빈의 EOL..

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Make an issue or Contact with this repository leader.
- Language
- [TypeScript](https://github.com/typescript-cheatsheets/react)
- Server State Management Library
- [React-Query](https://react-query.tanstack.com/overview)
- [@tanstack/react-query](https://@tanstack/react-query.tanstack.com/overview)
ChoiWonBeen marked this conversation as resolved.
Show resolved Hide resolved
- Routing Library
- [React Router v6](https://reactrouter.com/docs/en/v6/getting-started/tutorial)
- Linting Library
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@tanstack/react-query": "^5.8.1",
"@testing-library/jest-dom": "^5.14.1",
"@testing-library/react": "^13.0.0",
"@testing-library/user-event": "^13.2.1",
Expand All @@ -21,7 +22,6 @@
"react-dom": "^18.2.0",
"react-hook-form": "^7.36.1",
"react-naver-maps": "^0.1.2",
"react-query": "^3.39.1",
"react-router-dom": "^6.3.0",
"react-scripts": "5.0.1",
"react-toastify": "^9.0.8",
Expand Down
6 changes: 3 additions & 3 deletions src/api/follow/entity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EmailUser } from 'api/user/entity';
import { User } from 'api/user/entity';
import { FollowerInfo } from 'pages/Follow/static/entity';

export interface FollowListParams {
Expand Down Expand Up @@ -27,8 +27,8 @@ export interface SentOrReceivedFollowParams {
export interface SentOrReceivedFollowResponse {
content: {
id: number;
follower: EmailUser;
user: EmailUser;
follower: User;
user: User;
}[];
empty: boolean;
last: boolean;
Expand Down
2 changes: 1 addition & 1 deletion src/api/follow/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ export const recentlyActiveFollow = () => followApi.get<GetFollowListResponse>('

export const getFollowReview = (id: number, pageParam: string) => followApi.get<GetFollowReviewResponse>(`/review/follower/${id}/shops?size=9&${pageParam}`);

export const getDetailReview = (followId: number, placeId: string, pageParam: number) => followApi.get<GetDetailReviewResponse>(`/review/follower/${followId}/shop/${placeId}?${pageParam}`);
export const getDetailReview = (followId: number, placeId: string) => followApi.get<GetDetailReviewResponse>(`/review/follower/${followId}/shop/${placeId}`);

export const getFollowerReviewCount = (param: GetFollowerReviewCountParam) => followApi.get(`/review/follower/${param.followId}/count`);
8 changes: 1 addition & 7 deletions src/api/user/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface ModifyParams {
email?: string;
}

export interface EmailUser {
export interface User {
account: string;
id: number;
nickname: string;
Expand All @@ -46,12 +46,6 @@ export interface EmailUser {
};
}

export interface SNSUser extends Omit<EmailUser, 'account' | 'profileImage'> {
profileImage?: EmailUser['profileImage'];
}

export type User = EmailUser | SNSUser;

export interface SendRegisterEmailParams {
email: string;
}
Expand Down
3 changes: 1 addition & 2 deletions src/api/user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
LoginResponse,
ModifyParams,
RegisterParams,
EmailUser,
SendRegisterEmailParams,
SendFindEmailParams,
GetAccountParams,
Expand All @@ -15,7 +14,7 @@ import {
} from './entity';
import userApi from './userApiClient';

export const register = (param: RegisterParams) => userApi.post<EmailUser>('/', param);
export const register = (param: RegisterParams) => userApi.post<User>('/', param);

export const checkIdDuplicate = (param: CheckIdDuplicateParams) => userApi.get<User>(`/exists?account=${param.account}`);

Expand Down
41 changes: 41 additions & 0 deletions src/components/common/LoadingSpinner/LoadingSpinner.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
.loading-wrapper {
display: flex;
position: relative;
justify-content: center;
align-items: center;
width: 100%;
margin: 32px 0;
}

.loading-div {
box-sizing: border-box;
display: block;
position: absolute;
margin: 8px;
border: 8px solid #175c8e;
border-radius: 50%;
animation: loading-wrapper 1.2s cubic-bezier(0.5, 0, 0.5, 1) infinite;
border-color: #175c8e transparent transparent;
}

.loading-wrapper div:nth-child(1) {
animation-delay: -0.45s;
}

.loading-wrapper div:nth-child(2) {
animation-delay: -0.3s;
}

.loading-wrapper div:nth-child(3) {
animation-delay: -0.15s;
}

@keyframes loading-wrapper {
0% {
transform: rotate(0deg);
}

100% {
transform: rotate(360deg);
}
}
19 changes: 19 additions & 0 deletions src/components/common/LoadingSpinner/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import styles from './LoadingSpinner.module.scss';

interface LoadingSpinnerProps {
size: number;
}

function LoadingSpinner(props: LoadingSpinnerProps) {
const { size } = props;
return (
<div className={styles['loading-wrapper']} style={{ height: size }}>
<div className={styles['loading-div']} style={{ width: size, height: size }} />
<div className={styles['loading-div']} style={{ width: size, height: size }} />
<div className={styles['loading-div']} style={{ width: size, height: size }} />
<div className={styles['loading-div']} style={{ width: size, height: size }} />
</div>
);
}

export default LoadingSpinner;
4 changes: 2 additions & 2 deletions src/components/common/SideNavigation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default function SideNavigation(): JSX.Element {
},
{
name: '마이페이지',
icon: <SpriteSvg id="my-page" height="24" width="18" />,
icon: <SpriteSvg id="my-page" height="24" width="24" />,
link: auth ? '/profile' : '/login',
},
{
Expand Down Expand Up @@ -91,7 +91,7 @@ export default function SideNavigation(): JSX.Element {
{auth ? (
<li>
<img
// src={auth.profileImage?.url || `${defaultImage}`}
src={auth?.profileImage?.url}
alt="프로필 이미지"
className={styles['bottom-navigation__profile-image']}
/>
Expand Down
2 changes: 1 addition & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.scss';
import { BrowserRouter } from 'react-router-dom';
import { QueryClient, QueryClientProvider } from 'react-query';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { Provider as JotaiProvider } from 'jotai';
import App from './App';
import reportWebVitals from './reportWebVitals';
Expand Down
4 changes: 2 additions & 2 deletions src/pages/Auth/Signup/SignupPage/components/IdInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useEffect, useState } from 'react';
import cn from 'utils/ts/classNames';
import { ReactComponent as ErrorIcon } from 'assets/svg/auth/error.svg';
import { useFormContext } from 'react-hook-form';
import { useQuery } from 'react-query';
import { useQuery } from '@tanstack/react-query';
import { checkIdDuplicate } from 'api/user';
import { ERROR_MESSAGE } from 'pages/Auth/Signup/static/signUp';
import { AxiosError } from 'axios';
Expand Down Expand Up @@ -78,7 +78,7 @@ export default function IdInput() {
// 서버에서 타입을 같이 전달하고 있어 해당 부분 임시 처리
checkServerValidation: () => (error?.response?.data.errorMessage?.slice(-23)),
checkError: () => ((status !== 'error') || '이미 사용중인 아이디입니다.'),
checkLoading: () => ((status !== 'loading') || '중복 확인중입니다.'),
checkLoading: () => ((status !== 'pending') || '중복 확인중입니다.'),
},
})}
/>
Expand Down
7 changes: 4 additions & 3 deletions src/pages/Follow/components/FollowReview.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import useMediaQuery from 'utils/hooks/useMediaQuery';
import { useInfiniteQuery } from 'react-query';
import { useInfiniteQuery } from '@tanstack/react-query';
import { getDetailReview } from 'api/follow';
import useBooleanState from 'utils/hooks/useBooleanState';
import { ReactComponent as Arrow } from 'assets/svg/common/arrow.svg';
Expand All @@ -17,9 +17,10 @@ interface Props {

const useGetDetailReview = (placeId: string, followerId: number) => {
const { data } = useInfiniteQuery(
['detail', placeId],
({ pageParam = '' }) => getDetailReview(followerId, placeId, pageParam),
{
queryKey: ['detail', placeId],
initialPageParam: '',
queryFn: () => getDetailReview(followerId, placeId),
getNextPageParam: (last) => {
const len = last.data.content.length;
if (last.data.empty || last.data.last) return null;
Expand Down
11 changes: 7 additions & 4 deletions src/pages/Follow/hooks/useAcceptFollow.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { acceptFollow } from 'api/follow';
import { useMutation, useQueryClient } from 'react-query';
import { useMutation, useQueryClient } from '@tanstack/react-query';

// 팔로우 승인 후 받은 요청 목록을 다시 받아와 갱신
const useAcceptFollow = () => {
const queryClient = useQueryClient();
const { mutate: accept } = useMutation('accept', (id: number) => acceptFollow({ id }), {
const { mutate: accept } = useMutation({
mutationKey: ['accept'],
mutationFn: (id: number) => acceptFollow({ id }),
onSuccess: () => {
queryClient.invalidateQueries('received');
queryClient.invalidateQueries('follower');
queryClient.invalidateQueries({
queryKey: ['received', 'follower'],
});
},
});
return accept;
Expand Down
12 changes: 8 additions & 4 deletions src/pages/Follow/hooks/useCancelFollow.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { cancelFollow } from 'api/follow';
import { useMutation, useQueryClient } from 'react-query';
import { useMutation, useQueryClient } from '@tanstack/react-query';

const useCancelFollow = () => {
const queryClient = useQueryClient();
const { mutate: cancel } = useMutation('cancel', (id: number) => cancelFollow({ id }), {

const { mutate: cancel } = useMutation({
mutationKey: ['cancel'],
mutationFn: (id: number) => cancelFollow({ id }),
onSuccess: () => {
queryClient.invalidateQueries('sended');
queryClient.invalidateQueries('search');
queryClient.invalidateQueries({
queryKey: ['sended', 'search'],
});
},
});

Expand Down
9 changes: 6 additions & 3 deletions src/pages/Follow/hooks/useDeleteFollow.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { deleteFollow } from 'api/follow';
import { useMutation, useQueryClient } from 'react-query';
import { useMutation, useQueryClient } from '@tanstack/react-query';

const useDeleteFollow = () => {
const queryClient = useQueryClient();
const { mutate: del, data } = useMutation('delete', (account: string) => deleteFollow({ userAccount: account }), {

const { mutate: del, data } = useMutation({
mutationKey: ['delete'],
mutationFn: (account: string) => deleteFollow({ userAccount: account }),
onSuccess: () => {
queryClient.invalidateQueries('follower');
queryClient.invalidateQueries({ queryKey: ['follower'] });
},
});

Expand Down
22 changes: 11 additions & 11 deletions src/pages/Follow/hooks/useGetFollowList.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { followList } from 'api/follow';
import { GetFollowListResponse } from 'api/follow/entity';
import { useEffect } from 'react';
import { useInfiniteQuery } from 'react-query';
import { useInfiniteQuery } from '@tanstack/react-query';

// 친구 목록 가져오기
const useGetFollowList = () => {
const { data, hasNextPage, fetchNextPage } = useInfiniteQuery(
'follower',
({ pageParam = '' }) => followList(pageParam),
{
getNextPageParam: (last) => {
const len = last.data.content.length;
if (last.data.empty || last.data.last) return null;
return `cursor=${last.data.content[len - 1].id}`;
},
const { data, hasNextPage, fetchNextPage } = useInfiniteQuery({
queryKey: ['follower'],
queryFn: ({ pageParam = '' }) => followList(pageParam),
initialPageParam: 'cursor=0',
getNextPageParam: (last) => {
const len = last.data.content.length;
if (last.data.empty || last.data.last) return null;
return `cursor=${last.data.content[len - 1].id}`;
},
);

});
const flatData: GetFollowListResponse = {
content: data ? data.pages.flatMap((page) => page.data.content) : [],
empty: !data || data.pages.every((page) => page.data.empty),
Expand Down
23 changes: 11 additions & 12 deletions src/pages/Follow/hooks/useGetFollowerReview.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import { getFollowReview } from 'api/follow';
import { useEffect } from 'react';
import { useInfiniteQuery } from 'react-query';
import { useInfiniteQuery } from '@tanstack/react-query';

const useGetFollowerReview = (id: number) => {
const { data, hasNextPage, fetchNextPage } = useInfiniteQuery(
['review', id],
({ pageParam = '' }) => getFollowReview(id, pageParam),
{
getNextPageParam: (last) => {
const len = last.data.content.length;
if (last.data.empty) return null;
// cursor: 마지막으로 조회한 상점 id
return `cursor=${last.data.content[len - 1].shopId}`;
},
const { data, hasNextPage, fetchNextPage } = useInfiniteQuery({
queryKey: ['review', id],
queryFn: ({ pageParam = '' }) => getFollowReview(id, pageParam),
initialPageParam: 'cursor=0',
getNextPageParam: (last) => {
const len = last.data.content.length;
if (last.data.empty) return null;
// cursor: 마지막으로 조회한 상점 id
return `cursor=${last.data.content[len - 1].shopId}`;
},
);
});
const flatData = {
content: data ? data.pages.flatMap((page) => page.data.content) : [],
};
Expand Down
5 changes: 3 additions & 2 deletions src/pages/Follow/hooks/useGetFollowerReviewCount.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { getFollowerReviewCount } from 'api/follow';
import { useQuery } from 'react-query';
import { useQuery } from '@tanstack/react-query';

const useGetFollowerReviewCount = (followId: number) => {
const { data } = useQuery('reviewCount', () => getFollowerReviewCount({ followId }));
const { data } = useQuery({ queryKey: ['reviewCount'], queryFn: () => getFollowerReviewCount({ followId }) });

return data;
};

Expand Down
4 changes: 2 additions & 2 deletions src/pages/Follow/hooks/useGetRecentlyActiveFollower.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { recentlyActiveFollow } from 'api/follow';
import { useQuery } from 'react-query';
import { useQuery } from '@tanstack/react-query';

// 최대 15명만 보임, 접속한지 24시가 지나면 사라짐
const useGetRecentlyActiveFollower = () => {
const { data, isLoading } = useQuery('recent', () => recentlyActiveFollow());
const { data, isLoading } = useQuery({ queryKey: ['recent'], queryFn: () => recentlyActiveFollow() });

return { data, isLoading };
};
Expand Down
9 changes: 5 additions & 4 deletions src/pages/Follow/hooks/useRequestAndUpdate.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { requestFollow } from 'api/follow';
import { useMutation, useQueryClient } from 'react-query';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import makeToast from 'utils/ts/makeToast';

// 팔로우 요청 후 유저 목록을 다시 받아와 요청중 상태로 변경
const useRequestAndUpdate = () => {
const queryClient = useQueryClient();
const { mutate: request } = useMutation('request', (account: string) => requestFollow({ userAccount: account }), {
const { mutate: request } = useMutation({
mutationKey: ['request'],
mutationFn: (account: string) => requestFollow({ userAccount: account }),
onSuccess: () => {
queryClient.invalidateQueries('sended');
queryClient.invalidateQueries('search');
queryClient.invalidateQueries({ queryKey: ['sended', 'search'] });
},
onError: () => {
makeToast('error', '잘못된 친구 정보입니다.');
Expand Down
Loading
Loading