Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mv and rm batch calls #1596

Merged
merged 7 commits into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import { t } from "@lingui/macro"
import { CONTENT_TYPES } from "../../../Utils/Constants"
import { IFilesTableBrowserProps } from "../../Modules/FileBrowsers/types"
import { useHistory, useLocation, useToasts } from "@chainsafe/common-components"
import { extractFileBrowserPathFromURL, getPathWithFile, getUrlSafePathWithFile } from "../../../Utils/pathUtils"
import { extractFileBrowserPathFromURL, getAbsolutePathsFromCids, getUrlSafePathWithFile } from "../../../Utils/pathUtils"
import { ROUTE_LINKS } from "../../FilesRoutes"
import { FileBrowserContext } from "../../../Contexts/FileBrowserContext"
import { useFilesApi } from "../../../Contexts/FilesApiContext"
import { parseFileContentResponse } from "../../../Utils/Helpers"

const BinFileBrowser: React.FC<IFileBrowserModuleProps> = ({ controls = false }: IFileBrowserModuleProps) => {
const { buckets, refreshBuckets } = useFiles()
const { buckets } = useFiles()
const { filesApiClient } = useFilesApi()
const { addToast } = useToasts()
const [loadingCurrentPath, setLoadingCurrentPath] = useState(false)
Expand Down Expand Up @@ -52,83 +52,51 @@ const BinFileBrowser: React.FC<IFileBrowserModuleProps> = ({ controls = false }:
refreshContents(true)
}, [bucket, refreshContents])

