@@ -7,84 +7,61 @@ import AlarmCategoryItem from "@/components/home/AlarmCategoryItem";
7
7
import { useNavigation } from "expo-router" ;
8
8
import { Color , Font } from "@/constants/Theme" ;
9
9
import { NativeStackNavigationProp } from "react-native-screens/lib/typescript/native-stack/types" ;
10
-
11
- export default function AlarmPage ( { } : { } ) {
10
+ import { getKeywords } from "@/service/keyword/getKeywords" ;
11
+ export type NoticeItem = {
12
+ id : number ;
13
+ title : string ;
14
+ noticeTypeKorean : string ;
15
+ noticeTypeEnglish : string ;
16
+ pubDate : string ;
17
+ viewed : boolean ;
18
+ url : string ;
19
+ marked : boolean ;
20
+ } ;
21
+ export default function AlarmPage ( ) {
12
22
const navigation = useNavigation ( ) ;
13
23
14
- const [ filteredNotices , setFilteredNotices ] = useState < TCategoryList [ ] > ( [ ] ) ; // 필터링된 공지사항
15
- const [ hasUnreadNotices , setHasUnreadNotices ] = useState < boolean > ( false ) ; // 읽지 않은 공지가 있는지
16
- const [ unreadCount , setUnreadCount ] = useState < number > ( 0 ) ; // 읽지 않은 공지 개수
24
+ const [ filteredNotices , setFilteredNotices ] = useState < NoticeItem [ ] > ( [ ] ) ; // 필터링된 공지사항
17
25
const [ categoryName , setCategoryName ] = useState < string > ( "" ) ; // 카테고리 이름
26
+ const [ unreadKeywords , setUnreadKeywords ] = useState < {
27
+ [ key : string ] : boolean ;
28
+ } > ( { } ) ;
18
29
const [ keywords , setKeywords ] = useState < string [ ] > ( [ ] ) ; // 키워드 상태 관리
19
30
31
+ useEffect ( ( ) => {
32
+ loadKeywords ( ) ;
33
+ } , [ ] ) ;
34
+
20
35
const loadKeywords = async ( ) => {
21
36
try {
22
- const storedKeywords = await AsyncStorage . getItem ( "keywords" ) ;
23
- const parsedKeywords = storedKeywords ? JSON . parse ( storedKeywords ) : [ ] ;
24
- setKeywords ( parsedKeywords ) ;
37
+ const keywordList = await getKeywords ( ) ; // API로부터 키워드 목록 불러오기
38
+ setKeywords ( keywordList ) ; // 키워드 상태에 저장
25
39
} catch ( error ) {
26
40
console . error ( "키워드 불러오기 오류:" , error ) ;
27
41
}
28
42
} ;
29
43
30
- useEffect ( ( ) => {
31
- loadKeywords ( ) ;
32
- } , [ ] ) ;
33
-
34
- const getViewedNotices = async ( category : string ) => {
35
- const storageKey = `${ category } _viewed` ;
36
- const existingNotices = await AsyncStorage . getItem ( storageKey ) ;
37
- console . log ( `불러온 읽음 상태: ${ existingNotices } ` ) ; // 로그 추가
38
-
39
- return existingNotices ? JSON . parse ( existingNotices ) : [ ] ;
40
- } ;
41
-
42
- const checkUnreadNotices = ( notices : TCategoryList [ ] , category : string ) => {
43
- const unreadNotices = notices . filter ( ( notice ) => ! notice . read ) ; // read: false인 공지 필터링
44
- setUnreadCount ( unreadNotices . length ) ; // 읽지 않은 공지 개수 설정
45
- const hasUnread = unreadNotices . length > 0 ;
46
-
47
- setHasUnreadNotices ( hasUnread ) ;
48
- return hasUnread ; // 읽지 않은 공지가 있는지 여부 반환
44
+ const updateUnreadStatus = ( newUnreadStatus : { [ key : string ] : boolean } ) => {
45
+ setUnreadKeywords ( newUnreadStatus ) ; // 키워드별로 읽지 않은 상태 업데이트
49
46
} ;
50
-
51
- const handleFilterNotices = async (
52
- notices : TCategoryList [ ] ,
53
- category : string
54
- ) => {
55
- setCategoryName ( category ) ; // 카테고리 이름 저장
56
- const viewedNotices = await getViewedNotices ( category ) ;
57
- // 각 공지의 읽음 상태 적용
58
- const updatedNotices = notices . map ( ( notice ) => ( {
59
- ...notice ,
60
- read : viewedNotices . includes ( notice . id ) ,
61
- } ) ) ;
62
-
63
- setFilteredNotices ( updatedNotices ) ; // 필터링된 공지사항 업데이트
64
- checkUnreadNotices ( updatedNotices , category ) ; // 읽지 않은 공지 여부 체크
47
+ const handleFilterNotices = ( notices : NoticeItem [ ] ) => {
48
+ setFilteredNotices ( notices ) ;
65
49
} ;
66
50
67
51
const handleUpdateList = ( id : number ) => {
68
- setFilteredNotices ( ( prev ) =>
69
- prev . map ( ( notice ) =>
70
- notice . id === id ? { ...notice , read : true } : notice
71
- )
52
+ const updatedNotices = filteredNotices . map ( ( notice ) =>
53
+ notice . id === id ? { ...notice , viewed : true } : notice
72
54
) ;
73
-
74
- const updateOriginalNotices = async ( ) => {
75
- const key = `${ categoryName } _viewed` ; // 카테고리 이름 기반으로 읽음 상태 저장
76
- const existingNotices = await AsyncStorage . getItem ( key ) ;
77
- let parsedNotices = existingNotices ? JSON . parse ( existingNotices ) : [ ] ;
78
-
79
- if ( ! parsedNotices . includes ( id ) ) {
80
- parsedNotices . push ( id ) ;
81
- }
82
-
83
- await AsyncStorage . setItem ( key , JSON . stringify ( parsedNotices ) ) ;
84
- checkUnreadNotices ( filteredNotices , categoryName ) ;
85
- } ;
86
-
87
- updateOriginalNotices ( ) ;
55
+ setFilteredNotices ( updatedNotices ) ;
56
+
57
+ // 모든 공지가 읽혔는지 확인
58
+ const allRead = updatedNotices . every ( ( notice ) => notice . viewed ) ;
59
+ if ( allRead ) {
60
+ // 읽지 않은 공지가 없으면 해당 키워드의 빨간 점 제거
61
+ const updatedUnreadStatus = { ...unreadKeywords } ;
62
+ updatedUnreadStatus [ "keyword" ] = false ; // 현재 카테고리 키워드에 대해 빨간 점 제거
63
+ setUnreadKeywords ( updatedUnreadStatus ) ;
64
+ }
88
65
} ;
89
66
90
67
// 모든 카테고리에서 읽지 않은 공지를 확인하는 함수
@@ -94,6 +71,7 @@ export default function AlarmPage({}: {}) {
94
71
< AlarmHeader
95
72
onFilterNotices = { handleFilterNotices } // 필터링된 공지사항을 헤더에서 전달
96
73
navigation = { navigation }
74
+ updateUnreadStatus = { updateUnreadStatus }
97
75
/>
98
76
{ keywords . length === 0 ? (
99
77
< View
@@ -151,18 +129,18 @@ export default function AlarmPage({}: {}) {
151
129
</ View >
152
130
) : (
153
131
< FlatList
154
- style = { {
155
- marginBottom : 65 ,
156
- } }
157
- data = { filteredNotices }
158
- keyExtractor = { ( item ) => item . id . toString ( ) }
132
+ style = { { marginBottom : 65 } }
133
+ data = { filteredNotices } // 필터링된 공지사항 데이터
134
+ keyExtractor = { ( item ) => item . id . toString ( ) } // 각 항목의 고유 키 설정
159
135
renderItem = { ( { item } ) => (
160
136
< AlarmCategoryItem
161
- item = { item }
137
+ item = { item } // 각각의 공지사항 항목 전달
162
138
categoryKey = "keyword"
163
- updateList = { handleUpdateList }
139
+ updateList = { handleUpdateList } // 업데이트 함수 전달
164
140
/>
165
141
) }
142
+ showsVerticalScrollIndicator = { false } // 스크롤바 숨김 (선택사항)
143
+ contentContainerStyle = { { paddingBottom : 20 } } // 추가 패딩으로 마지막 항목 간격 유지
166
144
/>
167
145
) }
168
146
</ View >
0 commit comments