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

어드민 폴더 제거 #57

Merged
merged 2 commits into from
Oct 29, 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
25 changes: 0 additions & 25 deletions src/admin/Admin.tsx

This file was deleted.

127 changes: 0 additions & 127 deletions src/admin/CreateQuiz.tsx

This file was deleted.

45 changes: 0 additions & 45 deletions src/admin/style.ts

This file was deleted.

10 changes: 0 additions & 10 deletions src/admin/types/Quiz.ts

This file was deleted.

9 changes: 0 additions & 9 deletions src/admin/types/style.ts

This file was deleted.

5 changes: 2 additions & 3 deletions src/apis/quiz.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useQuery } from '@tanstack/react-query';
import Quiz from '../admin/types/Quiz';
import api from './axios/instance';
import Quiz from '../types/Quiz';

const QUIZ = {
getQuizzes: (sectionId: number, part: Quiz['part']) => {
getQuizzes: (sectionId: Quiz['sectionId'], part: Quiz['part']) => {
return useQuery({
queryKey: ['quizzes', { sectionId, part }],
queryFn: () => api.get(`/quizzes?sectionId=${sectionId}&part=${part}`),
Copy link
Collaborator

@ssi02014 ssi02014 Oct 28, 2024

Choose a reason for hiding this comment

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

@rhehfl @bluetree7878
axios를 커스텀하게 활용하려고 api를 만든 것 같은데 쿼리스트링을 편하게 관리하는 방법이 있어요 :)
axios에는 paramsSerializer 옵션이 있는데 이를 query-string 라이브러리와 함께 활용하면 api 요청 시 쿼리를 관리하는게 편합니다. 지금과 같이 문자열로 늘어뜨려서 활용 할 필요 없어집니다.

아래는 단순 예제입니다. 😄

import queryString from 'query-string';

// 한번 더 추상화
const getAPI = ({ url, params }) => {
  const response = axiosInstance({
    method: 'GET',
    url,
    params,
    paramsSerializer: (params) => {
      // query 객체를 stringify로 변환 시 null/undefined와 빈 문자열은 제외
      return queryString.stringify(params, { skipEmptyString: true, skipNull: true })
    },
    // ... 그 외 옵션들
  });
}
// as-is
api.get(`/quizzes?sectionId=${sectionId}&part=${part}`); // 쿼리를 하나하나 작성해줘야 함
getAPI({ url: 'https://www.example.com', params: { a: 1, b: 2 }}); // params 객체로 한번에 처리
// request: GET https://www.example.com?a=1&b=2

getAPI({ url: 'https://www.example.com', params: { a: 1, b: 2, c: null, d: '', e: 3 }});
// request: GET https://www.example.com?a=1&b=2&e=3

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

오.. 이런식으로 동적으로 쿼리스트링을 붙일 수 있으면 만들어놓은 커스텀 api의 재사용성이 올라갈 것 같네요
이제까지 axios 사용하면서 저 옵션이 있는 줄 몰랐습니다 좋은 기능 & 라이브러리 추천 감사힙니다!

Copy link
Collaborator

Choose a reason for hiding this comment

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

@rhehfl 좋아요😄👍 query-string 라이브러리는 쿼리 핸들링할때 많이 쓰이는 라이브러리라 좋은 조합이에요

Expand All @@ -13,6 +13,5 @@ const QUIZ = {
staleTime: 1 * 60 * 1000,
});
},
postQuiz: (quiz: Quiz) => api.post(`/quizzes`, quiz),
};
export default QUIZ;
7 changes: 1 addition & 6 deletions src/route/Router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import Main from '../pages/main/Main';
import Quest from '../pages/Quest/Quest';
import Ranking from '../pages/Ranking/Ranking';
import Quiz from '../pages/Quiz/Quiz';
import Admin from '../admin/Admin';
import CreateQuiz from '../admin/CreateQuiz';
import Login from '../pages/login/login';
import Login from '../pages/login/Login';
export default function Router() {
return (
<>
Expand All @@ -16,9 +14,6 @@ export default function Router() {
<Route path="/ranking" element={<Ranking />}></Route>
<Route path="/quiz/:section/:part" element={<Quiz />}></Route>
<Route path="/login" element={<Login />}></Route>
{/*어드민 페이지 부분 문제조회/추가 이외에 규모 확장 시 레포 분리 */}
<Route path="/admin" element={<Admin />} />
<Route path="/admin/create-quiz" element={<CreateQuiz />}></Route>
</Routes>
</BrowserRouter>
</>
Expand Down
2 changes: 1 addition & 1 deletion src/types/Quiz.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default interface Quiz {
id: number;
partId: number;
part: string;
sectionId: number;
title: string;
question: string;
Expand Down