const deleteFile = useCallback(async (cid: string) => {
const itemToDelete = pathContents.find((i) => i.cid === cid)
const deleteItems = useCallback(async (cids: string[]) => {
if (!bucket) return

if (!itemToDelete || !bucket) {
console.error("Bucket not set or no item found to delete")
return
}
const pathsToDelete = getAbsolutePathsFromCids(cids, currentPath, pathContents)

filesApiClient.removeBucketObject(bucket.id, { paths: pathsToDelete })
.then(() => {
addToast({
title: t`Data deleted successfully`,
type: "success",
testId: "deletion-success"
})
}).catch((error) => {
console.error("Error deleting:", error)
addToast({
title: t`There was an error deleting your data`,
type: "error"
})
}).finally(refreshContents)
}, [addToast, bucket, currentPath, filesApiClient, pathContents, refreshContents])

try {
await filesApiClient.removeBucketObject(
bucket.id,
{ paths: [getPathWithFile(currentPath, itemToDelete.name)] }
)
const recoverItems = useCallback(async (cids: string[], newPath: string) => {
if (!bucket) return

refreshContents()
refreshBuckets()
const message = `${
itemToDelete.isFolder ? t`Folder` : t`File`
} ${t`deleted successfully`}`
const pathsToRecover = getAbsolutePathsFromCids(cids, currentPath, pathContents)

filesApiClient.moveBucketObjects(
bucket.id,
{
paths: pathsToRecover,
new_path: newPath,
destination: buckets.find(b => b.type === "csf")?.id
}
).then(() => {
addToast({
title: message,
type: "success",
testId: "permanent-deletion-success"
title: t`Data restored successfully`,
type: "success"
})
return Promise.resolve()
} catch (error) {
const message = `${t`There was an error deleting this`} ${
itemToDelete.isFolder ? t`folder` : t`file`
}`
}).catch((error) => {
console.error("Error recovering:", error)
addToast({
title: message,
title: t`There was an error restoring your data`,
type: "error"
})
return Promise.reject()
}
}, [addToast, bucket, currentPath, pathContents, refreshContents, refreshBuckets, filesApiClient])

const deleteItems = useCallback(async (cids: string[]) => {
await Promise.all(
cids.map((cid: string) =>
deleteFile(cid)
))
refreshContents()
}, [deleteFile, refreshContents])

const recoverItems = useCallback(async (cids: string[], newPath: string) => {
if (!bucket) return
await Promise.all(
cids.map(async (cid: string) => {
const itemToRestore = pathContents.find((i) => i.cid === cid)
if (!itemToRestore) return
try {
await filesApiClient.moveBucketObjects(
bucket.id,
{
paths: [getPathWithFile(currentPath, itemToRestore.name)],
new_path: getPathWithFile(newPath, itemToRestore.name),
destination: buckets.find(b => b.type === "csf")?.id
}
)

const message = `${itemToRestore.isFolder ? t`Folder` : t`File`} ${t`recovered successfully`}`

addToast({
title: message,
type: "success"
})
} catch (error) {
const message = `${t`There was an error recovering this`} ${
itemToRestore.isFolder ? t`folder` : t`file`
}`
addToast({
title: message,
type: "error"
})
}
})).finally(refreshContents)
}).finally(refreshContents)
}, [addToast, pathContents, refreshContents, filesApiClient, bucket, buckets, currentPath])

const viewFolder = useCallback((cid: string) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
getURISafePathFromArray,
getPathWithFile,
extractFileBrowserPathFromURL,
getUrlSafePathWithFile
getUrlSafePathWithFile,
getAbsolutePathsFromCids
} from "../../../Utils/pathUtils"
import { IBulkOperations, IFileBrowserModuleProps, IFilesTableBrowserProps } from "./types"
import FilesList from "./views/FilesList"
Expand Down Expand Up @@ -74,43 +75,28 @@ const CSFFileBrowser: React.FC<IFileBrowserModuleProps> = () => {

const moveItemsToBin = useCallback(async (cids: string[], hideToast?: boolean) => {
if (!bucket) return
await Promise.all(
cids.map(async (cid: string) => {
const itemToDelete = pathContents.find((i) => i.cid === cid)
if (!itemToDelete) {
console.error("No item found to move to the trash")
return
}

try {
await filesApiClient.moveBucketObjects(bucket.id, {
paths: [getPathWithFile(currentPath, itemToDelete.name)],
new_path: getPathWithFile("/", itemToDelete.name),
destination: buckets.find(b => b.type === "trash")?.id
})
if (!hideToast) {
const message = `${
itemToDelete.isFolder ? t`Folder` : t`File`
} ${t`deleted successfully`}`
const id = addToast({
title: message,
type: "success",
testId: "deletion-success"
})
console.log(id)
}
return Promise.resolve()
} catch (error) {
const message = `${t`There was an error deleting this`} ${
itemToDelete.isFolder ? t`folder` : t`file`
}`
addToast({
title: message,
type: "error"
})
return Promise.reject()
}}
)).finally(refreshContents)

const pathsToDelete = getAbsolutePathsFromCids(cids, currentPath, pathContents)

filesApiClient.moveBucketObjects(bucket.id, {
paths: pathsToDelete,
new_path: "/",
destination: buckets.find(b => b.type === "trash")?.id
}).then(() => {
if (!hideToast) {
addToast({
title: t`Data deleted successfully`,
type: "success",
testId: "deletion-success"
})
}
}).catch((error) => {
console.error("Error deleting:", error)
addToast({
title: t`There was an error deleting your data`,
type: "error"
})
}).finally(refreshContents)
}, [addToast, currentPath, pathContents, refreshContents, filesApiClient, bucket, buckets])

// Rename
Expand All @@ -127,33 +113,24 @@ const CSFFileBrowser: React.FC<IFileBrowserModuleProps> = () => {

const moveItems = useCallback(async (cids: string[], newPath: string) => {
if (!bucket) return
await Promise.all(
cids.map(async (cid: string) => {
const itemToMove = pathContents.find((i) => i.cid === cid)
if (!itemToMove) return
try {
await filesApiClient.moveBucketObjects(bucket.id, {
paths: [getPathWithFile(currentPath, itemToMove.name)],
new_path: getPathWithFile(newPath, itemToMove.name)
})
const message = `${
itemToMove.isFolder ? t`Folder` : t`File`
} ${t`moved successfully`}`

addToast({
title: message,
type: "success"
})
} catch (error) {
const message = `${t`There was an error moving this`} ${
itemToMove.isFolder ? t`folder` : t`file`
}`
addToast({
title: message,
type: "error"
})
}
})).finally(refreshContents)

const pathsToMove = getAbsolutePathsFromCids(cids, currentPath, pathContents)

filesApiClient.moveBucketObjects(bucket.id, {
paths: pathsToMove,
new_path: newPath
}).then(() => {
addToast({
title: t`Data moved successfully`,
type: "success"
})
}).catch((error) => {
console.error("Error moving:", error)
addToast({
title: t`There was an error moving your data`,
type: "error"
})
}).finally(refreshContents)
}, [addToast, pathContents, refreshContents, filesApiClient, bucket, currentPath])

const handleDownload = useCallback(async (cid: string) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
getURISafePathFromArray,
getPathWithFile,
extractSharedFileBrowserPathFromURL,
getUrlSafePathWithFile
getUrlSafePathWithFile,
getAbsolutePathsFromCids
} from "../../../Utils/pathUtils"
import { IBulkOperations, IFilesTableBrowserProps } from "./types"
import { CONTENT_TYPES } from "../../../Utils/Constants"
Expand All @@ -20,7 +21,7 @@ import DragAndDrop from "../../../Contexts/DnDContext"
import FilesList from "./views/FilesList"

const SharedFileBrowser = () => {
const { downloadFile, uploadFiles, buckets, refreshBuckets, getStorageSummary } = useFiles()
const { downloadFile, uploadFiles, buckets, getStorageSummary } = useFiles()
const { filesApiClient } = useFilesApi()
const { addToast } = useToasts()
const [loadingCurrentPath, setLoadingCurrentPath] = useState(false)
Expand Down Expand Up @@ -94,48 +95,26 @@ const SharedFileBrowser = () => {
refreshContents(true)
}, [bucket, refreshContents])

const deleteFile = useCallback(async (cid: string) => {
const itemToDelete = pathContents.find((i) => i.cid === cid)

if (!itemToDelete || !bucket) {
console.error("Bucket not set or no item found to delete")
return
}

try {
await filesApiClient.removeBucketObject(
bucket.id,
{ paths: [getPathWithFile(currentPath, itemToDelete.name)] }
)
const deleteItems = useCallback(async (cids: string[]) => {
if (!bucket) return

refreshContents(false)
refreshBuckets(false)
const message = `${
itemToDelete.isFolder ? t`Folder` : t`File`
} ${t`deleted successfully`}`
addToast({
title: message,
type: "success"
})
return Promise.resolve()
} catch (error) {
const message = `${t`There was an error deleting this`} ${
itemToDelete.isFolder ? t`folder` : t`file`
}`
addToast({
title: message,
type: "error"
})
return Promise.reject()
}
}, [addToast, bucket, currentPath, pathContents, refreshContents, refreshBuckets, filesApiClient])
const pathsToDelete = getAbsolutePathsFromCids(cids, currentPath, pathContents)

const deleteItems = useCallback(async (cids: string[]) => {
await Promise.all(
cids.map((cid: string) =>
deleteFile(cid)
))
}, [deleteFile])
filesApiClient.removeBucketObject(bucket.id, { paths: pathsToDelete })
.then(() => {
addToast({
title: t`Data deleted successfully`,
type: "success",
testId: "deletion-success"
})
}).catch((error) => {
console.error("Error deleting:", error)
addToast({
title: t`There was an error deleting your data`,
type: "error"
})
}).finally(refreshContents)
}, [addToast, bucket, currentPath, filesApiClient, pathContents, refreshContents])

// Rename
const renameItem = useCallback(async (cid: string, newName: string) => {
Expand All @@ -150,16 +129,25 @@ const SharedFileBrowser = () => {

const moveItems = useCallback(async (cids: string[], newPath: string) => {
if (!bucket) return
await Promise.all(
cids.map(async (cid: string) => {
const itemToMove = pathContents.find(i => i.cid === cid)
if (!bucket || !itemToMove) return
await filesApiClient.moveBucketObjects(bucket.id, {
paths: [getPathWithFile(currentPath, itemToMove.name)],
new_path: getPathWithFile(newPath, itemToMove.name)
})
})).finally(refreshContents)
}, [refreshContents, filesApiClient, bucket, currentPath, pathContents])

const pathsToMove = getAbsolutePathsFromCids(cids, currentPath, pathContents)

filesApiClient.moveBucketObjects(bucket.id, {
paths: pathsToMove,
new_path: newPath
}).then(() => {
addToast({
title: t`Data moved successfully`,
type: "success"
})
}).catch((error) => {
console.error("Error recovering:", error)
addToast({
title: t`There was an error restoring your data`,
type: "error"
})
}).finally(refreshContents)
}, [refreshContents, filesApiClient, bucket, currentPath, pathContents, addToast])

const handleDownload = useCallback(async (cid: string) => {
const itemToDownload = pathContents.find(item => item.cid === cid)
Expand Down
Loading