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

๐Ÿ› ๏ธ ๋„คํŠธ์›Œํฌ ๋ถˆ์•ˆ์ • ํ† ์ŠคํŠธ ๋ฉ”์‹œ์ง€ ๊ด€๋ฆฌ #47

Merged
merged 17 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
3419432
feat: react-query keys ์œ„์น˜ ์ด๋™
BangDori May 10, 2024
d16c55f
feat: ์ฟผ๋ฆฌ ํ‚ค ๊ด€๋ฆฌ ํŒŒ์ผ ๋ช… ์ˆ˜์ •
BangDori May 10, 2024
9b32d93
feat: queryClient shared ๊ด€๋ฆฌ
BangDori May 10, 2024
090bdf0
refactor: ๋„คํŠธ์›Œํฌ ์—๋Ÿฌ ํ† ์ŠคํŠธ ์ตœ์ƒ๋‹จ ์œ„์น˜
BangDori May 10, 2024
9901470
feat: ๋ชจ๋“  API ์—๋Ÿฌ์— ๋Œ€ํ•ด ํ† ์ŠคํŠธ ๋ฉ”์‹œ์ง€ ๋ Œ๋”๋ง
BangDori May 10, 2024
b161305
feat: ์ธํ„ฐ๋„ท์— ์—ฐ๊ฒฐ๋˜์ง€ ์•Š์„ ์‹œ, ํ† ์ŠคํŠธ ๋ฉ”์‹œ์ง€ ๋ Œ๋”๋ง X
BangDori May 10, 2024
207611d
feat: consts ํด๋”๋กœ ์ด๋™
BangDori May 10, 2024
7f7de75
feat: ์ฟผ๋ฆฌ ์—๋Ÿฌ ๋ฐ ํ† ์ŠคํŠธ ํ•ธ๋“ค๋Ÿฌ ํ•จ์ˆ˜ ๋ถ„๋ฆฌ
BangDori May 10, 2024
64e8ed7
feat: ํ† ์ŠคํŠธ ๋ฒ„ํŠผ ๋ฐ ํ”„๋กœ๊ทธ๋ ˆ์Šค๋ฐ” ์Šคํƒ€์ผ ์ˆ˜์ •
BangDori May 10, 2024
0a5cb52
feat: ํ† ์ŠคํŠธ ์ปจํ…Œ์ด๋„ˆ ์„ค์ • ์ˆ˜์ •
BangDori May 10, 2024
282bda0
feat: autoClose 3์ดˆ ์„ค์ •
BangDori May 10, 2024
48101d4
feat: handleQueryError ๋ฉ”์„œ๋“œ ์ˆ˜์ •
BangDori May 10, 2024
f7c1b94
feat: like mutation ์—๋Ÿฌ ํ† ์ŠคํŠธ ๋ฉ”์‹œ์ง€ ์ œ๊ฑฐ
BangDori May 10, 2024
39b90ff
feat: handleQuery ํŒŒ์ผ๋ช… ์ˆ˜์ •
BangDori May 10, 2024
9c5c126
feat: ์ฟผ๋ฆฌ ์„ฑ๊ณต ์‹œ ํ† ์ŠคํŠธ ๋ฉ”์‹œ์ง€ ์ œ๊ฑฐ
BangDori May 10, 2024
bfcb610
feat: handleQuery ์ฃผ์„ ์ˆ˜์ •
BangDori May 10, 2024
73449ea
feat: ์ข‹์•„์š” ๋ฒ„ํŠผ ๋น„ํ™œ์„ฑํ™” ์ƒ‰์ƒ ์ˆ˜์ •
BangDori May 10, 2024
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
6 changes: 5 additions & 1 deletion src/app/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ import { QueryClientProvider } from '@tanstack/react-query';
import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
import { RouterProvider } from 'react-router-dom';

import { queryClient } from './providers/query-client';
import { queryClient } from '@/shared/react-query';
import { NetworkErrorToast } from '@/shared/ui';

import { router } from './routers/index';
import './styles/global.scss';

