//backup const generateConfirmationPDF = async () => { try { const input = pdfRef.current; if (!input) return null; const canvas = await html2canvas(input, { scale: 2, logging: false, useCORS: true, allowTaint: true, }); const pdf = new jsPDF("p", "mm", "a4"); pdf.addImage(canvas, "JPEG", 0, 0, 210, 297); const pdfBytes = pdf.output("arraybuffer"); const pdfBase64 = btoa( new Uint8Array(pdfBytes).reduce( (data, byte) => data + String.fromCharCode(byte), "" ) ); return pdfBase64; } catch (error) { console.error("Error generating PDF:", error); throw error; } }; const generateCOCPDF = async () => { try { const input = COCRef.current; if (!input) return null; const canvas = await html2canvas(input, { scale: 2, logging: false, useCORS: true, allowTaint: true, }); const pdf = new jsPDF("p", "mm", "a4"); pdf.addImage(canvas, "JPEG", 0, 0, 210, 297); const pdfBytes = pdf.output("arraybuffer"); const pdfCOC64 = btoa( new Uint8Array(pdfBytes).reduce( (data, byte) => data + String.fromCharCode(byte), "" ) ); return pdfCOC64; } catch (error) { console.error("Error generating PDF:", error); throw error; } }; const sendConfirmationEmail = async () => { setLoading(true); // setClickedSend(true); if ( !removeCocMail && !COCRef.current) { toast({ title: "COC generation failed.", description: "Please refresh the page and try again.", status: "error", className: "bg-red-400", duration: 3000, }); setShowPopup(""); setLoading(false) setClickedSend(false) return; } try { const pdfBase64 = await generateConfirmationPDF(); const pdfCOC64 = await generateCOCPDF(); const emailObj = { emailList, ccList, subject, initiated_by: userId, order_id: [Number(orderId)], type: "customer", receipient_id:customerId, customer_exact_id: customerExactId, html: html?.length > 0 ? html?.replace(/0\.8vw/g, "16px") : html, attachments: [ ...orderConfirmationPdf ? [{ name: `${documentTitle}.pdf`, data: pdfBase64 }] : [], ...(isCocNeeded && !removeCocMail ? [ { name: "ConformityCertificate.pdf", data: pdfCOC64, }, ] : []), ...(Array.isArray(attachments) ? attachments : [attachments]), ], }; if (emailList.length === 0) { toast({ title: "Recipient email", description: "Please add at least one recipient email", status: "error", className: "bg-red-300", }); return; } if (!subject) { toast({ title: "Email subject", description: "Please add an email subject", status: "error", className: "bg-red-300", }); return; } // if (!html || html === "
") { // toast({ // title: "Email body", // description: "Please add an email body", // status: "error", // className: "bg-red-300", // }); // return; // } const response = await dispatch(SendConfirmationEmail(emailObj)); localStorage.setItem('customerEmailData', JSON.stringify({ payload: emailObj, response: response })); if (response.type === "/customers/sendConfirmationEmail/fulfilled") { handleSendClick(); setTimeout(() => { window.location.reload(); }, 1000); } else { toast({ title: "Email sending failed", description: response?.payload?.error, status: "error", className: "bg-red-500", duration: 3000, }); } } catch (error) { console.error(error); setError("Failed to send confirmation email"); } finally { setLoading(false); setClickedSend(false); } }; const handleFileUpload = (e) => { const files = Array.from(e.target.files || []); const MAX_FILE_SIZE = 25 * 1024 * 1024; const oversizedFiles = files.filter((file) => file.size > MAX_FILE_SIZE); if (oversizedFiles.length > 0) { toast({ title: "File size exceeded", description: `Please upload a file smaller than 25 MB.`, status: "error", className: "bg-red-300", }); if (fileInputRef.current) fileInputRef.current.value = null; return; } files.forEach((file) => { const reader = new FileReader(); reader.onload = (event) => { const base64 = event.target?.result.split(",")[1]; setAttachments((prev) => [...prev, { name: file.name, data: base64 }]); }; reader.readAsDataURL(file); }); if (fileInputRef.current) fileInputRef.current.value = null; }; const { getRootProps, getInputProps, isDragActive } = useFileDropzone({ attachments, setAttachments, toast, maxFiles: 50, maxSizeMB: 25, acceptedFormats:{}, convertToBase64:true, }); ----------------------------------------------------------------------------------------------------------- export const SendConfirmationEmail = createAsyncThunk( "/customers/sendConfirmationEmail", async ( { emailList, ccList, subject, html, attachments, initiated_by, order_id, type, receipient_id, customer_exact_id }, { rejectWithValue } ) => { try { const emailObj = { to: emailList, cc: ccList, subject, body: html, attachments: attachments, initiated_by, order_id: order_id, type, receipient_id, customer_exact_id }; const response = await poService.post( "/po_service/send_email/", emailObj ); if (response.status === 200 && response.data.status === "success") { return "success"; } else { return rejectWithValue({ title: response.data?.message || "Email sending failed", description: response.data?.error || "No detailed error message provided", statusCode: response.data?.status_code || response.status, status: response.data?.status || "failed", data: response.data?.data }); } } catch (error) { return rejectWithValue(error.response?.data || "Failed to send email"); } } );