|
|
@@ -6,7 +6,7 @@ import Stack from "@mui/material/Stack"; |
|
|
|
import Print from '@mui/icons-material/Print'; |
|
|
|
// import { CreateInvoiceInputs, saveInvoice } from "@/app/api/companys/actions"; |
|
|
|
import { useRouter } from "next/navigation"; |
|
|
|
import React, { useCallback, useState, useLayoutEffect } from "react"; |
|
|
|
import React, { useCallback, useState, useLayoutEffect, useEffect } from "react"; |
|
|
|
import { useTranslation } from "react-i18next"; |
|
|
|
import { |
|
|
|
FieldErrors, |
|
|
@@ -23,6 +23,7 @@ import ProjectDetails from "./ProjectDetails"; |
|
|
|
import ProjectTotalFee from "./ProjectTotalFee"; |
|
|
|
import { timestampToDateString } from "@/app/utils/formatUtil"; |
|
|
|
import dayjs from "dayjs"; |
|
|
|
import { getSession } from "next-auth/react" |
|
|
|
|
|
|
|
const CreateInvoice: React.FC = ({ |
|
|
|
}) => { |
|
|
@@ -34,7 +35,7 @@ const CreateInvoice: React.FC = ({ |
|
|
|
const [invoiceDetail, setInvoiceDetail] = useState<InvoiceInformation>() |
|
|
|
const [serverError, setServerError] = useState(""); |
|
|
|
|
|
|
|
// const { getValues } = useForm(); |
|
|
|
const [accessToken, setAccessToken] = useState(''); |
|
|
|
|
|
|
|
const fetchProjectDetails = async () =>{ |
|
|
|
const projectId = searchParams.get("id") |
|
|
@@ -78,13 +79,84 @@ const CreateInvoice: React.FC = ({ |
|
|
|
fetchInvoiceDetails() |
|
|
|
}, []) |
|
|
|
|
|
|
|
// useEffect(() => { |
|
|
|
// const fetchData = async () => { |
|
|
|
// try { |
|
|
|
// const session = await getSession(); |
|
|
|
|
|
|
|
// if (session?.accessToken) { |
|
|
|
// const accessToken = session.accessToken; |
|
|
|
// // Use the access token as needed |
|
|
|
// setAccessToken(accessToken) |
|
|
|
// console.log(accessToken); |
|
|
|
// } else { |
|
|
|
// throw new Error('Access token not found in the session.'); |
|
|
|
// } |
|
|
|
// } catch (error) { |
|
|
|
// console.error(error); |
|
|
|
// } |
|
|
|
// }; |
|
|
|
|
|
|
|
// fetchData(); |
|
|
|
// }, []); |
|
|
|
|
|
|
|
|
|
|
|
const handleCancel = () => { |
|
|
|
router.back(); |
|
|
|
}; |
|
|
|
|
|
|
|
const handlePrintout = () => { |
|
|
|
// const formData = getValues(); |
|
|
|
console.log("Printing in Progress") |
|
|
|
|
|
|
|
const handlePrintout = async () => { |
|
|
|
const formData = formProps.getValues() |
|
|
|
const projectId = searchParams.get("id") |
|
|
|
// console.log(formData, projectId) |
|
|
|
const tempData = { |
|
|
|
...formData, |
|
|
|
id: projectId |
|
|
|
} |
|
|
|
try { |
|
|
|
// Make an API request to generate the JasperReport |
|
|
|
|
|
|
|
const response = await fetch(`http://localhost:8090/api/invoices/pdf`, { |
|
|
|
method: 'POST', |
|
|
|
headers: { |
|
|
|
'Content-Type': 'application/json', |
|
|
|
Authorization: `Bearer ${accessToken}` |
|
|
|
}, |
|
|
|
|
|
|
|
body: JSON.stringify(tempData), |
|
|
|
}); |
|
|
|
|
|
|
|
// Check if the request was successful |
|
|
|
if (response.ok) { |
|
|
|
// Extract the filename from the response headers |
|
|
|
const contentDisposition = response.headers.get('content-disposition'); |
|
|
|
console.log(contentDisposition) |
|
|
|
const fileName = contentDisposition |
|
|
|
? contentDisposition.split('filename=')[1] |
|
|
|
: 'invoice.pdf'; |
|
|
|
|
|
|
|
// Convert the response data to a Blob object |
|
|
|
const blob = await response.blob(); |
|
|
|
|
|
|
|
// Create a temporary URL for the Blob object |
|
|
|
const url = URL.createObjectURL(blob); |
|
|
|
|
|
|
|
// Create a link element to trigger the file download |
|
|
|
const link = document.createElement('a'); |
|
|
|
link.href = url; |
|
|
|
link.download = fileName; |
|
|
|
link.click(); |
|
|
|
|
|
|
|
// Clean up the temporary URL |
|
|
|
URL.revokeObjectURL(url); |
|
|
|
} else { |
|
|
|
throw new Error('Failed to generate the JasperReport.'); |
|
|
|
} |
|
|
|
} catch (error) { |
|
|
|
console.error(error); |
|
|
|
// Handle any errors that occurred during the process |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
const onSubmit = useCallback<SubmitHandler<InvoiceResult>>( |
|
|
|