chore: update remove file function logic (#1259)

* chore: update remove file function logic

* fix: workspace file delete endpoint
This commit is contained in:
Aaryan Khandelwal 2023-06-09 10:24:57 +05:30 committed by GitHub
parent d09f410f21
commit e7af3da115
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 24 deletions

View File

@ -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<Props> = ({
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<Props> = ({
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<Props> = ({
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);

View File

@ -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: "" })

View File

@ -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) => {

View File

@ -40,8 +40,11 @@ class FileServices extends APIService {
});
}
async deleteFile(asset: string): Promise<any> {
return this.delete(`/api/workspaces/file-assets/${asset}/`)
async deleteFile(workspaceId: string, assetUrl: string): Promise<any> {
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<any> {
return this.delete(`/api/users/file-assets/${asset}`)
async deleteUserFile(assetUrl: string): Promise<any> {
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;