Skip to content

Commit

Permalink
Merge pull request #53 from School-of-Company/refector/api
Browse files Browse the repository at this point in the history
🔀 1차 api 변경 (tanstack query로 변경)
  • Loading branch information
Ethen1264 authored Dec 10, 2024
2 parents 4e40317 + df284e5 commit 24400ac
Show file tree
Hide file tree
Showing 15 changed files with 383 additions and 135 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"plugin:prettier/recommended",
"prettier",
"plugin:@next/next/recommended",
"plugin:storybook/recommended"
"plugin:storybook/recommended",
"plugin:@tanstack/eslint-plugin-query/recommended"
],
"parser": "@typescript-eslint/parser",
"ignorePatterns": ["node_modules/**", "**/dist/**", ".next/**"],
Expand Down
164 changes: 164 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@storybook/nextjs": "^8.3.5",
"@storybook/react": "^8.3.5",
"@storybook/test": "^8.3.5",
"@tanstack/eslint-plugin-query": "^5.62.1",
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
Expand Down
2 changes: 0 additions & 2 deletions src/app/api/expo/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ export async function GET() {
return NextResponse.json(response.data);
} catch (error) {
const axiosError = error as AxiosError<{ message: string }>;

const status = axiosError.response?.status || 500;
const message = axiosError.response?.data?.message || 'expoList failed';

return NextResponse.json({ error: message }, { status });
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/shared/hocs/withLoading.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react';
import { Loading } from '../ui';

type WithLoadingProps = {
isLoading: boolean;
children: React.ReactNode;
};

const withLoading = ({ isLoading, children }: WithLoadingProps) => {
if (isLoading) return <Loading />;
return <>{children}</>;
};

export default withLoading;
15 changes: 14 additions & 1 deletion src/shared/libs/TanstackProviders.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
'use client';

import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import {
QueryCache,
QueryClient,
QueryClientProvider,
} from '@tanstack/react-query';
import { useState } from 'react';
import { toast } from 'react-toastify';

const TanstackProviders = ({ children }: { children: React.ReactNode }) => {
const [queryClient] = useState(
Expand All @@ -10,8 +15,16 @@ const TanstackProviders = ({ children }: { children: React.ReactNode }) => {
defaultOptions: {
queries: {
staleTime: 60 * 1000,
gcTime: 60 * 5000,
retryDelay: 1500,
retry: 5,
},
},
queryCache: new QueryCache({
onError: (error) => {
toast.error(`Query failed: ${(error as Error).message}`);
},
}),
}),
);

Expand Down
28 changes: 28 additions & 0 deletions src/shared/types/expo-detail/type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export interface ExpoDetail {
title: string;
description: string;
startedDay: string;
finishedDay: string;
location: string;
coverImage: string;
x: number;
y: number;
}

export interface ExpoTrainingDetail {
title: string;
startedAt: string;
endedAt: string;
category: 'ESSENTIAL' | 'CHOICE';
}

export interface ExpoTraining {
essential: ExpoTrainingDetail[];
choice: ExpoTrainingDetail[];
}

export interface ExpoStandard {
title: string;
startedAt: string;
endedAt: string;
}
File renamed without changes.
1 change: 1 addition & 0 deletions src/shared/ui/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export { default as Input } from './Input';
export { default as Modal } from './Modal';
export { default as PageHeader } from './PageHeader';
export { default as CheckBox } from './CheckBox';
export { default as Loading } from './Loading';
23 changes: 23 additions & 0 deletions src/widgets/expo-detail/api/getExpoDetail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import axios from 'axios';
import {
ExpoDetail,
ExpoStandard,
ExpoTrainingDetail,
} from '@/shared/types/expo-detail/type';

export const getExpoDetail = async (id: number): Promise<ExpoDetail> => {
const response = await axios.get(`/api/expo/${id}`);
return response.data;
};

export const getExpoStandard = async (id: number): Promise<ExpoStandard[]> => {
const response = await axios.get(`/api/standard/program/${id}`);
return response.data;
};

export const getExpoTraining = async (
id: number,
): Promise<ExpoTrainingDetail[]> => {
const response = await axios.get(`/api/training/program/${id}`);
return response.data;
};
46 changes: 46 additions & 0 deletions src/widgets/expo-detail/model/useExpoDetail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { useQuery } from '@tanstack/react-query';
import {
ExpoDetail,
ExpoStandard,
ExpoTrainingDetail,
} from '@/shared/types/expo-detail/type';
import {
getExpoDetail,
getExpoStandard,
getExpoTraining,
} from '../api/getExpoDetail';

interface ExpoTraining {
essential: ExpoTrainingDetail[];
choice: ExpoTrainingDetail[];
}

export const useExpoQueries = (id: number) => {
const expoDetailQuery = useQuery<ExpoDetail, Error>({
queryKey: ['expoDetail', id],
queryFn: () => getExpoDetail(id),
});

const expoStandardQuery = useQuery<ExpoStandard[], Error>({
queryKey: ['expoStandard', id],
queryFn: () => getExpoStandard(id),
});

const expoTrainingQuery = useQuery<ExpoTrainingDetail[], Error, ExpoTraining>(
{
queryKey: ['expoTraining', id],
queryFn: () => getExpoTraining(id),
select: (data) => ({
essential: data.filter((item) => item.category === 'ESSENTIAL'),
choice: data.filter((item) => item.category === 'CHOICE'),
}),
},
);

const isLoading =
expoDetailQuery.isLoading ||
expoStandardQuery.isLoading ||
expoTrainingQuery.isLoading;

return { expoDetailQuery, expoStandardQuery, expoTrainingQuery, isLoading };
};
Loading

0 comments on commit 24400ac

Please sign in to comment.