diff --git a/src/pages/gallery/index.tsx b/src/pages/gallery/index.tsx index 0ad5c58cf..094c1ad63 100644 --- a/src/pages/gallery/index.tsx +++ b/src/pages/gallery/index.tsx @@ -10,10 +10,10 @@ import { clearKeys, getKey, SESSION_KEYS } from 'utils/storage/sessionStorage'; import { File, getLocalFiles, - deleteFiles, syncFiles, updateMagicMetadata, VISIBILITY_STATE, + trashFiles, } from 'services/fileService'; import styled from 'styled-components'; import LoadingBar from 'react-top-loading-bar'; @@ -49,7 +49,7 @@ import { LoadingOverlay } from 'components/LoadingOverlay'; import PhotoFrame from 'components/PhotoFrame'; import { changeFilesVisibility, - getSelectedFileIds, + getSelectedFiles, sortFilesIntoCollections, } from 'utils/file'; import SearchBar, { DateValue } from 'components/SearchBar'; @@ -413,10 +413,9 @@ export default function Gallery() { const deleteFileHelper = async () => { loadingBar.current?.continuousStart(); try { - const fileIds = getSelectedFileIds(selected); - await deleteFiles(fileIds); - setDeleted([...deleted, ...fileIds]); - clearSelection(); + const selectedFiles = getSelectedFiles(selected, files); + await trashFiles(selectedFiles); + setDeleted([...deleted, ...selectedFiles.map((file) => file.id)]); } catch (e) { switch (e.status?.toString()) { case ServerErrorCodes.FORBIDDEN: diff --git a/src/services/fileService.ts b/src/services/fileService.ts index f3fc24ddf..b6df38815 100644 --- a/src/services/fileService.ts +++ b/src/services/fileService.ts @@ -86,6 +86,14 @@ export const NEW_MAGIC_METADATA: MagicMetadata = { count: 0, }; +interface TrashRequest { + items: TrashRequestItems[]; +} + +interface TrashRequestItems { + fileID: number; + collectionID: number; +} export const getLocalFiles = async () => { const files: Array = (await localForage.getItem(FILES)) || []; return files; @@ -232,22 +240,23 @@ const removeDeletedCollectionFiles = async ( return files; }; -export const deleteFiles = async (filesToDelete: number[]) => { +export const trashFiles = async (filesToTrash: File[]) => { try { const token = getToken(); if (!token) { return; } - await HTTPService.post( - `${ENDPOINT}/files/delete`, - { fileIDs: filesToDelete }, - null, - { - 'X-Auth-Token': token, - } - ); + const trashRequest: TrashRequest = { + items: filesToTrash.map((file) => ({ + fileID: file.id, + collectionID: file.collectionID, + })), + }; + await HTTPService.post(`${ENDPOINT}/files/trash`, trashRequest, null, { + 'X-Auth-Token': token, + }); } catch (e) { - logError(e, 'delete failed'); + logError(e, 'trash file failed'); throw e; } }; diff --git a/src/utils/file/index.ts b/src/utils/file/index.ts index a9186ac1f..8c53c18f8 100644 --- a/src/utils/file/index.ts +++ b/src/utils/file/index.ts @@ -54,7 +54,7 @@ export function sortFilesIntoCollections(files: File[]) { return collectionWiseFiles; } -export function getSelectedFileIds(selectedFiles: SelectedState) { +function getSelectedFileIds(selectedFiles: SelectedState) { const filesIDs: number[] = []; for (const [key, val] of Object.entries(selectedFiles)) { if (typeof val === 'boolean' && val) {