Skip to content
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
2 changes: 2 additions & 0 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { gsap } from "gsap";
import { ScrollTrigger } from "gsap/ScrollTrigger";
import { useEffect, useRef } from "react";
import { Route, Routes } from "react-router-dom";
import ScrollToTop from "./components/ScrollTop";
import Toastify from "./components/Toastify";
import { CreditProvider } from "./context/CreditContext";
import DefaultLayout from "./layouts/DefaultLayout";
Expand Down Expand Up @@ -40,6 +41,7 @@ function App() {
return (
<CreditProvider>
<Toastify />
<ScrollToTop />
<Routes>
<Route index element={<Landing />} />
<Route path="/" element={<DefaultLayout />}>
Expand Down
34 changes: 34 additions & 0 deletions src/apis/chartApi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// export const chartAPI = {
// getRanks: async (gender = "female", pageSize = "") => {
// try {
// const response = await baseAPI.get("/15-3/charts", {
// params: {
// gender,
// pageSize,
// },
// });
// return response.data;
// } catch (error) {
// throw new Error("λͺ©λ‘μ„ λΆˆλŸ¬μ˜€λŠ”λ° μ‹€νŒ¨ν–ˆμŠ΅λ‹ˆλ‹€.");
// }
// },
// };

import { baseAPI } from "./axios";

export const chartAPI = {
getRanks: async (gender, pageSize = 10) => {
try {
const response = await baseAPI.get(
`https://fandom-k-api.vercel.app/15-3/charts/${gender}`,
{
params: { gender, pageSize },
},
);
return response.data;
} catch (error) {
console.error("차트 뢈러였기 μ‹€νŒ¨", error);
throw new Error("λͺ©λ‘μ„ λΆˆλŸ¬μ˜€λŠ” 데 μ‹€νŒ¨ν–ˆμŠ΅λ‹ˆλ‹€.");
}
},
};
14 changes: 14 additions & 0 deletions src/components/ScrollTop.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { useEffect } from "react";
import { useLocation } from "react-router-dom";

export default function ScrollToTop() {
const { pathname } = useLocation();

useEffect(() => {
if (pathname) {
window.scrollTo(0, 0);
}
}, [pathname]);

return null;
}
69 changes: 0 additions & 69 deletions src/hooks/useChart.js

This file was deleted.

70 changes: 25 additions & 45 deletions src/pages/List/Chart/components/hooks/useChart.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { idolsAPI } from "@/apis/idolsAPI";
import { useEffect, useMemo, useState } from "react";
import { chartAPI } from "@/apis/chartAPI";
import { useEffect, useState } from "react";

const ITEMS_PER_PAGE = 10;
const TABLET_ITEMS_PER_PAGE = 5;
Expand All @@ -9,25 +9,32 @@ const getIsMobile = () => window.matchMedia("(max-width: 425px)").matches;
const getIsTablet = () =>
window.matchMedia("(min-width: 426px) and (max-width: 768px)").matches;

const useChart = () => {
const [idols, setIdols] = useState([]);
const [activeTab, setActiveTab] = useState("female");
const [visibleCount, setVisibleCount] = useState(ITEMS_PER_PAGE);
export const useChart = () => {
const [femaleIdols, setFemaleIdols] = useState([]);
const [maleIdols, setMaleIdols] = useState([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
const [visibleCount, setVisibleCount] = useState(ITEMS_PER_PAGE);

useEffect(() => {
const fetchIdols = async () => {
const fetchCharts = async () => {
try {
setLoading(true);
const res = await idolsAPI.getIdols(100);
setIdols(res.list);
} catch (e) {
console.error("μ•„μ΄λŒ 뢈러였기 μ‹€νŒ¨", e);

const femaleRes = await chartAPI.getRanks("female", 100);
const maleRes = await chartAPI.getRanks("male", 100);

setFemaleIdols(femaleRes.idols);
setMaleIdols(maleRes.idols);
} catch (err) {
console.error("차트 뢈러였기 μ‹€νŒ¨", err);
setError(err);
} finally {
setLoading(false);
}
};
fetchIdols();

fetchCharts();
}, []);

useEffect(() => {
Expand All @@ -42,49 +49,22 @@ const useChart = () => {
return () => window.removeEventListener("resize", handleResize);
}, []);

const femaleIdols = useMemo(() => {
return idols
.filter((i) => i.gender === "female")
.sort((a, b) => b.totalVotes - a.totalVotes);
}, [idols]);

const maleIdols = useMemo(() => {
return idols
.filter((i) => i.gender === "male")
.sort((a, b) => b.totalVotes - a.totalVotes);
}, [idols]);

const isFemale = activeTab === "female";
const visibleList = (isFemale ? femaleIdols : maleIdols).slice(
0,
visibleCount,
);

const handleMore = () => {
if (getIsMobile()) setVisibleCount((prev) => prev + MOBILE_ITEMS_PER_PAGE);
else if (getIsTablet())
setVisibleCount((prev) => prev + TABLET_ITEMS_PER_PAGE);
else setVisibleCount((prev) => prev + ITEMS_PER_PAGE);
};

const handleTabChange = (tab) => {
setActiveTab(tab);
if (getIsMobile()) setVisibleCount(MOBILE_ITEMS_PER_PAGE);
else if (getIsTablet()) setVisibleCount(TABLET_ITEMS_PER_PAGE);
else setVisibleCount(ITEMS_PER_PAGE);
};

return {
idols,
setIdols,
activeTab,
loading,
visibleList,
handleMore,
handleTabChange,
error,
femaleIdols,
maleIdols,
setFemaleIdols,
setMaleIdols,
visibleCount,
setVisibleCount,
handleMore,
};
};

export default useChart;
23 changes: 11 additions & 12 deletions src/pages/List/Chart/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import Button from "@/components/Button/Button";
import Circle from "@/components/Circle";
import LoadingError from "@/components/Error";
import Modal from "@/components/Modal";
import { useChart } from "@/hooks/useChart";
import ChartVoteModal from "@/pages/List/Chart/components/ChartVoteModal";
import IdolProfileModal from "@/pages/List/Chart/components/IdolProfileModal";
import { idolProfiles } from "@/pages/List/Chart/components/IdolProfiles";
import React, { useState } from "react";
import { useChart } from "./components/hooks/useChart";

import {
ChartButtonWrap,
Expand Down Expand Up @@ -41,7 +41,8 @@ const Chart = () => {
error,
femaleIdols,
maleIdols,
setIdols,
setFemaleIdols,
setMaleIdols,
visibleCount,
setVisibleCount,
handleMore,
Expand All @@ -51,10 +52,8 @@ const Chart = () => {
const closeModal = () => setIsModalOpen(false);

const isFemale = activeTab === "female";
const visibleList = (isFemale ? femaleIdols : maleIdols).slice(
0,
visibleCount,
);
const visibleList =
(isFemale ? femaleIdols : maleIdols)?.slice(0, visibleCount) || [];

const handleIdolClick = (idol) => {
const mockData = idolProfiles[idol.name];
Expand Down Expand Up @@ -133,7 +132,7 @@ const Chart = () => {
<ChartVoteModal
gender={activeTab}
idols={isFemale ? femaleIdols : maleIdols}
setIdols={setIdols}
setIdols={isFemale ? setFemaleIdols : setMaleIdols}
closeModal={closeModal}
/>
</Modal>
Expand Down Expand Up @@ -194,10 +193,10 @@ const Chart = () => {
</ChartIdolRight>
</ChartIdol>

{error ? (
<LoadingError error="chart" />
) : loading ? (
{loading ? (
<ChartList>{renderSkeletonItems()}</ChartList>
) : error ? (
<LoadingError error="chart" />
) : (
<>
<ChartList>
Expand All @@ -206,8 +205,8 @@ const Chart = () => {
))}
</ChartList>

{visibleList.length <
(isFemale ? femaleIdols.length : maleIdols.length) && (
{(isFemale ? femaleIdols : maleIdols)?.length >
visibleList.length && (
<MoreButton>
<Button size="load-more" onClick={handleMore}>
더 보기
Expand Down