task-managment / src / app / dashboard / stock / _components / AlertDeleteMaterial.tsx
AlertDeleteMaterial.tsx
Raw
"use client";
import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
  AlertDialogTrigger,
} from "@/components/ui/alert-dialog";
import { Button } from "@/components/ui/button";
import { useState } from "react";

import { trpc } from "@/app/_trpc/client";
import { ProjectMaterials } from "../types";

const AlertDeleteMaterials = ({ selectedMaterials }:{selectedMaterials:ProjectMaterials[]}) => {

  const utils = trpc.useUtils()
  const [open, setOpen] = useState<boolean>(false);
  const{mutate:deleteMaterial,isPending}=trpc.materials.deleteMaterials.useMutation({
    onSuccess:()=>{
      setOpen(false);
      utils.projects.getAllProjectMaterials.invalidate()
    }
  })
  

  const handledeleteMaterial = () => {
    const materialIds=selectedMaterials.map(material=>material.id)
    deleteMaterial({materialIds})
  };

  return (
    <AlertDialog open={open} onOpenChange={setOpen}>
      <AlertDialogTrigger>
        <Button variant="destructive" size='sm' disabled={selectedMaterials.length <= 0}>
          Eliminar
        </Button>
      </AlertDialogTrigger>
      <AlertDialogContent>
        <AlertDialogHeader>
          <AlertDialogTitle>
            Seguro que desea eliminar este articulo?
          </AlertDialogTitle>
          <AlertDialogDescription>
            Esta accion no puede ser reversible, y será borrada permanentemente
            de este projecto
          </AlertDialogDescription>
        </AlertDialogHeader>
        <AlertDialogFooter>
          <AlertDialogCancel>Cancel</AlertDialogCancel>
          <Button
            variant="destructive"
            disabled={selectedMaterials.length <= 0 || isPending}
            onClick={() => handledeleteMaterial()}
          >
            Eliminar
          </Button>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  );
};

export default AlertDeleteMaterials;