import { useState } from "react"; import { ActionButton } from "@/components/ui/action-button"; import { Button } from "@/components/ui/button"; import { Dialog, DialogClose, DialogContent, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Form, FormControl, FormField, FormItem, FormMessage, } from "@/components/ui/form"; import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import LoadingSpinner from "@/components/ui/spinner"; import { toast } from "@/components/ui/use-toast"; import { api } from "@/lib/trpc"; import { zodResolver } from "@hookform/resolvers/zod"; import { useForm } from "react-hook-form"; import { z } from "zod"; export default function AddToListModal({ bookmarkId, open, setOpen, }: { bookmarkId: string; open: boolean; setOpen: (open: boolean) => void; }) { const formSchema = z.object({ listId: z.string({ required_error: "Please select a list", }), }); const form = useForm>({ resolver: zodResolver(formSchema), }); const { data: lists, isPending: isFetchingListsPending } = api.lists.list.useQuery(); const bookmarksInvalidationFunction = api.useUtils().bookmarks.getBookmarks.invalidate; const { mutate: addToList, isPending: isAddingToListPending } = api.lists.addToList.useMutation({ onSuccess: (_resp, req) => { toast({ description: "List has been updated!", }); setOpen(false); bookmarksInvalidationFunction({ listId: req.listId }); }, onError: (e) => { if (e.data?.code == "BAD_REQUEST") { toast({ variant: "destructive", description: e.message, }); } else { toast({ variant: "destructive", title: "Something went wrong", }); } }, }); const isPending = isFetchingListsPending || isAddingToListPending; return (
{ addToList({ bookmarkId: bookmarkId, listId: value.listId, }); })} > Add to List
{lists ? ( { return ( ); }} /> ) : ( )}
Add
); } export function useAddToListModal(bookmarkId: string) { const [open, setOpen] = useState(false); return { open, setOpen, content: ( ), }; }