133 lines
5.7 KiB
TypeScript
133 lines
5.7 KiB
TypeScript
// * Packages
|
|
import { Link, useForm, usePage } from "@inertiajs/react";
|
|
// * Components shadcn
|
|
import { Button } from "@/components/ui/button";
|
|
import { Badge } from "@/components/ui/badge";
|
|
import { DropdownMenuSeparator } from "@/components/ui/dropdown-menu";
|
|
import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog";
|
|
// * Other
|
|
import { getFormatDate } from "@/lib/utils";
|
|
import { useToast } from "@/hooks/use-toast";
|
|
import trans from "@/hooks/use-translations";
|
|
import str from "@/hooks/use-string";
|
|
import { Model, Views } from "@/back/types/global";
|
|
|
|
export function ModelActions<TData, TValue>({
|
|
actions,
|
|
modelName,
|
|
model,
|
|
modelUpdatedAt = "",
|
|
inColumn = false,
|
|
}: {
|
|
actions: Views;
|
|
modelName: string;
|
|
model: Model;
|
|
modelUpdatedAt?: string;
|
|
inColumn?: boolean;
|
|
}) {
|
|
const { toast } = useToast();
|
|
const { delete: destroy } = useForm();
|
|
const deleteUser = (e: any) => {
|
|
e.preventDefault();
|
|
|
|
destroy(route(`back.${modelName}.destroy`, model.id), {
|
|
preserveScroll: true,
|
|
onSuccess: () => {
|
|
toast({
|
|
title: "Suppression réussie !",
|
|
description: "Vous venez de supprimer définitivement une entrée.",
|
|
});
|
|
},
|
|
onError: () => {
|
|
toast({
|
|
variant: "destructive",
|
|
title: "Suppression annulée !",
|
|
description: "Un problème est survenue lors de la suppression de cet entrée.",
|
|
});
|
|
},
|
|
});
|
|
};
|
|
return (
|
|
<div
|
|
className={
|
|
"flex justify-between items-center " +
|
|
(!inColumn ? "flex-col xl:flex-row mb-4" : "flex-col")
|
|
}
|
|
>
|
|
{!inColumn && modelUpdatedAt.length && (
|
|
<div className="flex justify-left items-center">
|
|
<p className="text-center xl:text-left">
|
|
{trans("back.model_action_updated_at")}{" "}
|
|
</p>
|
|
<Badge variant="secondary" className="ms-1 mt-1">
|
|
{getFormatDate(modelUpdatedAt)}
|
|
</Badge>
|
|
</div>
|
|
)}
|
|
<div
|
|
className={
|
|
"flex justify-between items-center " +
|
|
(!inColumn
|
|
? "flex-col sm:flex-row mt-4 xl:mt-0"
|
|
: "flex-col w-full")
|
|
}
|
|
>
|
|
{actions.map((action) => model.policies[action.policy] && (
|
|
<div key={action.value} className="w-full">
|
|
{action.value === "destroy" ? (
|
|
<>
|
|
{inColumn && <DropdownMenuSeparator />}
|
|
<Dialog>
|
|
<DialogTrigger asChild>
|
|
<Button variant="destructive">
|
|
<action.icon />
|
|
{str(trans("crud.actions." + action.label)).ucFirst().value()}
|
|
</Button>
|
|
</DialogTrigger>
|
|
<DialogContent>
|
|
<DialogHeader>
|
|
<DialogTitle>
|
|
{trans('back.are_you_sure')}
|
|
</DialogTitle>
|
|
<DialogDescription>
|
|
{trans('back.lost_data')}
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
<DialogFooter className="items-center sm:justify-start">
|
|
<DialogClose asChild>
|
|
<Button variant="secondary">
|
|
{trans('back.no_cancel')}
|
|
</Button>
|
|
</DialogClose>
|
|
<form
|
|
onSubmit={deleteUser}
|
|
className="w-fit mb-2 sm:mb-0"
|
|
>
|
|
<Button
|
|
type="submit"
|
|
variant="destructive"
|
|
>
|
|
{trans('back.yes_delete')}
|
|
</Button>
|
|
</form>
|
|
</DialogFooter>
|
|
</DialogContent>
|
|
</Dialog>
|
|
</>
|
|
) : (
|
|
<Button asChild variant="ghost">
|
|
<Link
|
|
href={route(`back.${modelName}.${action.value}`, model.id)}
|
|
className="w-full"
|
|
>
|
|
<action.icon />
|
|
{str(trans("crud.actions." + action.label)).ucFirst().value()}
|
|
</Link>
|
|
</Button>
|
|
)}
|
|
</div>
|
|
))}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|