33import React , { useState , useEffect } from "react" ;
44import { Modal , ModalBody , ModalContent , ModalFooter , ModalHeader , useDisclosure , Button } from "@heroui/react" ;
55import { useRouter } from "next/navigation" ;
6+ import { addToast } from "@heroui/toast" ;
67
7- import JobApplicationsDashboard , { Application } from "@/components/JobApplicationsDashboard " ;
8+ import JobApplicationsDashboard , { Application } from "@/components/JobApplicationsDashboardPreview " ;
89import { mockData } from "@/utils/mockData" ;
910
1011export default function PreviewDashboard ( ) {
1112 const { isOpen, onOpen, onClose } = useDisclosure ( ) ;
1213 const [ data , setData ] = useState < Application [ ] > ( [ ] ) ;
1314 const [ loading , setLoading ] = useState ( true ) ;
1415 const [ downloading , setDownloading ] = useState ( false ) ;
15- const router = useRouter ( ) ;
1616
1717 const [ currentPage , setCurrentPage ] = useState ( 1 ) ;
1818 const [ totalPages , setTotalPages ] = useState ( 1 ) ;
19+
20+ const router = useRouter ( ) ;
21+ const apiUrl = process . env . NEXT_PUBLIC_API_URL || "http://localhost:8000" ;
22+
1923 useEffect ( ( ) => {
2024 setLoading ( true ) ;
2125 const dataTimeout = setTimeout ( ( ) => {
@@ -113,6 +117,35 @@ export default function PreviewDashboard() {
113117 </ Modal >
114118 ) ;
115119
120+ const handleRemoveItem = async ( id : string ) => {
121+ try {
122+ // Make a DELETE request to the backend
123+ const response = await fetch ( `${ apiUrl } /delete-email/${ id } ` , {
124+ method : "DELETE" ,
125+ credentials : "include" // Include cookies for authentication
126+ } ) ;
127+
128+ if ( ! response . ok ) {
129+ throw new Error ( "Failed to delete the item" ) ;
130+ }
131+
132+ // If the deletion is successful, update the local state
133+ setData ( ( prevData ) => prevData . filter ( ( item ) => item . id !== id ) ) ;
134+
135+ addToast ( {
136+ title : "Item removed successfully" ,
137+ color : "success"
138+ } ) ;
139+ } catch ( error ) {
140+ console . error ( "Error deleting item:" , error ) ;
141+ addToast ( {
142+ title : "Failed to remove item" ,
143+ description : "Please try again or contact support." ,
144+ color : "danger"
145+ } ) ;
146+ }
147+ } ;
148+
116149 return (
117150 < JobApplicationsDashboard
118151 currentPage = { currentPage }
@@ -126,6 +159,7 @@ export default function PreviewDashboard() {
126159 onDownloadSankey = { downloadSankey }
127160 onNextPage = { nextPage }
128161 onPrevPage = { prevPage }
162+ onRemoveItem = { handleRemoveItem }
129163 />
130164 ) ;
131165}
0 commit comments