Kiranism / next-shadcn-dashboard-starter

Admin Dashboard Starter with Nextjs14 and shadcn ui
https://next-shadcn-dashboard-starter.vercel.app
MIT License
1.43k stars 302 forks source link

How to delete row from databale #14

Closed binfask closed 2 months ago

binfask commented 4 months ago

in my case i deleted data from data base by calling API from onConfirm() but the data is not removing from data table

m0nx73r commented 4 months ago

Hey @binfask, please share your code too Check this

binfask commented 4 months ago

the code from my cellAction component :

"use client"; import { AlertModal } from "@/components/modal/alert-modal"; import { Button } from "@/components/ui/button"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuLabel, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { Car } from "@/constants/data"; import { deleteCar } from "@/lib/actions/car.actions"; import { Edit, MoreHorizontal, Trash } from "lucide-react"; import { useRouter } from 'next/navigation'; import { useState } from "react"; import { useToast } from "../../ui/use-toast"; import { refreshcarpage } from "./refresh";

interface CellActionProps { data: Car; }

export const CellAction: React.FC = ({ data }) => { const [loading, setLoading] = useState(false); const [open, setOpen] = useState(false); const router = useRouter(); const { toast } = useToast();

const handleNavigate = async () => { // Optional: Perform any actions before navigation await router.push('/admin/car'); };

const onConfirm = async () => { setLoading(true); try { await deleteCar(data._id); toast({ title: "Success", description: "Car deleted successfully", }); setOpen(false);

  router.prefetch(`/admin/car`);
} catch (error) {
  console.error("Error deleting car:", error);
  toast({
    title: "Error",
    description: "Error deleting car",
  });
} finally {
  setLoading(false);
}

};

return ( <> <AlertModal isOpen={open} onClose={() => setOpen(false)} onConfirm={onConfirm} loading={loading} />

Actions router.push(`/admin/car/${data._id}`)} > Update setOpen(true)}> Delete ); }; it is deleting data from database and nothing change on table
Kiranism commented 3 months ago

You need to revalidate the data or refetch it to obtain the latest updated data.

Kiranism commented 2 months ago

You need to revalidate the data or refetch it to obtain the latest updated data.

revalidatePath or router.refresh also works