import React, { useState } from "react"; import { Dialog, DialogContent, DialogHeader, DialogTitle, } from "@/shared/common/ui/dialog"; import { Button } from "@/shared/common/ui/button"; import { Badge } from "@/shared/common/ui/badge"; import { AlertCircle, Trash2, Briefcase } from "lucide-react"; import { t } from "i18next"; import { useTranslation } from "react-i18next"; import { toast } from "sonner"; import { deleteEmployeePosition } from "../../services/api/employeePositionsService"; import { useLocalizedName } from "@/shared/common/localizedName"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogHeader, AlertDialogTitle, } from "@/shared/common/ui/alert-dialog"; export interface EmployeePosition { id: string; employeeId: string; position: { id: string; name: { am: string; en: string; }; }; } interface EmployeePositionsDialogProps { isOpen: boolean; onClose: () => void; employeeName: string; positions: EmployeePosition[]; onPositionRemoved?: () => void; } export const EmployeePositionsDialog: React.FC< EmployeePositionsDialogProps > = ({ isOpen, onClose, employeeName, positions, onPositionRemoved }) => { const { t } = useTranslation(); const [loading, setLoading] = useState(false); const [positionToDelete, setPositionToDelete] = useState(null); const [showConfirmDialog, setShowConfirmDialog] = useState(false); const localizedName = useLocalizedName(); const handleRemovePosition = async (position: EmployeePosition) => { setLoading(true); try { await deleteEmployeePosition(position.id); toast.success(t("positions.positionRemoved"), { description: t("positions.employeeRemovedFromPosition", { position: localizedName(position.position.name), }), duration: 4000, }); setShowConfirmDialog(false); setPositionToDelete(null); // Call callback to refresh positions list if (onPositionRemoved) { onPositionRemoved(); } } catch (error: unknown) { const errorMessage = error instanceof Error ? error.message : "Unknown error"; // Check if it's a "Referenced Entity does not exist" error if (errorMessage.includes("Referenced Entity")) { toast.error(t("positions.positionAlreadyRemoved"), { description: t("positions.positionMayHaveBeenRemovedAlready"), duration: 4000, }); // Still close the dialog and refresh setShowConfirmDialog(false); setPositionToDelete(null); if (onPositionRemoved) { onPositionRemoved(); } } else { toast.error(t("positions.failedToRemovePosition"), { description: errorMessage, duration: 4000, }); } } finally { setLoading(false); } }; const handleOpenConfirm = (position: EmployeePosition) => { setPositionToDelete(position); setShowConfirmDialog(true); }; return ( <> {t("positions.employeePositions")}
{/* Employee Name */}

{t("positions.employee")}

{employeeName}

{/* Positions List */} {positions && positions.length > 0 ? (
{positions .filter((position) => position && position.position && position.position.name) .map((position) => (

{localizedName(position.position.name)}

{t("positions.positionId")}: {position.id.slice(0, 8)}...

))}
) : (

{t("positions.noPositions")}

{t("positions.employeeHasNoPositions")}

)}
{/* Confirmation Dialog for Deletion */} {t("positions.removeFromPosition")} {t("positions.confirmRemoveFromPosition", { employee: employeeName, position: positionToDelete ? localizedName(positionToDelete.position.name) : "", })}
{t("common.cancel")} positionToDelete && handleRemovePosition(positionToDelete) } disabled={loading} className="bg-red-600 hover:bg-red-700 dark:bg-red-700 dark:hover:bg-red-600"> {loading ? t("common.removing") : t("positions.remove")}
); };