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

[FE] Refactor/#643: Profile 페이지 API 로직 수정 및 React-Query 적용 #647

Merged
merged 12 commits into from
Jan 18, 2024
Merged
36 changes: 36 additions & 0 deletions frontend/src/apis/Patrick/http.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios';

const API_PREFIX = 'api';

export const BASE_URL =
process.env.APP_URL || `https://mapbefine.com/${API_PREFIX}`;

const axiosInstance = axios.create({
baseURL: BASE_URL,
timeout: 5000,
});

export interface HttpClient extends AxiosInstance {
get<T = unknown>(url: string, config?: AxiosRequestConfig): Promise<T>;
post<T = unknown>(
url: string,
data?: any,
config?: AxiosRequestConfig,
): Promise<T>;
patch<T = unknown>(
url: string,
data?: any,
config?: AxiosRequestConfig,
): Promise<T>;
put<T = unknown>(
url: string,
data?: any,
config?: AxiosRequestConfig,
): Promise<T>;
delete<T = unknown>(url: string, config?: AxiosRequestConfig): Promise<T>;
}

export const http: HttpClient = axiosInstance;

// axios.interceptors.request.use()
Copy link
Collaborator

Choose a reason for hiding this comment

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

리프레쉬 토큰 패트릭이 보여주시나요? ㅎㅎ

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

하핳..! 전 아이크만 바라보고 있습니다 그저 빛.. 🔦

axios.interceptors.response.use((res) => res.data)
6 changes: 6 additions & 0 deletions frontend/src/apis/Patrick/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { http } from './http';
import { TopicCardProps } from '../../types/Topic';

export const getProfile = () => {
return http.get<TopicCardProps[] | null>("/members/my/topics");
};
19 changes: 4 additions & 15 deletions frontend/src/components/TopicCardList/index.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Fragment, useEffect, useState } from 'react';
import { useEffect, useState } from 'react';
import { styled } from 'styled-components';

import useGet from '../../apiHooks/useGet';
import { TopicCardProps } from '../../types/Topic';
import Button from '../common/Button';
import Flex from '../common/Flex';
import Grid from '../common/Grid';
import Space from '../common/Space';
import Text from '../common/Text';
import TopicCard from '../TopicCard';
import useProfileList from '../../hooks/queries/useProfileList';

interface TopicCardListProps {
url: string;
Expand All @@ -27,18 +27,7 @@ function TopicCardList({
routePage,
children,
}: TopicCardListProps) {
const [topics, setTopics] = useState<TopicCardProps[] | null>(null);
const { fetchGet } = useGet();

const getTopicsFromServer = async () => {
fetchGet<TopicCardProps[]>(url, errorMessage, (response) => {
setTopics(response);
});
};

useEffect(() => {
getTopicsFromServer();
}, []);
const { data: topics, refetch: refetchTopic } = useProfileList();

if (!topics) return null;

Expand Down Expand Up @@ -88,7 +77,7 @@ function TopicCardList({
bookmarkCount={topic.bookmarkCount}
isInAtlas={topic.isInAtlas}
isBookmarked={topic.isBookmarked}
getTopicsFromServer={getTopicsFromServer}
getTopicsFromServer={refetchTopic}
/>
</ul>
))}
Expand Down
13 changes: 13 additions & 0 deletions frontend/src/hooks/queries/useProfileList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { useSuspenseQuery } from '@tanstack/react-query';
import { getProfile } from '../../apis/Patrick';

const useProfileList = () => {
const { data, refetch } = useSuspenseQuery({
queryKey: ['profileList'],
queryFn: getProfile,
});

return { data, refetch };
};

export default useProfileList;
17 changes: 11 additions & 6 deletions frontend/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ReactDOM from 'react-dom/client';
import ReactGA from 'react-ga4';
import { ThemeProvider } from 'styled-components';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';

import App from './App';
import ErrorBoundary from './components/ErrorBoundary';
Expand All @@ -12,6 +13,8 @@ const rootElement = document.getElementById('root');
if (!rootElement) throw new Error('Failed to find the root element');
const root = ReactDOM.createRoot(rootElement);

const queryClient = new QueryClient();

// if (process.env.NODE_ENV === 'development') {
// const { worker } = require('./mocks/browser');
// worker.start({ quiet: true });
Expand All @@ -22,10 +25,12 @@ if (process.env.REACT_APP_GOOGLE_ANALYTICS) {
}

root.render(
<ThemeProvider theme={theme}>
<GlobalStyle />
<ErrorBoundary fallback={NotFound}>
<App />
</ErrorBoundary>
</ThemeProvider>,
<QueryClientProvider client={queryClient}>
<ThemeProvider theme={theme}>
<GlobalStyle />
<ErrorBoundary fallback={NotFound}>
<App />
</ErrorBoundary>
</ThemeProvider>
</QueryClientProvider>,
);
5 changes: 3 additions & 2 deletions frontend/src/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Home from './pages/Home';
import NotFound from './pages/NotFound';
import RootPage from './pages/RootPage';
import Search from './pages/Search';
import TopicListSkeleton from './components/Skeletons/TopicListSkeleton';

const SelectedTopic = lazy(() => import('./pages/SelectedTopic'));
const NewPin = lazy(() => import('./pages/NewPin'));
Expand Down Expand Up @@ -115,9 +116,9 @@ const routes: routeElement[] = [
{
path: 'my-page',
element: (
<SuspenseComp>
<Suspense fallback={<TopicListSkeleton />}>
<Profile />
</SuspenseComp>
</Suspense>
),
withAuth: true,
},
Expand Down
Loading