Skip to content

Commit

Permalink
fix: renderData 함수 추가 및 타입 설정
Browse files Browse the repository at this point in the history
  • Loading branch information
2NNS-V committed Sep 5, 2024
1 parent 88c663a commit 767c3d0
Showing 1 changed file with 35 additions and 34 deletions.
69 changes: 35 additions & 34 deletions src/pages/AnalyticsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,31 @@ interface axiosProps {
top: Array<[number, string]>;
}

const renderData = (data: axiosProps | null, loading: boolean, error: AxiosError<string> | Error | null) => {

Check failure on line 18 in src/pages/AnalyticsPage.tsx

View workflow job for this annotation

GitHub Actions / e2e

Cannot find name 'AxiosError'.
if (loading) return <Text size="m">Loading...</Text>;
if (error) return <Text size="m">Error: {error}</Text>;
if (data?.top.length == 0) {
return (
<Text size="m" color="secondary">
데이터가 존재하지 않습니다.
</Text>
);
}
if (data && data.top) {
return (
<TableContainer>
{data.top.map((value, index) => (
<Card key={index}>
<Rank>{index + 1}</Rank>
<Type>{value[0]}</Type>
</Card>
))}
</TableContainer>
);
}
return null;
};

export default function AnalyticsPage() {
const [selectedMajor, setSelectedMajor] = React.useState<string>("humanities");
const navigate = useNavigate();
Expand All @@ -25,17 +50,21 @@ export default function AnalyticsPage() {

const {
data: allData,
loading,
error,
} = useAxios<axiosProps>(
loading: allDataLoading,
error: allDataError,
} = useAxios<axiosProps | null>(
{
url: `/stats/top/mbti`,
method: "GET",
},
[],
);

const { data: majorData } = useAxios<axiosProps>(
const {
data: majorData,
loading: majorDataLoading,
error: majorDataError,
} = useAxios<axiosProps | null>(
{
url: `/stats/top/department?key=${selectedMajor}`,
method: "GET",
Expand All @@ -52,21 +81,7 @@ export default function AnalyticsPage() {
<Text size="m">어떤 유형이 가장 많을까요?</Text>
</TitleContainer>

<Main>
{loading && <Text size="m">Loading...</Text>}
{error && <Text size="m">Error: {error}</Text>}

{allData && allData.top && (
<TableContainer>
{allData.top.map((value, index) => (
<Card key={index}>
<Rank>{index + 1}</Rank>
<Type>{value[0]}</Type>
</Card>
))}
</TableContainer>
)}
</Main>
<Main>{renderData(allData, allDataLoading, allDataError)}</Main>

<MiddleSection />

Expand All @@ -84,21 +99,7 @@ export default function AnalyticsPage() {

<Text size="m">어떤 유형이 가장 많을까요?</Text>
</TitleContainer>
<Main>
{loading && <Text size="m">Loading...</Text>}
{error && <Text size="m">Error: {error}</Text>}

{majorData && majorData.top && (
<TableContainer>
{majorData.top.map((value, index) => (
<Card key={index}>
<Rank>{index + 1}</Rank>
<Type>{value[0]}</Type>
</Card>
))}
</TableContainer>
)}
</Main>
<Main>{renderData(majorData, majorDataLoading, majorDataError)}</Main>
</>
);
}

0 comments on commit 767c3d0

Please sign in to comment.