// PrefixSuffixTable.tsx import React, { useState } from "react"; import { Button } from "@/shared/common/ui/button"; import { Pencil, Trash2, X } from "lucide-react"; import { t } from "i18next"; import { Input } from "@/shared/common/ui/input"; import { Label } from "@/shared/common/ui/label"; import { prefixSuffixService } from "@/user-management/services/api/prefixSuffixService"; import { useQueryClient } from "@tanstack/react-query"; interface PrefixSuffixTableProps { items: any[]; isLoading: boolean; onEdit: (item: any) => void; onDelete: (id: string, type: string, recordType: string) => void; showPositionColumn?: boolean; showCountColumn?: boolean; positions?: any[]; } export const PrefixSuffixTable = ({ items, isLoading, onEdit, onDelete, showPositionColumn = false, showCountColumn = false, positions = [], }: PrefixSuffixTableProps) => { const queryClient = useQueryClient(); const getPositionName = (positionId: string) => { const pos = positions.find((p) => p.id === positionId); return pos?.name?.en || pos?.name?.am || positionId; }; const [countPopup, setCountPopup] = useState<{ itemId: string; currentCount: number; } | null>(null); const [countValue, setCountValue] = useState(""); // 1. Helper to get count from either source const getCount = (item: any): number | undefined => { return item.recordSequences?.[0]?.count ?? item.count ?? undefined; }; // 2. Helper to get sequence ID from either source const getSequenceId = (item: any): string | undefined => { return item.recordSequences?.[0]?.id ?? item.sequenceId ?? undefined; }; // 3. Updated openCountPopup const openCountPopup = (item: any) => { const currentCount = getCount(item) ?? 0; const sequenceId = getSequenceId(item); if (!sequenceId) { console.error("No sequence ID found for item:", item); return; } setCountPopup({ itemId: sequenceId, currentCount }); setCountValue(String(currentCount)); }; const closeCountPopup = () => { setCountPopup(null); setCountValue(""); queryClient.invalidateQueries({ queryKey: ["prefixes-by-position"], }); queryClient.invalidateQueries({ queryKey: ["unit-reference-numbers"], }); }; const handleCountSubmit = (e: React.FormEvent) => { e.preventDefault(); if (!countPopup) return; const newCount = parseInt(countValue, 10); if (isNaN(newCount) || newCount < 0) return; prefixSuffixService.updateCount(countPopup.itemId, newCount).then(() => { closeCountPopup(); }); }; return (
{showPositionColumn && ( )} {showCountColumn && ( )} {isLoading ? ( ) : items.length === 0 ? ( ) : ( items.map((item, idx) => { return ( {showPositionColumn && ( )} {showCountColumn && ( )} ); }) )}
# {t("header.amharic")} {t("header.english")} {t("contentManagement.type")} {t("contentManagement.position")} {t("contentManagement.count")} {t("userRecord.Actions")}
{t("contentManagement.loading")}
{t("contentManagement.noRec")}
{idx + 1} {item.name?.am || "-"} {item.name?.en || "-"} {item.type} {item.isForCC && ( (CC) )} {item.positionId ? getPositionName(item.positionId) : "-"} {(() => { const count = getCount(item); if (count === undefined) return "-"; return ( ); })()}
{/* Count Update Popup */} {countPopup && (

{t("contentManagement.updateCount")}

setCountValue(e.target.value)} placeholder={t("contentManagement.enterValue")} className="dark:bg-gray-700 dark:border-gray-600 dark:text-white" />

{t("contentManagement.currentCount")}:{" "} {countPopup.currentCount}

)}
); };