Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions src/components/admin/SectionForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ const fetchFiles = async (data, fileType, language) => {

const response = await fetch(item.url, {
method: 'GET',
mode: 'no-cors',
});

const blob = await response.blob();

const type = fileType === 'image' ? 'image/webp' : 'application/pdf';
Expand Down Expand Up @@ -111,7 +109,6 @@ const SectionForm = ({
async function fetchAttachments() {
if (section.files.length) {
const data = await fetchFiles(section.files, 'pdf', language);

setAttachments(data);
}
}
Expand Down Expand Up @@ -167,18 +164,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
),
);

Expand All @@ -191,7 +188,6 @@ const SectionForm = ({

filesToAdd.forEach(async (file) => {
const blob = await fileToDataUri(file);

onSectionAttachment(section.frontId, blob, { [language]: file.name });
});
}
Expand Down Expand Up @@ -391,7 +387,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
/>
Expand Down
Loading