From e7af3da1153e972ab1c33c681ff8ae449ac3d242 Mon Sep 17 00:00:00 2001 From: Aaryan Khandelwal <65252264+aaryan610@users.noreply.github.com> Date: Fri, 9 Jun 2023 10:24:57 +0530 Subject: [PATCH] chore: update remove file function logic (#1259) * chore: update remove file function logic * fix: workspace file delete endpoint --- .../app/components/core/image-upload-modal.tsx | 18 ++++++------------ .../pages/[workspaceSlug]/me/profile/index.tsx | 5 +---- .../pages/[workspaceSlug]/settings/index.tsx | 5 +---- apps/app/services/file.service.ts | 14 ++++++++++---- 4 files changed, 18 insertions(+), 24 deletions(-) diff --git a/apps/app/components/core/image-upload-modal.tsx b/apps/app/components/core/image-upload-modal.tsx index 2ede6e8d7..4168c5e0a 100644 --- a/apps/app/components/core/image-upload-modal.tsx +++ b/apps/app/components/core/image-upload-modal.tsx @@ -9,6 +9,8 @@ import { useDropzone } from "react-dropzone"; import { Transition, Dialog } from "@headlessui/react"; // services import fileServices from "services/file.service"; +// hooks +import useWorkspaceDetails from "hooks/use-workspace-details"; // ui import { PrimaryButton, SecondaryButton } from "components/ui"; // icons @@ -35,6 +37,8 @@ export const ImageUploadModal: React.FC = ({ const router = useRouter(); const { workspaceSlug } = router.query; + const { workspaceDetails } = useWorkspaceDetails(); + const onDrop = useCallback((acceptedFiles: File[]) => { setImage(acceptedFiles[0]); }, []); @@ -62,12 +66,7 @@ export const ImageUploadModal: React.FC = ({ setIsImageUploading(false); setImage(null); - if (value) { - const index = value.indexOf(".com"); - const asset = value.substring(index + 5); - - fileServices.deleteUserFile(asset); - } + if (value) fileServices.deleteUserFile(value); }) .catch((err) => { console.error(err); @@ -81,12 +80,7 @@ export const ImageUploadModal: React.FC = ({ setIsImageUploading(false); setImage(null); - if (value) { - const index = value.indexOf(".com"); - const asset = value.substring(index + 5); - - fileServices.deleteFile(asset); - } + if (value && workspaceDetails) fileServices.deleteFile(workspaceDetails.id, value); }) .catch((err) => { console.error(err); diff --git a/apps/app/pages/[workspaceSlug]/me/profile/index.tsx b/apps/app/pages/[workspaceSlug]/me/profile/index.tsx index c64a5a4d1..ce589c41d 100644 --- a/apps/app/pages/[workspaceSlug]/me/profile/index.tsx +++ b/apps/app/pages/[workspaceSlug]/me/profile/index.tsx @@ -90,10 +90,7 @@ const Profile: NextPage = () => { setIsRemoving(true); - const index = url.indexOf(".com"); - const asset = url.substring(index + 5); - - fileService.deleteUserFile(asset).then(() => { + fileService.deleteUserFile(url).then(() => { if (updateUser) userService .updateUser({ avatar: "" }) diff --git a/apps/app/pages/[workspaceSlug]/settings/index.tsx b/apps/app/pages/[workspaceSlug]/settings/index.tsx index 1ac94661b..9e0917d16 100644 --- a/apps/app/pages/[workspaceSlug]/settings/index.tsx +++ b/apps/app/pages/[workspaceSlug]/settings/index.tsx @@ -111,10 +111,7 @@ const WorkspaceSettings: NextPage = () => { setIsImageRemoving(true); - const index = url.indexOf(".com"); - const asset = url.substring(index + 5); - - fileService.deleteFile(asset).then(() => { + fileService.deleteFile(activeWorkspace.id, url).then(() => { workspaceService .updateWorkspace(activeWorkspace.slug, { logo: "" }, user) .then((res) => { diff --git a/apps/app/services/file.service.ts b/apps/app/services/file.service.ts index d3a5118b8..ad87e3a19 100644 --- a/apps/app/services/file.service.ts +++ b/apps/app/services/file.service.ts @@ -40,8 +40,11 @@ class FileServices extends APIService { }); } - async deleteFile(asset: string): Promise { - return this.delete(`/api/workspaces/file-assets/${asset}/`) + async deleteFile(workspaceId: string, assetUrl: string): Promise { + const lastIndex = assetUrl.lastIndexOf("/"); + const assetId = assetUrl.substring(lastIndex + 1); + + return this.delete(`/api/workspaces/file-assets/${workspaceId}/${assetId}/`) .then((response) => response?.data) .catch((error) => { throw error?.response?.data; @@ -56,8 +59,11 @@ class FileServices extends APIService { }); } - async deleteUserFile(asset: string): Promise { - return this.delete(`/api/users/file-assets/${asset}`) + async deleteUserFile(assetUrl: string): Promise { + const lastIndex = assetUrl.lastIndexOf("/"); + const assetId = assetUrl.substring(lastIndex + 1); + + return this.delete(`/api/users/file-assets/${assetId}`) .then((response) => response?.data) .catch((error) => { throw error?.response?.data;