"use client"; import { unstable_noStore as noStore } from 'next/cache'; import { useEffect, useState } from 'react'; import { Eye, EyeOff, X, Check, RefreshCw } from 'lucide-react'; import { IconSpinner } from '@/app/components/ui/icons'; import { toast } from 'react-toastify'; import Swal from 'sweetalert2'; export default function AdminManageRequests() { noStore(); const [userRequests, setUserRequests] = useState([]); const [loading, setLoading] = useState(true); const [isRefreshed, setIsRefreshed] = useState(true); // Track whether the data has been refreshed // Fetch userRequest requests from the server const fetchRequests = async () => { try { setLoading(true); const response = await fetch('/api/admin/collections-requests', { method: 'GET', headers: { 'Content-Type': 'application/json', }, } ); if (!response.ok) { console.error('Error fetching userRequest requests:', response.statusText) toast.error('Error fetching userRequest requests:', { position: "top-right", closeOnClick: true, }); setLoading(false); return false; } const data = await response.json(); setUserRequests(data.collectionsReq); console.log('Collection Requests:', data.collectionsReq); } catch (error) { console.error('Error fetching userRequest requests:', error); toast.error('Error fetching userRequest requests:', { position: "top-right", closeOnClick: true, }); setLoading(false); return false; } setLoading(false); return true; }; useEffect(() => { // Fetch userRequest requests from the server fetchRequests(); }, []); // Handle reject collection request const handleReject = async (collectionId: string) => { // Show confirmation dialog Swal.fire({ title: 'Reject Request', text: "Are you sure you want to reject this collection request?", icon: 'question', showCancelButton: true, confirmButtonText: 'Yes', cancelButtonText: 'No', confirmButtonColor: '#4caf50', cancelButtonColor: '#b91c1c', }).then((result) => { if (result.isConfirmed) { // if user confirms, send request to server fetch(`/api/admin/collections-requests/reject`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ collection_id: collectionId, }), }).then(async (response) => { if (!response.ok) { console.error('Error rejecting collection request:', response.statusText); // Show error dialog Swal.fire({ title: 'Error!', text: 'Error rejecting collection request. Please try again later. (Check Console for more details)', icon: 'error', confirmButtonColor: '#4caf50', }); return; } const data = await response.json(); console.log('Collection Request Rejected:', data); // Show success dialog Swal.fire({ title: 'Success!', text: 'Collection request has been rejected successfully.', icon: 'success', confirmButtonColor: '#4caf50', }); // Remove approved request from the list setUserRequests(userRequests.filter((userRequest) => userRequest.collection_id !== collectionId)); }).catch((error) => { console.error('Error rejecting collection request:', error); // Show error dialog Swal.fire({ title: 'Error!', text: 'Error rejecting collection request. Please try again later. (Check Console for more details)', icon: 'error', confirmButtonColor: '#4caf50', }); }); } }); } // Handle approve collection request const handleApprove = async (collectionId: string, is_make_public: boolean) => { // Show confirmation dialog Swal.fire({ title: 'Approve Request', text: "Are you sure you want to approve this collection request?", icon: 'question', showCancelButton: true, confirmButtonText: 'Yes', cancelButtonText: 'No', confirmButtonColor: '#4caf50', cancelButtonColor: '#b91c1c', }).then((result) => { if (result.isConfirmed) { // if user confirms, send request to server fetch(`/api/admin/collections-requests/approve`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ collection_id: collectionId, is_make_public: is_make_public, }), }).then(async (response) => { if (!response.ok) { console.error('Error approving collection request:', response.statusText); // Show error dialog Swal.fire({ title: 'Error!', text: 'Error approving collection request. Please try again later. (Check Console for more details)', icon: 'error', confirmButtonColor: '#4caf50', }); return; } const data = await response.json(); console.log('Collection Request Approved:', data); // Show success dialog Swal.fire({ title: 'Success!', text: 'Collection request has been approved successfully.', icon: 'success', confirmButtonColor: '#4caf50', }); // Remove approved request from the list setUserRequests(userRequests.filter((userRequest) => userRequest.collection_id !== collectionId)); }).catch((error) => { console.error('Error approving collection request:', error); // Show error dialog Swal.fire({ title: 'Error!', text: 'Error approving collection request. Please try again later. (Check Console for more details)', icon: 'error', confirmButtonColor: '#4caf50', }); }); } }); } const handleRefresh = async () => { setIsRefreshed(false); const timer = setInterval(() => { // Timer to simulate a spinner while refreshing data setIsRefreshed(true); }, 1000); // Adjust the delay time as needed (e.g., 5000 milliseconds = 5 seconds) // Display a toast notification if (await fetchRequests()) { toast('Data refreshed successfully!', { type: 'success', position: 'top-right', autoClose: 3000, closeOnClick: true, pauseOnHover: true, }); } else { toast('Error refreshing data. Please try again later.', { type: 'error', position: 'top-right', autoClose: 3000, closeOnClick: true, pauseOnHover: true, }); } return () => clearInterval(timer); // Cleanup the timer on complete } return (

New Collection Requests

{/* Refresh Data button */}
{loading ? ( ) : userRequests.length === 0 ? (
No New Requests.
) : (
{userRequests.map((userRequest, index) => ( {/* Render table rows */} ))}
Display Name Description Current Visibility Requestor Name Requested Visibility Requested Actions
{userRequest.collections.display_name} {userRequest.collections.description} {userRequest.collections.is_public ? Public : Private} {userRequest.collections.users.name} {userRequest.is_make_public ? Public : Private} {new Date(userRequest.created_at).toLocaleDateString('en-GB', { year: 'numeric', month: 'short', day: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric', hour12: true })} {/* Approved or Reject Status */}
)}
); }