Skip to content

Commit

Permalink
feat: implement drive item deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
Earthyyy committed Jun 16, 2024
1 parent 8fab2d8 commit 56cd151
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
} from "@/components/ui/dropdown-menu";
import { useRouter } from "next/navigation";
import { BACKEND_BASE_URL } from "@/lib/constants";
import { useModal } from "@/hooks/use-modal-store";

type fileType =
| "image/png"
Expand All @@ -51,6 +52,7 @@ const DriveItem = ({
folderId,
fileUrl,
}: DriveItemProps) => {
const { onOpen } = useModal();
const router = useRouter();
const getIcon = (type: fileType) => {
if (type === "folder") {
Expand Down Expand Up @@ -130,7 +132,13 @@ const DriveItem = ({
Details
</DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuItem className="text-red-500 group">
<DropdownMenuItem
className="text-red-500 group"
onClick={(event) => {
event.stopPropagation();
onOpen("delete-drive-item", { itemId: folderId });
}}
>
<Delete className="h-4 w-4 mr-2 group-hover:text-red-700" />
<span className="group-hover:text-red-700">Delete</span>
</DropdownMenuItem>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { useQueryClient } from "@tanstack/react-query";
import { useSession } from "next-auth/react";
import { useToast } from "../ui/use-toast";
import { useModal } from "@/hooks/use-modal-store";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "../ui/dialog";
import { Button } from "../ui/button";
import axios from "axios";
import { DELETE_DRIVE_ITEM_ENDPOINT } from "@/lib/constants";

const DeleteDriveItemModal = () => {
const { data: session } = useSession();
const queryClient = useQueryClient();
const { toast } = useToast();
const { isOpen, onClose, type, data } = useModal();

const isModalOpen = isOpen && type === "delete-drive-item";
const itemId = data?.itemId;
const access_token = session?.tokens.access_token;

const handleClose = () => {
onClose();
};

const handleDelete = async () => {
try {
await axios.delete(`${DELETE_DRIVE_ITEM_ENDPOINT(itemId)}`, {
headers: {
Authorization: `Bearer ${access_token}`,
},
});

toast({
title: "Item deleted",
description: "The item has been deleted successfully",
});
queryClient.invalidateQueries({
queryKey: ["drive-items"],
});
onClose();
} catch (error) {
toast({
title: "Error",
description: "An error occurred while deleting the item",
variant: "destructive",
});
}
};

return (
<Dialog open={isModalOpen} onOpenChange={handleClose}>
<DialogContent className="p-4 overflow-hidden">
<DialogHeader>
<DialogTitle>Delete item</DialogTitle>
<DialogDescription>
Are you sure you want to delete this item?
</DialogDescription>
</DialogHeader>

<DialogFooter>
<Button variant="secondary" onClick={handleClose}>
Cancel
</Button>
<Button variant="destructive" onClick={handleDelete}>
Delete
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
};

export default DeleteDriveItemModal;
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import NewConversationModal from "../modal/new-conversation-modal";
import GroupSettingsModal from "../modal/group-settings-modal";
import NewMemberModal from "../modal/new-member-modal";
import NewFileModal from "../modal/new-file-modal";
import DeleteDriveItemModal from "../modal/delete-drive-item-modal";

export const ModalProvider = () => {
const [isMounted, setIsMounted] = useState(false);
Expand All @@ -22,6 +23,7 @@ export const ModalProvider = () => {
<NewFileModal />
<NewConversationModal />
<NewFolderModal />
<DeleteDriveItemModal />
<NewGroupModal />
<GroupSettingsModal />
<NewMemberModal />
Expand Down
11 changes: 10 additions & 1 deletion packages/insea-connect-ui/components/provider/query-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@ import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { useState } from "react";

const QueryProvider = ({ children }: { children: React.ReactNode }) => {
const [queryClient] = useState(() => new QueryClient());
const [queryClient] = useState(
() =>
new QueryClient({
defaultOptions: {
queries: {
refetchOnWindowFocus: false,
},
},
})
);
return (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
);
Expand Down
1 change: 1 addition & 0 deletions packages/insea-connect-ui/hooks/use-modal-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { create } from "zustand";
export type ModalType =
| "new-file"
| "new-folder"
| "delete-drive-item"
| "new-group"
| "new-member"
| "group-settings"
Expand Down
3 changes: 3 additions & 0 deletions packages/insea-connect-ui/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,6 @@ export const UPLOAD_FILE_ENDPOINT = (
parentId: string = "0"
) =>
`${BACKEND_BASE_URL}/${BACKEND_API_VERSION}/drive/${degreePathId}/folders/${parentId}/upload`;

export const DELETE_DRIVE_ITEM_ENDPOINT = (itemId: string) =>
`${BACKEND_BASE_URL}/${BACKEND_API_VERSION}/drive/items/${itemId}`;

0 comments on commit 56cd151

Please sign in to comment.