-
Notifications
You must be signed in to change notification settings - Fork 3
Fix/#28/favorite idol UI #126
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
Changes from all commits
4b90d3e
d9cebb8
d1e4a1b
43787f8
1470179
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import axios from 'axios'; | ||
|
|
||
| export const fetchIdols = async (pageSize = 30) => { | ||
| try { | ||
| const response = await axios.get( | ||
| `https://fandom-k-api.vercel.app/13-3/idols?pageSize=${pageSize}` | ||
| ); | ||
| return response.data.list; | ||
| } catch (error) { | ||
| console.error('아이돌 데이터를 불러오는 중 오류 발생:', error); | ||
| return []; | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,33 +1,175 @@ | ||
| import React, { useState, useEffect } from 'react'; | ||
| import axios from 'axios'; | ||
| import Header from '@/components/Header'; | ||
| import IdolCard from '@/components/IdolCard'; | ||
|
|
||
| import nextIcon from '@/assets/icons/nextIcon.svg'; | ||
| import prevIcon from '@/assets/icons/prevIcon.svg'; | ||
| import checkIcon from '@/assets/images/check.png'; | ||
| import { fetchIdols } from '@/apis/idolApi'; | ||
|
|
||
| const storageKey = 'favoriteIdols'; | ||
|
|
||
| const MyPage = () => { | ||
| const [idols, setIdols] = useState([]); | ||
| const [selectedIdols, setSelectedIdols] = useState([]); | ||
| const [favoriteIdols, setFavoriteIdols] = useState([]); | ||
| const [currentPage, setCurrentPage] = useState(0); | ||
| const [isClicked, setIsClicked] = useState(false); | ||
|
|
||
| const getItemsPerPage = () => { | ||
| if (typeof window !== 'undefined') { | ||
| if (window.innerWidth < 640) return 6; | ||
| if (window.innerWidth < 768) return 8; | ||
| return 16; | ||
| } | ||
| return 16; | ||
|
Comment on lines
+21
to
+26
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 결국 return 16을 할 거면 if (tyeof window !== 'undefined') 는 무의미한 거 같아요! 또 저희 반응형 크기는 https://github.com/yoonc01/Fandom-K/blob/main/tailwind.config.js#L25 이거에요!
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 확인했습니다. |
||
| }; | ||
|
|
||
| useEffect(() => { | ||
| const fetchIdols = async () => { | ||
| try { | ||
| const response = await axios.get( | ||
| 'https://fandom-k-api.vercel.app/13-3/idols?pageSize=30' | ||
| ); | ||
| setIdols(response.data.list); // API 응답 데이터 리스트를 저장 | ||
| } catch (error) { | ||
| console.error('아이돌 데이터를 불러오는 중 오류 발생:', error); //아이돌 데이터 로딩 중 에러 확인 | ||
| } | ||
| const loadIdols = async () => { | ||
| const data = await fetchIdols(30); | ||
| setIdols(data); | ||
| }; | ||
| loadIdols(); | ||
| }, []); | ||
|
|
||
| fetchIdols(); | ||
| useEffect(() => { | ||
| const storedFavorites = localStorage.getItem(storageKey); | ||
| if (storedFavorites) { | ||
| setFavoriteIdols(storedFavorites.split(',')); | ||
| } | ||
| }, []); | ||
|
|
||
| useEffect(() => { | ||
| localStorage.setItem(storageKey, favoriteIdols.join(',')); | ||
| }, [favoriteIdols]); | ||
|
Comment on lines
+37
to
+46
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 로컬스토리지 다루는 부분을 utils에 따로 설정할 수 있을 거 같아요! 또 로컬스토리지 다루는 부분에 대해서 재현님과 상의해서 매개변수로 storageKey를 받도록 해서 함수를 합칠 수도 있을 거 같아요!
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 어려워요 ㅋㅋ낼 팀미팅시간에 설명 한번만 부탁드려도 될까유// |
||
|
|
||
| const handleToggle = (idolId) => { | ||
| setSelectedIdols((prev) => | ||
| prev.includes(idolId) | ||
| ? prev.filter((id) => id !== idolId) | ||
| : [...prev, idolId] | ||
| ); | ||
| setIsClicked(true); | ||
| }; | ||
|
|
||
| const handleAddFavorites = () => { | ||
| if (!isClicked) return; | ||
|
|
||
| setFavoriteIdols((prev) => [...prev, ...selectedIdols]); | ||
| setSelectedIdols([]); | ||
| setIsClicked(false); | ||
| }; | ||
|
|
||
| const handleRemoveFavorite = (idolId) => { | ||
| setFavoriteIdols((prev) => prev.filter((id) => id !== idolId)); | ||
| }; | ||
|
|
||
| const nextPage = () => { | ||
| const itemsPerPage = getItemsPerPage(); | ||
| if ((currentPage + 1) * itemsPerPage < idols.length) { | ||
| setCurrentPage((prev) => prev + 1); | ||
| } | ||
| }; | ||
|
|
||
| const prevPage = () => { | ||
| if (currentPage > 0) { | ||
| setCurrentPage((prev) => prev - 1); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="h-screen w-full bg-midnightBlack flex items-center justify-center"> | ||
| <div className="flex w-full max-w-[1200px] px-4 justify-center gap-6"> | ||
| {idols.length > 0 ? ( | ||
| idols.map((idol) => <IdolCard key={idol.id} idol={idol} />) | ||
| ) : ( | ||
| <p className="text-white">아이돌 데이터를 불러오는 중...</p> | ||
| )} | ||
| <div className="w-full min-h-screen bg-[#02000E] flex flex-col items-center "> | ||
| <Header /> | ||
|
Comment on lines
+83
to
+84
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이거는 이전에 썼던 bg-midnightBlack 잘 쓰셨어요! |
||
|
|
||
| <div className="w-full max-w-[1200px] flex flex-col items-center py-6 sm:py-10"> | ||
| <h1 className="text-white text-[16px] sm:text-[20px] md:text-[24px] font-bold self-start"> | ||
| 내가 관심있는 아이돌 | ||
| </h1> | ||
|
|
||
| <div className="w-full overflow-x-auto"> | ||
| <div className="flex gap-4 mt-4 min-h-[150px] min-w-max"> | ||
| {favoriteIdols.length > 0 ? ( | ||
| favoriteIdols.map((idolId) => { | ||
| const idol = idols.find((i) => i.id === idolId); | ||
| return idol ? ( | ||
| <div key={idol.id} className="flex-shrink-0"> | ||
| <IdolCard | ||
| idol={idol} | ||
| onToggle={handleRemoveFavorite} | ||
| className="w-[88px] h-[88px] rounded-full object-cover" | ||
| /> | ||
| </div> | ||
| ) : null; | ||
| }) | ||
| ) : ( | ||
| <p className="text-gray-500 text-center w-full mt-[50px]"> | ||
| 관심있는 아이돌을 추가해보세요. | ||
| </p> | ||
| )} | ||
| </div> | ||
| </div> | ||
|
|
||
| <div className="w-[1200px] h-[1px] bg-gray-600 my-6"></div> | ||
|
|
||
| <h2 className="text-white text-[16px] sm:text-[20px] md:text-[24px] font-bold self-start"> | ||
| 관심 있는 아이돌을 추가해보세요. | ||
| </h2> | ||
|
|
||
| <div className="relative w-full max-w-[1200px] mt-[20px]"> | ||
| <button | ||
| onClick={prevPage} | ||
| disabled={currentPage === 0} | ||
| className="absolute w-[29px] h-[135px] top-1/2 -left-[40px] bg-[#1B1B1B]/80 | ||
| flex items-center justify-center rounded-lg opacity-80 hover:opacity-100 transition-all z-10 | ||
| disabled:opacity-50 disabled:cursor-not-allowed transform -translate-y-1/2" | ||
| > | ||
| <img src={prevIcon} alt="Previous" className="w-4 h-4" /> | ||
| </button> | ||
|
|
||
| <button | ||
| onClick={nextPage} | ||
| disabled={(currentPage + 1) * getItemsPerPage() >= idols.length} | ||
| className="absolute w-[29px] h-[135px] top-1/2 -right-[40px] bg-[#1B1B1B]/80 | ||
| flex items-center justify-center rounded-lg opacity-80 hover:opacity-100 transition-all z-10 | ||
| disabled:opacity-50 disabled:cursor-not-allowed transform -translate-y-1/2" | ||
| > | ||
| <img src={nextIcon} alt="Next" className="w-4 h-4" /> | ||
| </button> | ||
|
|
||
| <div className="grid grid-cols-3 sm:grid-cols-4 md:grid-cols-8 gap-6 mt-4 mx-auto bg-[#02000E] min-h-[300px]"> | ||
| {idols | ||
| .slice( | ||
| currentPage * getItemsPerPage(), | ||
| (currentPage + 1) * getItemsPerPage() | ||
| ) | ||
| .map((idol) => ( | ||
| <div | ||
| key={idol.id} | ||
| className="relative w-[98px] h-[98px] rounded-full border-[2px] border-[#FF4D78] flex items-center justify-center" | ||
| onClick={() => handleToggle(idol.id)} | ||
| > | ||
| <IdolCard | ||
| idol={idol} | ||
| onToggle={handleToggle} | ||
| isSelectable={true} | ||
| className="w-[88px] h-[88px] rounded-full object-cover" | ||
| /> | ||
| </div> | ||
| ))} | ||
| </div> | ||
| </div> | ||
|
|
||
| <button | ||
| onClick={handleAddFavorites} | ||
| disabled={!isClicked} | ||
| className={`w-[255px] h-[50px] mt-6 text-white rounded-full text-lg font-bold | ||
| bg-[#FE578F] hover:bg-[#e4567e] transition-all | ||
| ${!isClicked ? 'opacity-50 cursor-not-allowed' : 'cursor-pointer'}`} | ||
| > | ||
| + 추가하기 | ||
| </button> | ||
| </div> | ||
| </div> | ||
| ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,21 @@ | ||
| import { defineConfig } from 'vite'; | ||
| import react from '@vitejs/plugin-react'; | ||
| import path from "path"; | ||
| import path from 'path'; | ||
|
|
||
| // https://vite.dev/config/ | ||
| export default defineConfig({ | ||
| plugins: [react()], | ||
| resolve: { | ||
| alias: { | ||
| "@": path.resolve(__dirname, "src"), // 수정된 부분 | ||
| '@': path.resolve(__dirname, 'src'), | ||
| }, | ||
| }, | ||
| }); | ||
| server: { | ||
| proxy: { | ||
| '/api': { | ||
| target: 'https://fandom-k-api.vercel.app', | ||
| changeOrigin: true, | ||
| secure: false, | ||
| }, | ||
| }, | ||
| }, | ||
|
Comment on lines
+12
to
+20
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 프록시 서버를 설정하신 이유를 알고 싶습니다!
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 서버 오류시 프록시 사용을 권장한다고 하더라구요 굳이 필요 없나용
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 필요없을 거 같긴 해요! |
||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
줄바꿈을 왜 주셨는지 알 수 있을까요??
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
중간에 하다가 올린거라 그래요 ㅋㅋ