@@ -8,6 +8,8 @@ import useFetchLinks from "@/hooks/useFetchLinks";
88import { useRouter } from "next/router" ;
99import { useState } from "react" ;
1010import { parse } from "cookie" ;
11+ import LoadingSpinner from "@/components/LoadingSpinner" ;
12+ import EmptyFavoriteList from "@/components/Favorite/EmptyFavoriteList" ;
1113
1214interface FavoriteDataType {
1315 id : number ;
@@ -31,11 +33,20 @@ export const getServerSideProps: GetServerSideProps = async (
3133 const { req } = context ;
3234 const cookies = parse ( req . headers . cookie || "" ) ;
3335 const accessToken = cookies . accessToken ;
36+
3437 try {
35- const res = await axiosInstance . get ( "/favorites?page=1&pageSize=10" , {
38+ if ( ! accessToken ) {
39+ return {
40+ redirect : {
41+ destination : "/login" ,
42+ permanent : false ,
43+ } ,
44+ } ;
45+ }
46+
47+ const res = await axiosInstance . get ( "/favorites" , {
3648 headers : { Authorization : `Bearer ${ accessToken } ` } ,
3749 } ) ;
38-
3950 const { list, totalCount } = res . data || { list : [ ] , totalCount : 0 } ;
4051 return { props : { favoriteList : list , totalCount } } ;
4152 } catch ( error ) {
@@ -44,42 +55,55 @@ export const getServerSideProps: GetServerSideProps = async (
4455 }
4556} ;
4657
47- const FavoritePage = ( { favoriteList, totalCount } : FavoriteProps ) => {
58+ const FavoritePage = ( {
59+ favoriteList,
60+ totalCount : initialTotalCount ,
61+ } : FavoriteProps ) => {
4862 const router = useRouter ( ) ;
49-
5063 const [ linkCardList , setLinkCardList ] =
5164 useState < FavoriteDataType [ ] > ( favoriteList ) ;
65+ const [ totalCount , setTotalCount ] = useState ( initialTotalCount ) ;
66+
67+ const loading = useFetchLinks ( setLinkCardList , setTotalCount , router . query ) ;
68+
69+ // 마이링크 페이지로 돌아감
70+ const returnButton = ( ) => {
71+ router . push ( `/link` ) ;
72+ } ;
5273
53- useFetchLinks ( setLinkCardList ) ;
5474 return (
5575 < >
56- < div className = "page-title pt-[10px] md:pt -5 pb -10 md:pb-[60px] bg-gray100 text-center" >
76+ < div className = "flex justify-center items-center sm:h-[117px] h-[219px] sm:mb -5 mb -10 bg-gray100 text-center" >
5777 < h2 className = "text-[32px] md:text-[40px] font-semibold" >
5878 ⭐️ 즐겨찾기
5979 </ h2 >
6080 </ div >
6181 < Container >
62- < CardsLayout >
63- { linkCardList . length > 0
64- ? linkCardList . map ( ( favorite ) => (
65- < LinkCard key = { favorite . id } info = { favorite } />
66- ) )
67- : null }
68- </ CardsLayout >
82+ < div className = "flex justify-end" >
83+ < button onClick = { returnButton } className = "mb-5 text-purple100" >
84+ 👈 마이링크로 돌아가기
85+ </ button >
86+ </ div >
6987
70- { /* 즐겨찾기 항목이 없을 때 보여줄 메시지 (공통 컴포넌트로 사용할 건지 논의 필요) */ }
71- { favoriteList . length === 0 && (
72- < div className = "flex flex-col justify-center items-center h-full p-10 bg-gray100 text-center text-gray600" >
73- < div className = "text-2xl md:text-3xl font-semibold text-gray600" >
74- < span className = "block mb-4" > ⭐️</ span >
75- 즐겨찾기 항목이 없습니다.
76- </ div >
77- < div className = "text-sm text-purple100 mt-2" >
78- 저장한 즐겨찾기 항목을 추가해보세요.
79- </ div >
88+ { /* 로딩 중일 때 */ }
89+ { loading ? (
90+ < div className = "text-center" >
91+ < LoadingSpinner />
8092 </ div >
93+ ) : linkCardList . length > 0 ? (
94+ < >
95+ < CardsLayout >
96+ { linkCardList . length > 0
97+ ? linkCardList . map ( ( favorite ) => (
98+ < LinkCard key = { favorite . id } info = { favorite } />
99+ ) )
100+ : null }
101+ </ CardsLayout >
102+ < Pagination totalCount = { totalCount } />
103+ </ >
104+ ) : (
105+ < EmptyFavoriteList />
81106 ) }
82- < Pagination totalCount = { totalCount } />
83107 </ Container >
84108 </ >
85109 ) ;
0 commit comments