function App() {
return (
<QueryClientProvider client={queryClient}>
<RouterProvider router={router} />

<NetworkErrorToast />
<ReactQueryDevtools />
</QueryClientProvider>
);
Expand Down
13 changes: 0 additions & 13 deletions src/app/providers/query-client.ts

This file was deleted.

4 changes: 4 additions & 0 deletions src/app/styles/_reset.scss
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ button {
background-color: transparent;
border: none;
padding: 0;

&:disabled {
color: inherit;
}
}

.icon {
Expand Down
2 changes: 1 addition & 1 deletion src/features/feed-main-like/api/useLikes.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';

import { requestLikeFeed, requestUnlikeFeed } from '@/shared/axios';
import { QUERY_KEYS } from '@/shared/consts';
import { QUERY_KEYS } from '@/shared/react-query';
import { isErrorResponse } from '@/shared/utils';

import { FeedsQueryData } from '../consts';
Expand Down
1 change: 0 additions & 1 deletion src/shared/consts/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export { QUERY_KEYS } from './query-key/queryKeys';
export type * from './types';
export * from './color';
31 changes: 31 additions & 0 deletions src/shared/react-query/consts/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {
MutationCache,
QueryCache,
QueryClient,
QueryClientConfig,
} from '@tanstack/react-query';

import {
handleMutationError,
handleQueryError,
handleQuerySuccess,
} from '../dir';

const queryClientOptions: QueryClientConfig = {
defaultOptions: {
queries: {
staleTime: 10 * 60 * 1000, // 10 minutes
gcTime: 15 * 60 * 1000, // 15 minutes
refetchOnWindowFocus: false,
retry: false,
},
},
queryCache: new QueryCache({
onSuccess: () => handleQuerySuccess(),
onError: (_, query) => handleQueryError(query),
}),
mutationCache: new MutationCache({
onError: () => handleMutationError(),
}),
};
export const queryClient = new QueryClient(queryClientOptions);
2 changes: 2 additions & 0 deletions src/shared/react-query/consts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { queryClient } from './client';
export { QUERY_KEYS } from './keys';
31 changes: 31 additions & 0 deletions src/shared/react-query/dir/handleQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Query, QueryKey } from '@tanstack/react-query';

import { removeErrorHandler, showErrorHandler } from './toastHandlers';

/**
* ์ฟผ๋ฆฌ ์„ฑ๊ณต ํ•ธ๋“ค๋Ÿฌ
*/
export function handleQuerySuccess() {
removeErrorHandler();
}

/**
* ์ฟผ๋ฆฌ ์—๋Ÿฌ ํ•ธ๋“ค๋Ÿฌ ํ•จ์ˆ˜
* @param query ์ฟผ๋ฆฌ
* @if ํ”ผ๋“œ ๋ฉ”์ธ ํŽ˜์ด์ง€ 2 ํŽ˜์ด์ง€๋ถ€ํ„ฐ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ๋„คํŠธ์›Œํฌ ์—๋Ÿฌ ํ† ์ŠคํŠธ๋ฅผ ๋„์›๋‹ˆ๋‹ค.
*/
export function handleQueryError(
query: Query<unknown, unknown, unknown, QueryKey>,
) {
const { queryKey, state } = query;

// feeds ์ฟผ๋ฆฌ์—์„œ 2 ํŽ˜์ด์ง€๋ถ€ํ„ฐ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ๋„คํŠธ์›Œํฌ ์—๋Ÿฌ ํ† ์ŠคํŠธ๋ฅผ ๋„์›๋‹ˆ๋‹ค.
if (queryKey[0] === 'feeds' && state.data) showErrorHandler();
}

/**
* ๋ฎคํ…Œ์ด์…˜ ์—๋Ÿฌ ํ•ธ๋“ค๋Ÿฌ
*/
export function handleMutationError() {
return;
}
2 changes: 2 additions & 0 deletions src/shared/react-query/dir/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './handleQuery';
export * from './toastHandlers';
27 changes: 27 additions & 0 deletions src/shared/react-query/dir/toastHandlers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Bounce, toast } from 'react-toastify';

const id = 'react-query-toast';

/**
* ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ํ† ์ŠคํŠธ ๋ฉ”์‹œ์ง€๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
*/
export function showErrorHandler() {
// reference: https://fkhadra.github.io/react-toastify/api/toast
if (!toast.isActive(id)) {
toast('์ธํ„ฐ๋„ท ์—ฐ๊ฒฐ์ด ๋ถˆ์•ˆ์ •ํ•ด์š”', {
toastId: id,
autoClose: 3000,
position: 'bottom-center',
transition: Bounce,
});
}
}

