@@ -4,55 +4,77 @@ import CustomText from "@components/display/customText";
44import NothingHere from "@components/display/nothing" ;
55import NotificationBox from "@components/display/notification" ;
66import OnlyIconButton from "@components/inputs/onlyIconButton" ;
7+ import RadioBtn from "@components/inputs/radioBtn" ;
78import { FlashList , ListRenderItemInfo } from "@shopify/flash-list" ;
9+ import { notification } from "@utils/types" ;
810import { useRouter } from "expo-router" ;
9- import { collection , getDocs , Timestamp } from "firebase/firestore" ;
10- import { useEffect , useState } from "react" ;
11+ import { collection , onSnapshot } from "firebase/firestore" ;
12+ import React , { useEffect , useState } from "react" ;
1113import { StyleSheet , View } from "react-native" ;
1214import { useSafeAreaInsets } from "react-native-safe-area-context" ;
1315
14- type notification = {
15- id : string ;
16- message : string ;
17- title : string ;
18- data : { url : string } ;
19- createdAt : Timestamp ;
20- }
2116
2217export default function NotificationScreen ( ) {
2318 const insets = useSafeAreaInsets ( ) ;
2419 const router = useRouter ( ) ;
25- const [ notif , setNotif ] = useState < notification [ ] > ( [ ] ) ;
20+ const [ readNotif , setReadNotif ] = useState < notification [ ] > ( [ ] ) ;
21+ const [ unreadNotif , setUnreadNotif ] = useState < notification [ ] > ( [ ] ) ;
22+ const [ currTab , setCurrTab ] = useState < string > ( "Unread" ) ;
2623 const currUser = auth . currentUser ;
2724
28- async function loadNotif ( ) {
29- const snap = await getDocs ( collection ( db , "notifications" , currUser ? currUser . uid : "" , "unread" ) ) ;
3025
31- const data = snap . docs . map ( doc => ( {
32- id : doc . id ,
33- ...( doc . data ( ) ) as Omit < notification , "id" >
34- } ) ) ;
26+ useEffect (
27+ ( ) => {
28+ const readNotifSub = onSnapshot ( collection ( db , "notifications" , currUser ? currUser . uid : "" , "read" ) , ( snap ) => {
29+ const data : notification [ ] = snap . docs . map ( doc => (
30+ {
31+ id : doc . id ,
32+ ...( doc . data ( ) as Omit < notification , 'id' > )
33+ }
34+ ) ) ;
35+ console . log ( data ) ;
36+ setReadNotif ( data ) ;
37+ } ) ;
38+
39+
40+ const unreadNotifSub = onSnapshot ( collection ( db , "notifications" , currUser ? currUser . uid : "" , "unread" ) , ( snap ) => {
41+ const data : notification [ ] = snap . docs . map ( doc => (
42+ {
43+ id : doc . id ,
44+ ...( doc . data ( ) as Omit < notification , 'id' > )
45+ }
46+ ) ) ;
47+
48+ setUnreadNotif ( data ) ;
49+
50+ } ) ;
51+ } , [ ]
52+ )
3553
36- setNotif ( data ) ;
37- // console.log(data);
38- }
3954
40- useEffect ( ( ) => {
41- loadNotif ( ) ;
42- } , [ ] )
4355 return (
4456 < View style = { { backgroundColor : "#17171d" , flex : 1 , paddingTop : insets . top , paddingBottom : 100 , alignItems : "center" } } >
45- < View style = { { flexDirection : "row" , alignItems : "center" , width : "100%" , marginBottom : 20 } } >
57+ < View style = { { flexDirection : "row" , alignItems : "center" , width : "100%" , marginBottom : 10 } } >
4658 < OnlyIconButton icon = "arrow-left" func = { ( ) => { router . back ( ) } } style = { { top : 0 , left : 20 , zIndex : 5 } } />
4759 < CustomText style = { { color : "white" , left : 50 , fontSize : 18 , top : 0 , fontWeight : 700 } } > Notifications</ CustomText >
4860 </ View >
61+ < RadioBtn
62+ options = { [ "Unread" , "Read" ] }
63+ iconList = { [ "email-mark-as-unread" , "sticker-check" ] }
64+ selected = { currTab }
65+ setSelected = { setCurrTab }
66+ style = { { width : "100%" , alignItems : "center" , justifyContent : "center" } }
67+ />
4968 < View style = { styles . listContainer } >
69+
5070 < FlashList
51- data = { notif }
71+ data = { ( currTab == "Unread" ) ? unreadNotif : readNotif }
5272 keyExtractor = { item => item . id }
5373 ListEmptyComponent = { < NothingHere /> }
5474 renderItem = { ( { item } : ListRenderItemInfo < notification > ) => (
5575 < NotificationBox
76+ status = { currTab == "Unread" ? "unread" : "read" }
77+ id = { item . id }
5678 title = { item . title }
5779 message = { item . message }
5880 data = { item . data }
@@ -71,6 +93,9 @@ const styles = StyleSheet.create({
7193 listContainer : {
7294 flex : 1 ,
7395 width : "95%" ,
96+ borderTopWidth : 1 ,
97+ borderTopColor : "#5f6977ff" ,
98+ paddingTop : 10
7499 }
75100
76101} ) ;
0 commit comments