import { View, Text, ScrollView, RefreshControl, ActivityIndicator } from 'react-native'
import React, { useState, useEffect } from 'react'
import { widthPercentageToDP as wp, heightPercentageToDP as hp } from 'react-native-responsive-screen';
import TabBar from '../themes/TabBar';
import {
ChevronLeftIcon,
ShoppingCartIcon,
TruckIcon,
CheckCircleIcon,
XCircleIcon,
TagIcon,
BellIcon
} from 'react-native-heroicons/outline';
import { useNavigation } from '@react-navigation/native';
import { useNotifications } from '../context/NotificationContext';
const NotificationScreen = () => {
const navigation = useNavigation();
const { notifications, loading, error, fetchNotifications } = useNotifications();
const [refreshing, setRefreshing] = useState(false);
const [groupedNotifications, setGroupedNotifications] = useState({ today: [], yesterday: [] });
useEffect(() => {
fetchNotifications();
}, []);
useEffect(() => {
groupNotifications();
}, [notifications]);
const groupNotifications = () => {
const today = new Date();
today.setHours(0, 0, 0, 0);
const yesterday = new Date(today);
yesterday.setDate(yesterday.getDate() - 1);
const todayNotifications = [];
const yesterdayNotifications = [];
notifications.forEach(notification => {
const notificationDate = new Date(notification.created_at);
notificationDate.setHours(0, 0, 0, 0);
if (notificationDate.getTime() === today.getTime()) {
todayNotifications.push(notification);
} else if (notificationDate.getTime() === yesterday.getTime()) {
yesterdayNotifications.push(notification);
}
});
setGroupedNotifications({
today: todayNotifications,
yesterday: yesterdayNotifications
});
};
const onRefresh = async () => {
setRefreshing(true);
try {
await fetchNotifications();
} finally {
setRefreshing(false);
}
};
const getNotificationIcon = (type) => {
switch (type) {
case 'order_placed':
return ;
case 'order_shipped':
return ;
case 'order_delivered':
return ;
case 'order_cancelled':
return ;
case 'coupon':
return ;
default:
return ;
}
};
const formatTimeAgo = (dateString) => {
const date = new Date(dateString);
const now = new Date();
const diffInHours = Math.floor((now - date) / (1000 * 60 * 60));
if (diffInHours < 1) {
return 'Just now';
} else if (diffInHours < 24) {
return `${diffInHours}h`;
} else {
const diffInDays = Math.floor(diffInHours / 24);
return `${diffInDays}d`;
}
};
const renderNotificationGroup = (notifications, title) => {
if (notifications.length === 0) return null;
return (
<>
{title}
{notifications.map((notification, index) => (
{getNotificationIcon(notification.type)}
{notification.type.split('_').map(word =>
word.charAt(0).toUpperCase() + word.slice(1)
).join(' ')}
{formatTimeAgo(notification.created_at)}
{notification.message}
))}
>
);
};
if (loading && !refreshing) {
return (
);
}
if (error) {
return (
{error}
);
}
const unreadCount = notifications.filter(n => !n.read).length;
return (
0 ? (
{unreadCount}
NEW
) : null
}
title={"Notification "}
suffix={}
titleStyle={{
color: "black",
fontWeight: "bold",
fontSize: 20
}}
prefixStyle={{
backgroundColor: "white",
borderWidth: 1,
primary: "#704f38",
backgroundColor: "#704f38",
height: 40,
width: 60,
paddingHorizontal: 10
}}
suffixStyle={{
backgroundColor: "white",
borderWidth: 1,
borderColor: "black",
height: 40,
width: 40,
}}
containerStyle=""
suffixAction={() => navigation.goBack()}
/>
}
>
{notifications.length === 0 ? (
No notifications yet
) : (
<>
{renderNotificationGroup(groupedNotifications.today, "T O D A Y")}
{renderNotificationGroup(groupedNotifications.yesterday, "Y E S T E R D A Y")}
>
)}
)
}
export default NotificationScreen