/**
* ์—๋Ÿฌ ๋ฉ”์‹œ์ง€ ํ† ์ŠคํŠธ๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
*/
export function removeErrorHandler() {
if (toast.isActive(id)) {
toast.dismiss(id);
}
}
1 change: 1 addition & 0 deletions src/shared/react-query/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './consts';
2 changes: 1 addition & 1 deletion src/shared/ui/network-error-toast/NetworkErrorToast.scss
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

.Toastify__close-button,
.Toastify__progress-bar--wrp {
display: none;
visibility: hidden;
}
}
}
38 changes: 4 additions & 34 deletions src/shared/ui/network-error-toast/NetworkErrorToast.tsx
Original file line number Diff line number Diff line change
@@ -1,47 +1,17 @@
import { useEffect, useRef } from 'react';
import { ToastContainer, toast } from 'react-toastify';
import { ToastContainer } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';

import './NetworkErrorToast.scss';
import { Icon } from '..';

interface NetworkToastErrorProps {
isVisible: boolean;
errorMessage: string;
}

export const NetworkErrorToast: React.FC<NetworkToastErrorProps> = ({
isVisible,
errorMessage,
}) => {
const toastId = useRef<number | string>(-1);

useEffect(() => {
// ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๊ฐ€ ๋ณด์—ฌ์•ผ ํ•˜๊ณ , ํ˜„์žฌ ํ† ์ŠคํŠธ๊ฐ€ ํ‘œ์‹œ๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด,
// ์ƒˆ๋กœ์šด ํ† ์ŠคํŠธ๋ฅผ ํ‘œ์‹œํ•˜๊ณ  ๊ทธ ID๋ฅผ toastId์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
if (isVisible && toastId.current === -1) {
toastId.current = toast(errorMessage);
return;
}

// ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๊ฐ€ ๋ณด์—ฌ์ง€์ง€ ์•Š์•„์•ผ ํ•˜๊ณ , ํ˜„์žฌ ํ† ์ŠคํŠธ๊ฐ€ ํ‘œ์‹œ๋˜์–ด ์žˆ๋‹ค๋ฉด,
// ํ† ์ŠคํŠธ๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  toastId๋ฅผ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค.
if (!isVisible && toastId.current !== -1) {
toast.dismiss(toastId.current);
toastId.current = -1;
return;
}
}, [isVisible, errorMessage]);

export const NetworkErrorToast = () => {
return (
<ToastContainer
className='network-error-toast b1semi'
position='bottom-center'
autoClose={false}
icon={<Icon name='caution' width='20' height='20' />}
hideProgressBar={true}
rtl={false}
limit={1}
pauseOnHover={false}
pauseOnFocusLoss={false}
theme='colored'
/>
);
Expand Down
3 changes: 2 additions & 1 deletion src/widgets/feed-main-list/api/useInfinityFeeds.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useInfiniteQuery } from '@tanstack/react-query';

import { axiosInstance } from '@/shared/axios';
import { FetchFeeds, QUERY_KEYS } from '@/shared/consts';
import { FetchFeeds } from '@/shared/consts';
import { QUERY_KEYS } from '@/shared/react-query';

async function fetchFeeds(
page: number,
Expand Down
6 changes: 1 addition & 5 deletions src/widgets/feed-main-list/ui/FeedMainList.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NetworkError, NetworkErrorToast, Observer } from '@/shared/ui';
import { NetworkError, Observer } from '@/shared/ui';

import { useInfinityFeeds } from '../api/useInfinityFeeds';

Expand Down Expand Up @@ -42,10 +42,6 @@ export const FeedMainList = () => {
)}
{hasNextFeeds && <SkeletonFeedMainList count={3} />}
</div>
<NetworkErrorToast
isVisible={isError && !!feeds}
errorMessage='์ธํ„ฐ๋„ท ์—ฐ๊ฒฐ์ด ๋ถˆ์•ˆ์ •ํ•ด์š”'
/>
</section>
);
};
Loading