Skip to content

Commit

Permalink
feat: Add delete bookmark confirmation dialog. Fixes #776 (#787)
Browse files Browse the repository at this point in the history
  • Loading branch information
lexafaxine authored Dec 31, 2024
1 parent b6d5556 commit f476fca
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 38 deletions.
19 changes: 16 additions & 3 deletions apps/mobile/components/bookmarks/BookmarkCard.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
ActivityIndicator,
Alert,
Image,
Platform,
Pressable,
Expand Down Expand Up @@ -70,6 +71,20 @@ function ActionBar({ bookmark }: { bookmark: ZBookmark }) {
onError,
});

const deleteBookmarkAlert = () =>
Alert.alert(
"Delete bookmark?",
"Are you sure you want to delete this bookmark?",
[
{ text: "Cancel", style: "cancel" },
{
text: "Delete",
onPress: () => deleteBookmark({ bookmarkId: bookmark.id }),
style: "destructive",
},
],
);

return (
<View className="flex flex-row gap-4">
{(isArchivePending || isDeletionPending) && <ActivityIndicator />}
Expand All @@ -93,9 +108,7 @@ function ActionBar({ bookmark }: { bookmark: ZBookmark }) {
onPressAction={({ nativeEvent }) => {
Haptics.selectionAsync();
if (nativeEvent.event === "delete") {
deleteBookmark({
bookmarkId: bookmark.id,
});
deleteBookmarkAlert();
} else if (nativeEvent.event === "archive") {
archiveBookmark({
bookmarkId: bookmark.id,
Expand Down
21 changes: 9 additions & 12 deletions apps/web/components/dashboard/bookmarks/BookmarkOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import type {
ZBookmarkedLink,
} from "@hoarder/shared/types/bookmarks";
import {
useDeleteBookmark,
useRecrawlBookmark,
useUpdateBookmark,
} from "@hoarder/shared-react/hooks//bookmarks";
Expand All @@ -37,6 +36,7 @@ import { useBookmarkGridContext } from "@hoarder/shared-react/hooks/bookmark-gri
import { BookmarkTypes } from "@hoarder/shared/types/bookmarks";

import { BookmarkedTextEditor } from "./BookmarkedTextEditor";
import DeleteBookmarkConfirmationDialog from "./DeleteBookmarkConfirmationDialog";
import { ArchivedActionIcon, FavouritedActionIcon } from "./icons";
import { useManageListsModal } from "./ManageListsModal";
import { useTagModel } from "./TagModal";
Expand All @@ -53,6 +53,8 @@ export default function BookmarkOptions({ bookmark }: { bookmark: ZBookmark }) {
const { setOpen: setManageListsModalOpen, content: manageListsModal } =
useManageListsModal(bookmark.id);

const [deleteBookmarkDialogOpen, setDeleteBookmarkDialogOpen] =
useState(false);
const [isTextEditorOpen, setTextEditorOpen] = useState(false);

const { listId } = useBookmarkGridContext() ?? {};
Expand All @@ -63,14 +65,6 @@ export default function BookmarkOptions({ bookmark }: { bookmark: ZBookmark }) {
title: t("common.something_went_wrong"),
});
};
const deleteBookmarkMutator = useDeleteBookmark({
onSuccess: () => {
toast({
description: t("toasts.bookmarks.deleted"),
});
},
onError,
});

const updateBookmarkMutator = useUpdateBookmark({
onSuccess: () => {
Expand Down Expand Up @@ -112,6 +106,11 @@ export default function BookmarkOptions({ bookmark }: { bookmark: ZBookmark }) {
<>
{tagModal}
{manageListsModal}
<DeleteBookmarkConfirmationDialog
bookmark={bookmark}
open={deleteBookmarkDialogOpen}
setOpen={setDeleteBookmarkDialogOpen}
/>
<BookmarkedTextEditor
bookmark={bookmark}
open={isTextEditorOpen}
Expand Down Expand Up @@ -240,9 +239,7 @@ export default function BookmarkOptions({ bookmark }: { bookmark: ZBookmark }) {
<DropdownMenuItem
disabled={demoMode}
className="text-destructive"
onClick={() =>
deleteBookmarkMutator.mutate({ bookmarkId: bookmark.id })
}
onClick={() => setDeleteBookmarkDialogOpen(true)}
>
<Trash2 className="mr-2 size-4" />
<span>{t("actions.delete")}</span>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { usePathname, useRouter } from "next/navigation";
import { ActionButton } from "@/components/ui/action-button";
import ActionConfirmingDialog from "@/components/ui/action-confirming-dialog";
import { toast } from "@/components/ui/use-toast";
import { useTranslation } from "@/lib/i18n/client";

import { useDeleteBookmark } from "@hoarder/shared-react/hooks//bookmarks";
import { ZBookmark } from "@hoarder/shared/types/bookmarks";

export default function DeleteBookmarkConfirmationDialog({
bookmark,
children,
open,
setOpen,
}: {
bookmark: ZBookmark;
children?: React.ReactNode;
open: boolean;
setOpen: (v: boolean) => void;
}) {
const { t } = useTranslation();
const currentPath = usePathname();
const router = useRouter();

const { mutate: deleteBoomark, isPending } = useDeleteBookmark({
onSuccess: () => {
toast({
description: t("toasts.bookmarks.deleted"),
});
setOpen(false);
if (currentPath.includes(bookmark.id)) {
router.push("/dashboard/bookmarks");
}
},
onError: () => {
toast({
variant: "destructive",
description: `Something went wrong`,
});
},
});

return (
<ActionConfirmingDialog
open={open}
setOpen={setOpen}
title={t("dialogs.bookmarks.delete_confirmation_title")}
description={t("dialogs.bookmarks.delete_confirmation_description")}
actionButton={() => (
<ActionButton
type="button"
variant="destructive"
loading={isPending}
onClick={() => deleteBoomark({ bookmarkId: bookmark.id })}
>
Delete
</ActionButton>
)}
>
{children}
</ActionConfirmingDialog>
);
}
37 changes: 15 additions & 22 deletions apps/web/components/dashboard/preview/ActionBar.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useRouter } from "next/navigation";
import { useState } from "react";
import { ActionButton } from "@/components/ui/action-button";
import { Button } from "@/components/ui/button";
import {
Tooltip,
TooltipContent,
Expand All @@ -10,16 +11,16 @@ import { useTranslation } from "@/lib/i18n/client";
import { Trash2 } from "lucide-react";

import type { ZBookmark } from "@hoarder/shared/types/bookmarks";
import {
useDeleteBookmark,
useUpdateBookmark,
} from "@hoarder/shared-react/hooks/bookmarks";
import { useUpdateBookmark } from "@hoarder/shared-react/hooks/bookmarks";

import DeleteBookmarkConfirmationDialog from "../bookmarks/DeleteBookmarkConfirmationDialog";
import { ArchivedActionIcon, FavouritedActionIcon } from "../bookmarks/icons";

export default function ActionBar({ bookmark }: { bookmark: ZBookmark }) {
const { t } = useTranslation();
const router = useRouter();
const [deleteBookmarkDialogOpen, setDeleteBookmarkDialogOpen] =
useState(false);

const onError = () => {
toast({
variant: "destructive",
Expand All @@ -44,16 +45,6 @@ export default function ActionBar({ bookmark }: { bookmark: ZBookmark }) {
},
onError,
});
const { mutate: deleteBookmark, isPending: pendingDeletion } =
useDeleteBookmark({
onSuccess: () => {
toast({
description: "The bookmark has been deleted!",
});
router.back();
},
onError,
});

return (
<div className="flex items-center justify-center gap-3">
Expand Down Expand Up @@ -100,17 +91,19 @@ export default function ActionBar({ bookmark }: { bookmark: ZBookmark }) {
</TooltipContent>
</Tooltip>
<Tooltip delayDuration={0}>
<DeleteBookmarkConfirmationDialog
bookmark={bookmark}
open={deleteBookmarkDialogOpen}
setOpen={setDeleteBookmarkDialogOpen}
/>
<TooltipTrigger asChild>
<ActionButton
loading={pendingDeletion}
<Button
className="size-14 rounded-full bg-background"
variant="none"
onClick={() => {
deleteBookmark({ bookmarkId: bookmark.id });
}}
onClick={() => setDeleteBookmarkDialogOpen(true)}
>
<Trash2 />
</ActionButton>
</Button>
</TooltipTrigger>
<TooltipContent side="bottom">{t("actions.delete")}</TooltipContent>
</Tooltip>
Expand Down
6 changes: 6 additions & 0 deletions apps/web/lib/i18n/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,12 @@
}
}
},
"dialogs": {
"bookmarks": {
"delete_confirmation_title": "Delete Bookmark?",
"delete_confirmation_description": "Are you sure you want to delete this bookmark?"
}
},
"toasts": {
"bookmarks": {
"updated": "The bookmark has been updated!",
Expand Down
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f476fca

Please sign in to comment.