import React, { useState } from "react"; import { Button } from "@/shared/common/ui/button"; import { Card } from "@/shared/common/ui/card"; import { Input } from "@/shared/common/ui/input"; import { Label } from "@radix-ui/react-label"; import { Plus, Trash2, Pencil } from "lucide-react"; import { prefixSuffixService, RemarkPayload, } from "@/user-management/services/api/prefixSuffixService"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger, } from "@/shared/common/ui/dialog"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "@/shared/common/ui/alert-dialog"; import { useMutation, useQueryClient } from "@tanstack/react-query"; import { useRemarkByUnitId } from "@/super-admin/hooks/useRemark"; import { t } from "i18next"; interface Props { unitId: string; } interface RemarkDto { id: string; remark: string; description?: string; } export const CommonRemarks = ({ unitId }: Props) => { const [remark, setRemark] = useState(""); const [description, setDescription] = useState(""); const [editingId, setEditingId] = useState(null); const [isDialogOpen, setIsDialogOpen] = useState(false); const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false); const [deleteRemarkId, setDeleteRemarkId] = useState(null); const [error, setError] = useState(""); const queryClient = useQueryClient(); const { data: remarksData, isLoading: isRemarkLoading } = useRemarkByUnitId(unitId,{ skip: 0, take: 300, }); // Save/Edit Remark const saveRemarkMutation = useMutation({ mutationFn: async () => { if (!remark) throw new Error(t("contentManagement.remarkRequired")); const payload: RemarkPayload = { unitId, remark, description }; if (editingId) { return prefixSuffixService.editRemark(editingId, payload); } return prefixSuffixService.createRemark(payload); }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["remark", unitId] }); resetForm(); setIsDialogOpen(false); }, onError: (err: any) => setError(err.message || t("contentManagement.failed")), }); // Delete Remark const deleteRemarkMutation = useMutation({ mutationFn: (id: string) => prefixSuffixService.deleteRemarks(id), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["remark", unitId] }); setIsDeleteDialogOpen(false); setDeleteRemarkId(null); }, onError: () => setError(t("contentManagement.failedToDelete")), }); const resetForm = () => { setRemark(""); setDescription(""); setEditingId(null); setError(""); }; const handleEdit = (item: RemarkDto) => { setEditingId(item.id); setRemark(item.remark); setDescription(item.description || ""); setIsDialogOpen(true); }; const handleSubmit = () => saveRemarkMutation.mutate(); return ( {/* Add/Edit Dialog */} {editingId ? t("contentManagement.editRemark") : t("contentManagement.addRemark")}
setRemark(e.target.value)} placeholder={t("header.Remark")} disabled={isRemarkLoading} className="w-full dark:bg-gray-700 dark:border-gray-600 dark:text-white" /> setDescription(e.target.value)} placeholder={t("header.Description")} disabled={isRemarkLoading} className="w-full mb-2 dark:bg-gray-700 dark:border-gray-600 dark:text-white" />
{error &&

{error}

}
{/* Remarks Table */}

{t("contentManagement.commonRemarks")}

{isRemarkLoading ? (
{t("contentManagement.loading")}
) : (
{remarksData?.items.length ? ( remarksData.items.map((item: any, idx: any) => ( )) ) : ( )}
# {t("header.Remark")} {t("header.Description")} {t("userRecord.Actions")}
{idx + 1} {item.remark} {item.description}
{t("contentManagement.noRec")}
)} { if (deleteRemarkMutation.isPending) return; setIsDeleteDialogOpen(open); if (!open) setDeleteRemarkId(null); }} > {t("common.delete")} {t("contentManagement.deleteMsg")} {t("common.cancel")} { if (!deleteRemarkId) return; deleteRemarkMutation.mutate(deleteRemarkId); }} > {deleteRemarkMutation.isPending ? t("common.deleting", "Deleting...") : t("common.delete")}
); };