import React, { useState } from "react"; import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; import { apiRequest } from "@/lib/queryClient"; import { Button } from "../ui/button"; import { Edit, Delete, Plus } from "lucide-react"; import { CredentialForm } from "./InsuranceCredForm"; import { DeleteConfirmationDialog } from "../ui/deleteDialog"; type Credential = { id: number; siteKey: string; username: string; password: string; }; // Map site keys to friendly labels const SITE_KEY_LABELS: Record = { MH: "MassHealth", DDMA: "Delta Dental MA", DELTAINS: "Delta Dental Ins", DENTAQUEST: "Tufts SCO / DentaQuest", UNITEDSCO: "United SCO", CCA: "CCA", }; function getSiteKeyLabel(siteKey: string): string { return SITE_KEY_LABELS[siteKey] || siteKey; } export function CredentialTable() { const queryClient = useQueryClient(); // Fetch current user const { data: currentUser, isLoading: isUserLoading, isError: isUserError, } = useQuery({ queryKey: ["/api/users/"], queryFn: async () => { const res = await apiRequest("GET", "/api/users/"); if (!res.ok) throw new Error("Failed to fetch user"); return res.json(); }, }); const [currentPage, setCurrentPage] = useState(1); const [modalOpen, setModalOpen] = useState(false); const [editingCred, setEditingCred] = useState(null); const credentialsPerPage = 5; const { data: credentials = [], isLoading, error } = useQuery({ queryKey: ["/api/insuranceCreds/"], queryFn: async () => { const res = await apiRequest("GET", "/api/insuranceCreds/"); if (!res.ok) throw new Error("Failed to fetch credentials"); return res.json() as Promise; }, }); const deleteMutation = useMutation({ mutationFn: async (cred: Credential) => { const res = await apiRequest("DELETE", `/api/insuranceCreds/${cred.id}`); if (!res.ok) throw new Error("Failed to delete credential"); return true; }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["/api/insuranceCreds/"] }); }, }); // New state for delete dialog const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false); const [credentialToDelete, setCredentialToDelete] = useState(null); const handleDeleteClick = (cred: Credential) => { setCredentialToDelete(cred); setIsDeleteDialogOpen(true); }; const handleConfirmDelete = () => { if (credentialToDelete) { deleteMutation.mutate(credentialToDelete, { onSuccess: () => { setIsDeleteDialogOpen(false); setCredentialToDelete(null); }, }); } }; const handleCancelDelete = () => { setIsDeleteDialogOpen(false); setCredentialToDelete(null); }; const indexOfLast = currentPage * credentialsPerPage; const indexOfFirst = indexOfLast - credentialsPerPage; const currentCredentials = credentials.slice(indexOfFirst, indexOfLast); const totalPages = Math.ceil(credentials.length / credentialsPerPage); if (isUserLoading) return

Loading user...

; if (isUserError) return

Error loading user

; return (

Insurance Credentials

{isLoading ? ( ) : error ? ( ) : currentCredentials.length === 0 ? ( ) : ( currentCredentials.map((cred) => ( )) )}
Provider Username Password
Loading credentials...
Error loading credentials
No credentials found.
{getSiteKeyLabel(cred.siteKey)} {cred.username} ••••••••
{/* Pagination */} {credentials.length > credentialsPerPage && (

Showing {indexOfFirst + 1} to{" "} {Math.min(indexOfLast, credentials.length)} of{" "} {credentials.length} results

)} {/* Modal for Add/Edit */} {modalOpen && currentUser && ( setModalOpen(false)} /> )}
); }