"use client"; import { useState, useEffect } from 'react'; import { List, Table, Trash, Eye, EyeOff, RefreshCw } from 'lucide-react'; import Swal from 'sweetalert2'; import { toast } from 'react-toastify'; import { IconSpinner } from '@/app/components/ui/icons'; import { useSession } from 'next-auth/react'; export default function QueryCollectionManage() { const [userCollections, setUserCollections] = useState([]); const [tableView, setTableView] = useState(false); // Track whether to show the table view const [isRefreshed, setIsRefreshed] = useState(true); // Track whether the data has been refreshed const [isLoading, setIsLoading] = useState(true); // Track whether the data is loading const { data: session, status } = useSession(); const supabaseAccessToken = session?.supabaseAccessToken; // Retrieve the user's collections and collections requests data from the database const getUserCollectionsandRequests = async () => { setIsLoading(true); // Fetch the user's public collection requests from the API fetch('/api/user/collections-requests' , { method: 'GET', headers: { 'Content-Type': 'application/json', }, } ) .then((response) => response.json()) .then((data) => { const userCollectionsRequests = data.userCollectionsReq; console.log('User Collections Requests:', userCollectionsRequests); // Sort the collections by created date in descending order (oldest first) userCollectionsRequests.sort((a: any, b: any) => new Date(a.created_at).getTime() - new Date(b.created_at).getTime()); // Extract the collection data from the collections requests const updatedCollections = userCollectionsRequests.map((collection: any) => { // Check if the collection has any collection requests if (collection.collections_requests.length === 0) { // If not, return the collection data with no other details return { collection_id: collection.collection_id, display_name: collection.display_name, description: collection.description, isPublic: collection.is_public, // Convert the date to a readable format in the user's locale and timezone e.g. "2022-01-01T12:00:00" => "1 Jan, 2022, 12:00:00 PM" created_at: new Date(collection.created_at).toLocaleDateString('en-GB', { year: 'numeric', month: 'short', day: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric', hour12: true }), }; } else { // If the collection has collection requests, return the collection data with the request status and dates return { collection_id: collection.collection_id, display_name: collection.display_name, description: collection.description, created_at: new Date(collection.created_at).toLocaleDateString('en-GB', { year: 'numeric', month: 'short', day: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric', hour12: true }), isPublic: collection.is_public, requestType: collection.collections_requests[0].is_make_public ? 'Public' : 'Private', requestStatus: collection.collections_requests[0].is_pending ? '⏳Pending' : collection.collections_requests[0].is_approved ? '✅Approved' : '❌Rejected', requestDate: new Date(collection.collections_requests[0].created_at).toLocaleDateString('en-GB', { year: 'numeric', month: 'short', day: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric', hour12: true }), updatedRequestDate: new Date(collection.collections_requests[0].updated_at).toLocaleDateString('en-GB', { year: 'numeric', month: 'short', day: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric', hour12: true }), }; } }); // Update the userCollections state with the fetched data setUserCollections(updatedCollections); setIsLoading(false); }) .catch((error) => { console.error("Error fetching user collection requests:", error); setIsLoading(false); return false; }); return true; } // Fetch the user's collections and collections requests data from the database on component mount useEffect(() => { getUserCollectionsandRequests(); }, []); // Function to toggle between list and table views const toggleView = () => { setTableView((prevValue) => !prevValue); }; // Function to handle requesting to be public or private const handleRequest = (collectionId: string, isPublic: boolean) => { // Implement request logic here console.log(`Requesting to set ${isPublic ? 'be Public' : 'be Private'} for collection with ID: ${collectionId}`); // Display a confirmation dialog Swal.fire({ title: 'Request Confirmation', text: `Are you sure you want to request to ${isPublic ? 'make this collection Public (available to other users in "Chat/Search")' : 'make this collection Private (remove from "Chat/Search")'}?`, icon: 'question', showCancelButton: true, confirmButtonText: 'Yes', cancelButtonText: 'No', confirmButtonColor: '#4caf50', cancelButtonColor: '#b91c1c', }).then((result) => { if (result.isConfirmed) { // Check if there is an existing request for the collection const existingRequest = userCollections.find((collection) => collection.collection_id === collectionId)?.requestStatus; const existingRequestType = userCollections.find((collection) => collection.collection_id === collectionId)?.requestType; // If an existing request is found, Make a POST request to the API & display a confirmation dialog if (existingRequest) { // Display a confirmation dialog if there is an existing request Swal.fire({ title: 'Existing Request Found', text: `Existing Request with Status: ${existingRequest} for Type: ${existingRequestType}. Do you want to proceed with this new request?`, icon: 'question', showCancelButton: true, confirmButtonText: 'Yes', cancelButtonText: 'No', confirmButtonColor: '#4caf50', cancelButtonColor: '#b91c1c', }).then((result) => { if (result.isConfirmed) { // If confirm, make a put request to the API & display a success/error message fetch('/api/user/collections-requests', { method: 'PUT', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ collection_id: collectionId, is_make_public: isPublic }), } ) .then(async response => { if (response.ok) { // Show success dialog console.log('Request sent successfully:', response.statusText); Swal.fire({ title: 'Request Sent', text: `Your request to ${isPublic ? 'make this collection Public' : 'make this collection Private'} has been sent successfully.`, icon: 'success', confirmButtonText: 'OK', confirmButtonColor: '#4caf50', }); // Refresh the userCollections state after the request is sent getUserCollectionsandRequests(); } else { const data = await response.json(); // Log to console console.error('Error sending request:', data.error); // Show error dialog Swal.fire({ title: 'Request Failed', text: 'An error occurred while sending the request. Please try again later. (Check Console for more details)', icon: 'error', confirmButtonText: 'OK', confirmButtonColor: '#4caf50', }); } }) .catch(error => { console.error('Error sending request:', error); // Show error dialog Swal.fire({ title: 'Error!', text: 'Failed to send request. Please try again later. (Check Console for more details)', icon: 'error', confirmButtonColor: '#4caf50', }); }); } }); } // If there is no existing request, make a POST request to the API & display a success/error message else { fetch('/api/user/collections-requests', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ collection_id: collectionId, is_make_public: isPublic }), } ) .then(async response => { if (response.ok) { // Show success dialog console.log('Request sent successfully:', response.statusText); Swal.fire({ title: 'Request Sent', text: `Your request to ${isPublic ? 'make this collection Public' : 'make this collection Private'} has been sent successfully.`, icon: 'success', confirmButtonText: 'OK', confirmButtonColor: '#4caf50', }); // Refresh the userCollections state after the request is sent getUserCollectionsandRequests(); } else { const data = await response.json(); // Log to console console.error('Error sending request:', data.error); // Show error dialog Swal.fire({ title: 'Request Failed', text: 'An error occurred while sending the request. Please try again later. (Check Console for more details)', icon: 'error', confirmButtonText: 'OK', confirmButtonColor: '#4caf50', }); } }) .catch(error => { console.error('Error sending request:', error); // Show error dialog Swal.fire({ title: 'Error!', text: 'Failed to send request. Please try again later. (Check Console for more details)', icon: 'error', confirmButtonColor: '#4caf50', }); }); } } }); }; // Function to handle cancelling a request const handleCancelRequest = (collectionId: string, isPublic: boolean) => { // Implement cancel request logic here console.log(`Cancelling request for collection with ID: ${collectionId}`); // Display a confirmation dialog Swal.fire({ title: 'Cancel Request Confirmation', text: `Are you sure you want to cancel the request to ${isPublic ? 'make this collection Private' : 'make this collection Public'}?`, icon: 'question', showCancelButton: true, confirmButtonText: 'Yes', cancelButtonText: 'No', confirmButtonColor: '#4caf50', cancelButtonColor: '#b91c1c', }).then((result) => { if (result.isConfirmed) { // Make a delete request to the API & display a success/error message fetch('/api/user/collections-requests', { method: 'DELETE', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ collection_id: collectionId }), } ) .then(async response => { if (response.ok) { // Show success dialog console.log('Request cancelled successfully:', response.statusText); Swal.fire({ title: 'Request Cancelled', text: `Your request to ${isPublic ? 'make this collection Private' : 'make this collection Public'} has been cancelled successfully.`, icon: 'success', confirmButtonText: 'OK', confirmButtonColor: '#4caf50', }); // Refresh the userCollections state after the request is sent getUserCollectionsandRequests(); } else { const data = await response.json(); // Log to console console.error('Error cancelling request:', data.error); // Show error dialog Swal.fire({ title: 'Request Cancellation Failed', text: 'An error occurred while cancelling the request. Please try again later. (Check Console for more details)', icon: 'error', confirmButtonText: 'OK', confirmButtonColor: '#4caf50', }); } }) .catch(error => { console.error('Error cancelling request:', error); // Show error dialog Swal.fire({ title: 'Error!', text: 'Failed to upload and index document set. Please try again later. (Check Console for more details)', icon: 'error', confirmButtonColor: '#4caf50', }); }); } }); }; // Function to handle deleting a collection const handleDelete = (collectionId: string, isPublic: boolean) => { // Implement delete logic here console.log(`Deleting collection with ID: ${collectionId}`); // Display a confirmation dialog Swal.fire({ title: 'Delete Confirmation', text: `Are you sure you want to delete this collection? This action cannot be undone!`, icon: 'warning', showCancelButton: true, confirmButtonText: 'Yes', cancelButtonText: 'No', confirmButtonColor: '#4caf50', cancelButtonColor: '#b91c1c', }).then((result) => { if (result.isConfirmed) { // If the user confirms the delete, make a delete request to the API, display a success/error message fetch('/api/user/collections', { method: 'DELETE', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${supabaseAccessToken}`, // Add the access token to the request headers }, body: JSON.stringify({ collection_id: collectionId }), } ) .then(async response => { if (response.ok) { // If the delete request is successful, display a success message console.log('Collection deleted successfully:', response.statusText); Swal.fire({ title: 'Collection Deleted!', text: 'Your collection has been deleted successfully.', icon: 'success', confirmButtonColor: '#4caf50', }); // Refresh the userCollections state after the request is sent getUserCollectionsandRequests(); } else { const data = await response.json(); // Log to console console.error('Error deleting collection:', data.error); // Show error dialog Swal.fire({ title: 'Error!', text: 'Failed to delete collection. Please try again later. (Check Console for more details)', icon: 'error', confirmButtonColor: '#4caf50', }); } }) .catch(error => { console.error('Error uploading and indexing document set:', error); // Show error dialog Swal.fire({ title: 'Error!', text: 'Failed to delete collection. 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 getUserCollectionsandRequests()) { 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 (

Manage Your Collections:

{/* Refresh Data button */} {/* Button to toggle between list and table views */}
) : (
    {userCollections.map((collection, index) => (
  • {/* Render list items */}
    {collection.display_name}
    {collection.description}
    {/* Divider */}
    Created: {collection.created_at}
    Current Visibility: {collection.isPublic ? Public : Private}
    {/* Render request status and dates */} {collection.requestStatus && (
    Request Type: {collection.requestType}
    Request Status: {collection.requestStatus}
    Requested: {collection.requestDate}
    Request Updated: {collection.updatedRequestDate}
    )}
    {/* Manage section */}
    {/* Conditional rendering button based on request status */} {collection.requestStatus === '⏳Pending' ? ( ) : collection.isPublic ? ( ) : ( )} {/* Conditional rendering of delete button based on current visibility of collection, only private collection can be deleted */} {!collection.isPublic && ( )}
  • ))}
)}
); }