Skip to content

Commit

Permalink
feature(web): Add an archive button to list management dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedBassem committed May 19, 2024
1 parent bfcf0a4 commit f99f4c0
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
10 changes: 9 additions & 1 deletion apps/web/components/dashboard/bookmarks/ManageListsModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
import { toast } from "@/components/ui/use-toast";
import { api } from "@/lib/trpc";
import { zodResolver } from "@hookform/resolvers/zod";
import { X } from "lucide-react";
import { Archive, X } from "lucide-react";
import { useForm } from "react-hook-form";
import { z } from "zod";

Expand All @@ -30,6 +30,7 @@ import {
} from "@hoarder/shared-react/hooks/lists";

import { BookmarkListSelector } from "../lists/BookmarkListSelector";
import ArchiveBookmarkButton from "./action-buttons/ArchiveBookmarkButton";

export default function ManageListsModal({
bookmarkId,
Expand Down Expand Up @@ -179,6 +180,13 @@ export default function ManageListsModal({
Close
</Button>
</DialogClose>
<ArchiveBookmarkButton
type="button"
bookmarkId={bookmarkId}
onDone={() => setOpen(false)}
>
<Archive className="mr-2 size-4" /> Archive
</ArchiveBookmarkButton>
<ActionButton
type="submit"
loading={isAddingToListPending}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import React from "react";
import { ActionButton, ActionButtonProps } from "@/components/ui/action-button";
import { toast } from "@/components/ui/use-toast";
import { api } from "@/lib/trpc";

import { useUpdateBookmark } from "@hoarder/shared-react/hooks/bookmarks";

interface ArchiveBookmarkButtonProps
extends Omit<ActionButtonProps, "loading" | "disabled"> {
bookmarkId: string;
onDone?: () => void;
}

const ArchiveBookmarkButton = React.forwardRef<
HTMLButtonElement,
ArchiveBookmarkButtonProps
>(({ bookmarkId, onDone, ...props }, ref) => {
const { data } = api.bookmarks.getBookmark.useQuery({ bookmarkId });

const { mutate: updateBookmark, isPending: isArchivingBookmark } =
useUpdateBookmark({
onSuccess: () => {
toast({
description: "Bookmark has been archived!",
});
onDone?.();
},
onError: (e) => {
if (e.data?.code == "BAD_REQUEST") {
toast({
variant: "destructive",
description: e.message,
});
} else {
toast({
variant: "destructive",
title: "Something went wrong",
});
}
},
});

if (!data) {
return <span />;
}

return (
<ActionButton
ref={ref}
loading={isArchivingBookmark}
disabled={data.archived}
onClick={() =>
updateBookmark({
bookmarkId,
archived: !data.archived,
})
}
{...props}
/>
);
});

ArchiveBookmarkButton.displayName = "ArchiveBookmarkButton";
export default ArchiveBookmarkButton;
1 change: 1 addition & 0 deletions apps/web/components/ui/action-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,4 @@ const ActionButtonWithTooltip = React.forwardRef<
ActionButtonWithTooltip.displayName = "ActionButtonWithTooltip";

export { ActionButton, ActionButtonWithTooltip };
export type { ActionButtonProps };

0 comments on commit f99f4c0

Please sign in to comment.