"use client";
import { formatDateMonthYear, t } from "@/utils";
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table";
import { useSelector } from "react-redux";
import { useEffect, useState } from "react";
import { getNotificationList } from "@/utils/api";
import { CurrentLanguageData } from "@/redux/reducer/languageSlice";
import Pagination from "@/components/Common/Pagination";
import NoData from "@/components/EmptyStates/NoData";
import NotificationSkeleton from "./NotificationSkeleton";
import CustomImage from "@/components/Common/CustomImage";
import { userSignUpData } from "@/redux/reducer/authSlice";
import { useNavigate } from "@/components/Common/useNavigate";
import { cn } from "@/lib/utils";
const Notifications = () => {
const CurrentLanguage = useSelector(CurrentLanguageData);
const [notifications, setNotifications] = useState([]);
const [currentPage, setCurrentPage] = useState(1);
const [totalPages, setTotalPages] = useState(0);
const [isLoading, setIsLoading] = useState(false);
const userData = useSelector(userSignUpData);
const { navigate } = useNavigate();
const fetchNotificationData = async (page) => {
try {
setIsLoading(true);
const response = await getNotificationList.getNotification({ page });
if (response?.data?.error === false) {
setNotifications(response?.data.data.data);
setTotalPages(response?.data?.data?.last_page);
} else {
toast.error(data.message);
}
} catch (error) {
console.error("Error:", error);
} finally {
setIsLoading(false);
}
};
useEffect(() => {
fetchNotificationData(currentPage);
}, [currentPage]);
const handlePageChange = (page) => {
setCurrentPage(page);
};
const handleNotificationClick = (notification) => {
// Check if notification has item slug
if (notification?.item?.slug) {
const currentUserId = userData?.id; // Get current user ID
const notificationUserId = notification?.item?.user_id; // Get notification user ID
if (currentUserId == notificationUserId) {
// If current user is the same as notification user, redirect to my-listing
navigate(`/my-listing/${notification.item.slug}`);
} else {
// Otherwise, redirect to ad-details
navigate(`/ad-details/${notification.item.slug}`);
}
}
};
return isLoading ? (