"use client"; import { LoaderCircleIcon, Trash2Icon } from "lucide-react"; import { useTranslations } from "next-intl"; import { useState } from "react"; import { errorToast } from "../../../../components"; import { Modules } from "../../../../core"; import { usePageUrlGenerator } from "../../../../hooks"; import { Action } from "../../../../permissions"; import { getRoleId } from "../../../../roles"; import { AlertDialog, AlertDialogContent, AlertDialogDescription, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger, Button, Input, Label, } from "../../../../shadcnui"; import { AuthService } from "../../../auth/data"; import { UserInterface } from "../../../user"; import { useCurrentUserContext } from "../../../user/contexts"; import { CompanyInterface } from "../../data"; import { CompanyService } from "../../data/company.service"; type CompanyDeleterProps = { company: CompanyInterface; }; type CompanyDeleterInternalProps = CompanyDeleterProps & { isAdministrator: boolean; }; function CompanyDeleterInternal({ company, isAdministrator }: CompanyDeleterInternalProps) { const t = useTranslations(); const generateUrl = usePageUrlGenerator(); const [open, setOpen] = useState(false); const [finalWarningOpen, setFinalWarningOpen] = useState(false); const [isDeleting, setIsDeleting] = useState(false); const [companyName, setCompanyName] = useState(""); const handleProceedToFinalWarning = () => { setOpen(false); setFinalWarningOpen(true); }; const handleFinalDelete = async () => { setIsDeleting(true); try { if (isAdministrator) { await CompanyService.delete({ companyId: company.id }); } else { await CompanyService.selfDelete({ companyId: company.id }); } await AuthService.logout(); window.location.href = generateUrl({ page: `/` }); } catch (error) { errorToast({ title: t(`common.errors.delete`), error: error }); setIsDeleting(false); } }; const handleGoBack = () => { setFinalWarningOpen(false); setCompanyName(""); }; return ( <> {/* Dialog 1: Company Name Confirmation */} {t(`common.delete.title`, { type: t(`entities.companies`, { count: 1 }) })} {t(`common.delete.subtitle`, { type: t(`entities.companies`, { count: 1 }) })}
{t(`common.delete.description`, { type: t(`entities.companies`, { count: 1 }) })}
{t(`common.delete.confirmation`, { type: t(`entities.companies`, { count: 1 }) })}
setCompanyName(e.target.value)} />
{/* Dialog 2: Final Warning */} {t(`common.delete.finalWarning.title`)} {t(`common.delete.finalWarning.subtitle`)}

{t(`common.delete.finalWarning.description`)}

  • {t(`common.delete.finalWarning.bullet1`)}
  • {t(`common.delete.finalWarning.bullet2`)}
  • {t(`common.delete.finalWarning.bullet3`)}
  • {t(`common.delete.finalWarning.bullet4`)}
  • {t(`common.delete.finalWarning.bullet5`)}
); } export function CompanyDeleter({ company }: CompanyDeleterProps) { const { hasPermissionToModule, hasRole } = useCurrentUserContext(); const isAdministrator = hasRole(getRoleId().Administrator); const isCompanyAdministrator = hasRole(getRoleId().CompanyAdministrator); const hasDeletePermission = hasPermissionToModule({ module: Modules.Company, action: Action.Delete }); if (!isAdministrator && !isCompanyAdministrator && !hasDeletePermission) { return null; } return ; }