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 = (url: string) => {
return http.get<TopicCardProps[] | null>(url);
Copy link
Collaborator

Choose a reason for hiding this comment

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

url을 매개변수로 받아오는 이유가 있을까요?? getProfile 이라고 한다면 프로파일에 관련된 정보를 받아올 것 같고 반환 타입을 TopicCardProps[] 라고 명시해주었으니 재사용이 어려울 듯한데, url을 바로 적지 않고 외부에서 받아오도록 한 이유가 있나용?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

오 이 부분에 대해서 세인 리뷰에 달았는데 저도 동의합니다! 어차피 분리가 되어 다른 곳에서 사용하지 않을 것이니 바로 url을 적으면 될 것 같습니다! 구우웃!

Copy link
Collaborator

Choose a reason for hiding this comment

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

제가 했던 getTopics는 홈화면에서 비슷하게 3번사용되서 이렇게 url을 넘겨받는 방법으로 했는데 이 부분은 한번만 쓰이는건가보내요

};
8 changes: 5 additions & 3 deletions frontend/src/components/TopicCardList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ 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 @@ -29,11 +30,12 @@ function TopicCardList({
}: TopicCardListProps) {
const [topics, setTopics] = useState<TopicCardProps[] | null>(null);
const { fetchGet } = useGet();
const { data } = useProfileList(url);
Copy link
Collaborator

Choose a reason for hiding this comment

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

TopicCardList 내부에서 사용되서 url을 밖에서 받아오신거군요. 이 컴포넌트가 제가 맡은 페이지에서만 사용되는 줄 알았는데 아니였군요. 개인적으로 이 컴포넌트는 TopicCardContainer랑 모습이 동일해서 제거할 생각인데.. 패트릭은 어떻게 생각하시나요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

오호 확인해보니 비슷한데 스와이프가 적용된 것이군요!

이 부분 좀 있다가 회의 때 한번 더 말해보면 좋을 것 같습니다! 이게 한번에 보는거랑 스와이프로 보는 것 중 선택해서 수정만 하면 될 것 같아요! 굿굿


const getTopicsFromServer = async () => {
fetchGet<TopicCardProps[]>(url, errorMessage, (response) => {
setTopics(response);
});
if (data !== undefined) {
setTopics(data);
Copy link
Collaborator

Choose a reason for hiding this comment

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

서버 상태의 data를 직접 사용하지 않고 setTopics를 통해 useState 상태로 다시 할당한 이유가 있나용?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

아하! 역시 이래서 리뷰가 필요한가 봅니다 바로 컨펌 후 수정하도록 하겠습니다!! 👍

}
};

useEffect(() => {
Expand Down
15 changes: 15 additions & 0 deletions frontend/src/hooks/queries/useProfileList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useQuery } from '@tanstack/react-query';
import { getProfile } from '../../apis/Patrick';
import { TopicCardProps } from '../../types/Topic';

const useProfileList = (url: string) => {
return useQuery({
queryKey: ['profileList'],
queryFn: async () => {
const data = await getProfile(url);
return data;
Copy link
Collaborator

Choose a reason for hiding this comment

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

getProfile 로 넘기지 않고 async 함수를 한 번 감싼 이유가 url 인자 때문일까요?

Copy link
Collaborator

Choose a reason for hiding this comment

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

쿼리쓰면 요기 async await없어도 되는거같던데???

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

바로 삭제쓰~ 감사합니다 여러분 😆

},
});
};

export default useProfileList;
Loading