From 67946fe7bc396f4c12b21ac7cf15b83186697679 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arto=20P=C3=A4rssinen?= Date: Tue, 25 Mar 2025 13:18:54 +0200 Subject: [PATCH 1/2] fix: fixed fileupload not working as intended --- src/components/admin/SectionForm.jsx | 29 ++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/components/admin/SectionForm.jsx b/src/components/admin/SectionForm.jsx index 6496e72c3..bbfe5f2bc 100644 --- a/src/components/admin/SectionForm.jsx +++ b/src/components/admin/SectionForm.jsx @@ -5,7 +5,7 @@ import { connect } from 'react-redux'; import PropTypes from 'prop-types'; import { FormattedMessage, useIntl } from 'react-intl'; import { get, isEmpty } from 'lodash'; -import { Button, Card, Checkbox, FileInput, LoadingSpinner, Select } from 'hds-react'; +import { Button, Card, Checkbox, FileInput, formatBytes, LoadingSpinner, Select } from 'hds-react'; import { QuestionForm } from './QuestionForm'; import MultiLanguageTextField, { TextFieldTypes } from '../forms/MultiLanguageTextField'; @@ -37,11 +37,15 @@ const fetchFiles = async (data, fileType, language) => { const response = await fetch(item.url, { method: 'GET', - mode: 'no-cors', }); - const blob = await response.blob(); + // Check if blob size is suspiciously small + if (blob.size < 100) { + console.debug(`Warning: File ${name} from ${item.url} has suspiciously small size: ${blob.size} bytes`); + // You might decide to throw an error here instead of continuing + } + const type = fileType === 'image' ? 'image/webp' : 'application/pdf'; const file = new File([blob], name || fileType, { @@ -92,6 +96,8 @@ const SectionForm = ({ const [sectionImage, setSectionImage] = useState(); const [attachments, setAttachments] = useState(); + console.debug(attachments); + const intl = useIntl(); useEffect(() => { @@ -111,7 +117,7 @@ const SectionForm = ({ async function fetchAttachments() { if (section.files.length) { const data = await fetchFiles(section.files, 'pdf', language); - + console.debug('section files', data); setAttachments(data); } } @@ -167,18 +173,18 @@ const SectionForm = ({ */ const onAttachmentChange = (files) => { const filesToDelete = attachments?.filter( - (item, oldIndex) => + (item) => !files.some( - (newFile, newIndex) => - item.file.name === newFile.name && item.file.size === newFile.size && oldIndex === newIndex, + (newFile) => + item.file.name === newFile.name && item.file.size === newFile.size ), ); const filesToAdd = files.filter( - (newFile, newIndex) => + (newFile) => !attachments?.some( - (item, oldIndex) => - newFile.name === item.file.name && newFile.size === item.file.size && newIndex === oldIndex, + (item) => + newFile.name === item.file.name && newFile.size === item.file.size ), ); @@ -191,7 +197,6 @@ const SectionForm = ({ filesToAdd.forEach(async (file) => { const blob = await fileToDataUri(file); - onSectionAttachment(section.frontId, blob, { [language]: file.name }); }); } @@ -391,7 +396,7 @@ const SectionForm = ({ accept={ACCEPTED_FILE_TYPES} language={language} onChange={onAttachmentChange} - defaultValue={attachments} + defaultValue={attachments?.map(item => item.file) || []} maxSize={MAX_FILE_SIZE * 1024 * 1024} multiple /> From b202970c1e6c7005ef3076431b8e4b83c139abec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arto=20P=C3=A4rssinen?= Date: Tue, 25 Mar 2025 14:12:11 +0200 Subject: [PATCH 2/2] chore: fix linter errors --- src/components/admin/SectionForm.jsx | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/components/admin/SectionForm.jsx b/src/components/admin/SectionForm.jsx index bbfe5f2bc..0ff428f33 100644 --- a/src/components/admin/SectionForm.jsx +++ b/src/components/admin/SectionForm.jsx @@ -5,7 +5,7 @@ import { connect } from 'react-redux'; import PropTypes from 'prop-types'; import { FormattedMessage, useIntl } from 'react-intl'; import { get, isEmpty } from 'lodash'; -import { Button, Card, Checkbox, FileInput, formatBytes, LoadingSpinner, Select } from 'hds-react'; +import { Button, Card, Checkbox, FileInput, LoadingSpinner, Select } from 'hds-react'; import { QuestionForm } from './QuestionForm'; import MultiLanguageTextField, { TextFieldTypes } from '../forms/MultiLanguageTextField'; @@ -40,12 +40,6 @@ const fetchFiles = async (data, fileType, language) => { }); const blob = await response.blob(); - // Check if blob size is suspiciously small - if (blob.size < 100) { - console.debug(`Warning: File ${name} from ${item.url} has suspiciously small size: ${blob.size} bytes`); - // You might decide to throw an error here instead of continuing - } - const type = fileType === 'image' ? 'image/webp' : 'application/pdf'; const file = new File([blob], name || fileType, { @@ -96,8 +90,6 @@ const SectionForm = ({ const [sectionImage, setSectionImage] = useState(); const [attachments, setAttachments] = useState(); - console.debug(attachments); - const intl = useIntl(); useEffect(() => { @@ -117,7 +109,6 @@ const SectionForm = ({ async function fetchAttachments() { if (section.files.length) { const data = await fetchFiles(section.files, 'pdf', language); - console.debug('section files', data); setAttachments(data); } }