{"version":3,"file":"AttachmentUploader.aOHyNZPM.js","sources":["../../src/components/attachments/AttachmentUploader.jsx"],"sourcesContent":["import { useEffect, useState } from \"react\";\r\nimport { Button, IconButton, Paper, Switch, Tooltip, Typography } from \"@mui/material\";\r\nimport PropTypes from \"prop-types\";\r\n\r\nimport ProgressBar from \"src/components/layout/Progress/ProgressBar\";\r\nimport { useUserData } from \"src/context/userContext\";\r\nimport { attachment_upload } from \"./AttachmentFunctions\";\r\nimport { pumiFi_attachment_upload } from \"src/data/pumiFi/pumiFiClass\";\r\nimport { v4 as uuidv4 } from \"uuid\";\r\nimport { aikaPvmStringLong } from \"src/components/js/statics1\";\r\nimport { CloseIcon } from \"src/components/icons/ActionIcons\";\r\nimport { allowAccess_KalleUrhoOy_Toiminnot } from \"src/data/ifs/allowAccess\";\r\nimport styled from \"styled-components\";\r\nimport ProgressCirc from \"../layout/Progress/ProgressCirc\";\r\n\r\nconst ProcessLogContainer = styled(Paper)`\r\n font-family: monospace;\r\n padding: 1px;\r\n margin-top: 1px;\r\n text-align: left;\r\n font-size: 0.8rem;\r\n width: 100%;\r\n position: relative;\r\n border: ${(props) => (props.$uploadError ? \"1px solid red\" : \"none\")};\r\n border-radius: 4px;\r\n`;\r\n\r\nexport default function AttachmentUploader({ onUpload = () => {}, keikkaId = 0 }) {\r\n const { token, userAsiakas, user } = useUserData();\r\n\r\n const [dragging, setDragging] = useState(false);\r\n const [isLoadingFileName, setIsLoadingFileName] = useState(null);\r\n const [isUploadToPumiFiChecked, setIsUploadToPumiFiChecked] = useState(true);\r\n const [processLog, setProcessLog] = useState([]);\r\n const [uploadError, setUploadError] = useState(false);\r\n\r\n // const [uploadingIndexNumber, setUploadingIndexNumber] = useState(0);\r\n const [uploadTotalBytes, setUploadTotalBytes] = useState(0);\r\n const [uploadedBytes, setUploadedBytes] = useState(0);\r\n\r\n const handleFileChange = async (event) => {\r\n const input = event.target;\r\n await uploadFiles(event.target.files);\r\n input.value = null;\r\n };\r\n\r\n useEffect(() => {\r\n const uploadAlsoToPumiFi = allowAccess_KalleUrhoOy_Toiminnot(user);\r\n setIsUploadToPumiFiChecked(uploadAlsoToPumiFi);\r\n }, [user]);\r\n\r\n const addLog = (log) => {\r\n console.log(log);\r\n setProcessLog((prevLogs) => [...prevLogs, log]);\r\n };\r\n\r\n const uploadFiles = async (files) => {\r\n setUploadError(false);\r\n if (files.length === 0) {\r\n setProcessLog([]);\r\n addLog(\"EI TIEDOSTOJA\");\r\n addLog(\"Yhtään tiedostoa ei löytynyt.\");\r\n setUploadError(true);\r\n return;\r\n }\r\n addLog(aikaPvmStringLong(new Date(), true) + \" Aloitetaan tiedostojen siirto...\");\r\n const startTime = new Date().getTime();\r\n setProcessLog([]);\r\n // count total bytes\r\n let _bytes = 0;\r\n for (let i = 0; i < files.length; i++) {\r\n _bytes += files[i].size;\r\n }\r\n if (isUploadToPumiFiChecked) _bytes *= 2;\r\n setUploadTotalBytes(_bytes);\r\n\r\n for (let i = 0; i < files.length; i++) {\r\n addLog(\"- Lähetetään tiedosto \" + files[i].name);\r\n const file = files[i];\r\n // create new filename as guid for each file\r\n const newFileName = uuidv4() + files[i].name.substring(file.name.lastIndexOf(\".\"));\r\n const origFileName = file.name;\r\n setIsLoadingFileName(origFileName);\r\n const ownerAsiakasId = userAsiakas.asiakasId;\r\n const fileComment = null;\r\n\r\n try {\r\n const attachmentId = await attachment_upload(\r\n file,\r\n keikkaId,\r\n 0,\r\n 0,\r\n 0,\r\n 0,\r\n 0,\r\n ownerAsiakasId,\r\n newFileName,\r\n fileComment,\r\n user.personId,\r\n token\r\n );\r\n const size = file.size;\r\n setUploadedBytes((prev) => prev + size);\r\n if (attachmentId > 0 && isUploadToPumiFiChecked) {\r\n addLog(\"- Lähetetään tiedosto PUMI.fi palveluun\");\r\n await pumiFi_attachment_upload(file, newFileName, attachmentId, origFileName, token);\r\n setUploadedBytes((prev) => prev + size);\r\n }\r\n } catch (error) {\r\n addLog(\"Virhe tiedoston \" + files[i].name + \" siirrossa: \" + error);\r\n setUploadError(true);\r\n }\r\n }\r\n // calculate how long it took to upload all files\r\n const endTime = new Date().getTime();\r\n const timeDiff = Math.round((endTime - startTime) / 1000);\r\n addLog(\"Kesto: \" + timeDiff + \" s\");\r\n addLog(\r\n aikaPvmStringLong(new Date(), true) +\r\n \" Kaikki \" +\r\n files.length +\r\n \" tiedostoa lähetetty (\" +\r\n timeDiff +\r\n \" s)\"\r\n );\r\n setUploadedBytes(0);\r\n setIsLoadingFileName(null);\r\n onUpload(files);\r\n };\r\n const handleCloseClick = () => {\r\n setProcessLog([]);\r\n setIsLoadingFileName(null);\r\n setDragging(false);\r\n };\r\n const handleDragOver = (event) => {\r\n event.preventDefault();\r\n setDragging(true);\r\n };\r\n const handleDrop = async (event) => {\r\n event.preventDefault();\r\n setDragging(false);\r\n await uploadFiles(event.dataTransfer.files);\r\n };\r\n\r\n return (\r\n