@@ -350,6 +350,33 @@ export const ProgramSummary = ({
350350
351351 const { nextSession, nextRoom } = safeBookingInfo ;
352352
353+ const [ invoiceId , setInvoiceId ] = useState ( null ) ;
354+ const [ isLoadingInvoiceId , setIsLoadingInvoiceId ] = useState ( true ) ;
355+ const { id } = useParams ( ) ;
356+
357+ useEffect ( ( ) => {
358+ const fetchData = async ( ) => {
359+ try {
360+ const invoiceResponse = await backend . get ( `/invoices/event/${ id } ` )
361+ // Get the invoice with the most recent start_date
362+ if ( invoiceResponse . data && invoiceResponse . data . length > 0 ) {
363+ const mostRecentInvoice = invoiceResponse . data . reduce ( ( latest , current ) => {
364+ const latestDate = new Date ( latest . startDate ) ;
365+ const currentDate = new Date ( current . startDate ) ;
366+ return currentDate > latestDate ? current : latest ;
367+ } ) ;
368+
369+ setInvoiceId ( mostRecentInvoice . id ) ;
370+ }
371+ } catch ( err ) {
372+ console . error ( "Error fetching bookings:" , err ) ;
373+ } finally {
374+ setIsLoadingInvoiceId ( false ) ;
375+ }
376+ } ;
377+ fetchData ( ) ;
378+ } , [ id ] ) ;
379+
353380 return (
354381 < Box
355382 minH = "10vh"
@@ -400,12 +427,10 @@ export const ProgramSummary = ({
400427 align = "center"
401428 gap = { 2 }
402429 >
403- < PDFButton
404- leftIcon = { < Icon as = { DownloadIcon } /> }
405- fontWeight = { "700" }
406- >
407- Invoice
408- </ PDFButton >
430+ < PDFButtonInvoice
431+ id = { invoiceId }
432+ onlyIcon = { true }
433+ />
409434 { ! isArchived ? (
410435 < EditCancelPopup
411436 handleEdit = { handleEdit }
@@ -2246,40 +2271,43 @@ const MyDocument = ({ bookingData }) => {
22462271 ) ;
22472272} ;
22482273
2249- const PDFButton = ( ) => {
2250- const { backend } = useBackendContext ( ) ;
2251- // const [bookingData, setBookingData] = useState(null);
2252- const [ isLoading , setIsLoading ] = useState ( true ) ;
2253- const { id } = useParams ( ) ;
2254-
2255- useEffect ( ( ) => {
2256- const fetchData = async ( ) => {
2257- try {
2258- // const response = await backend.get("/bookings");
2259- // setBookingData(Array.isArray(response.data) ? response.data : []);
2260-
2261- // console.log("id", id)
2262- const eventsResponse = await backend . get ( `/events/${ id } ` )
2263- console . log ( "eventsResponse" , eventsResponse ) ;
2264- } catch ( err ) {
2265- console . error ( "Error fetching bookings:" , err ) ;
2266- } finally {
2267- setIsLoading ( false ) ;
2268- }
2269- } ;
2270- fetchData ( ) ;
2271- } , [ backend ] ) ;
2272-
2273- // if (isLoading) return <div>Loading...</div>;
2274-
2275-
2276-
2277- return (
2278- < div >
2279- { /* <PDFButtonInvoice
2280- id={id}
2281- onlyIcon={true}
2282- /> */ }
2283- </ div >
2284- ) ;
2285- } ;
2274+ // const PDFButton = () => {
2275+ // const { backend } = useBackendContext();
2276+ // const [invoiceId, setInvoiceId] = useState(null);
2277+ // // const [isLoading, setIsLoading] = useState(true);
2278+ // const { id } = useParams();
2279+
2280+ // useEffect(() => {
2281+ // const fetchData = async () => {
2282+ // try {
2283+ // const invoiceResponse = await backend.get(`/invoices/event/${id}`)
2284+
2285+ // // Get the invoice with the most recent start_date
2286+ // if (invoiceResponse.data && invoiceResponse.data.length > 0) {
2287+ // const mostRecentInvoice = invoiceResponse.data.reduce((latest, current) => {
2288+ // const latestDate = new Date(latest.start_date);
2289+ // const currentDate = new Date(current.start_date);
2290+ // return currentDate > latestDate ? current : latest;
2291+ // });
2292+
2293+ // setInvoiceId(mostRecentInvoice.id);
2294+ // }
2295+ // } catch (err) {
2296+ // console.error("Error fetching bookings:", err);
2297+ // }
2298+ // };
2299+ // fetchData();
2300+ // }, [backend, id]);
2301+
2302+ // // if (isLoading) return <div>Loading...</div>;
2303+
2304+
2305+
2306+ // return (
2307+ // <div>
2308+ // <PDFButtonInvoice
2309+ // id={invoiceId}
2310+ // />
2311+ // </div>
2312+ // );
2313+ // };
0 commit comments