1
1
'use client' ;
2
2
3
- import { useState } from 'react' ;
3
+ import React , { useState } from 'react' ;
4
4
import { useQuery } from '@tanstack/react-query' ;
5
5
import Image from 'next/image' ;
6
6
import PopularContents from '@/components/learning/PopularContents' ;
@@ -12,21 +12,24 @@ import ArrowIcon from '../../../public/images/learning/bottom_arrow_button.svg';
12
12
import BackgroundImage from '../../../public/images/learning/learning_background.png' ;
13
13
import SubscriptionBanner from './SubscriptionBanner' ;
14
14
import { dropdownOptions } from '@/utils/dropdownOptions' ;
15
+ import { OverviewResponse , Content } from '@/types/learning' ;
16
+ import { FilteredContent , FilteredResponse } from '@/types/filterArticle' ;
15
17
16
- const LearningPageClient = ( { initialData } : { initialData : any } ) => {
18
+ const LearningPageClient = ( { initialData } : { initialData : OverviewResponse } ) => {
17
19
const [ selectedType , setSelectedType ] = useState < string > ( '' ) ;
18
20
const [ selectedCategory , setSelectedCategory ] = useState < string > ( '' ) ;
19
21
const [ activeDropdown , setActiveDropdown ] = useState < string | null > ( null ) ;
20
22
const [ page , setPage ] = useState < number > ( 1 ) ;
21
23
22
- const fetchContentsWithPage = async ( ) => {
24
+ const fetchContentsWithPage = async ( ) : Promise < FilteredResponse > => {
23
25
const type = selectedType || 'all' ;
24
26
const category = selectedCategory || 'all' ;
25
27
const endpoint = `http://43.200.90.72/contents/list?type=${ type } &category=${ category } &page=${ page } ` ;
26
28
const response = await fetch ( endpoint ) ;
27
29
if ( ! response . ok ) throw new Error ( 'API 호출 실패' ) ;
28
30
return response . json ( ) ;
29
31
} ;
32
+
30
33
31
34
const { data, isLoading } = useQuery ( {
32
35
queryKey : [ 'contents' , selectedType , selectedCategory , page ] ,
@@ -57,6 +60,14 @@ const LearningPageClient = ({ initialData }: { initialData: any }) => {
57
60
setActiveDropdown ( null ) ;
58
61
} ;
59
62
63
+ const extractContents = ( contents : Content [ ] | undefined ) =>
64
+ Array . isArray ( contents )
65
+ ? contents . map ( ( item ) => ( {
66
+ ...item . article ,
67
+ likedByMe : item . likedByMe ,
68
+ } ) )
69
+ : [ ] ;
70
+
60
71
return (
61
72
< div >
62
73
< div className = "relative w-full h-[300px] lg:h-[400px]" >
@@ -67,8 +78,8 @@ const LearningPageClient = ({ initialData }: { initialData: any }) => {
67
78
objectFit = "cover"
68
79
className = "w-full"
69
80
/>
70
- < div className = "absolute bottom-0 left-0 right-0 flex justify-end items-center border-b shadow-sm z-50 bg-[#fffffc]/50" >
71
- < button
81
+ < div className = "absolute bottom-0 left-0 right-0 flex justify-end items-center border-b shadow-sm z-50 bg-[#fffffc]/50" >
82
+ < button
72
83
onClick = { resetFilters }
73
84
className = "flex items-center gap-2 px-6 py-4 text-lg font-semibold text-gray-800"
74
85
>
@@ -154,22 +165,22 @@ const LearningPageClient = ({ initialData }: { initialData: any }) => {
154
165
< div className = "max-w-[360px] mx-auto desk:max-w-[1000px] w-full sm:w-[90%] lg:w-[100%] mt-8" >
155
166
{ ! selectedType && ! selectedCategory ? (
156
167
< >
157
- < PopularContents contents = { initialData . popularContents } />
158
- < RecentContents contents = { initialData . recentContents } />
168
+ < PopularContents contents = { extractContents ( initialData . popularContents ) } />
169
+ < RecentContents contents = { extractContents ( initialData . recentContents ) } />
159
170
< div className = "hidden sm:flex w-full" >
160
- < SubscriptionBanner />
171
+ < SubscriptionBanner />
161
172
</ div >
162
- < LatestNewsClipping contents = { initialData . latestNewsClipping } />
173
+ < LatestNewsClipping contents = { extractContents ( initialData . newsContents ) } />
163
174
</ >
164
175
) : isLoading ? (
165
176
null
166
177
) : (
167
178
< FilteredContents
168
- contents = { data ?. content || [ ] }
169
- total = { data ?. total || 0 }
170
- page = { page }
171
- size = { data ?. size || 5 }
172
- onPageChange = { ( newPage ) => setPage ( newPage ) }
179
+ contents = { data ?. content || [ ] }
180
+ total = { data ?. total || 0 }
181
+ page = { page }
182
+ size = { data ?. size || 5 }
183
+ onPageChange = { ( newPage ) => setPage ( newPage ) }
173
184
/>
174
185
) }
175
186
</ div >
0 commit comments