import ArrowBackIcon from "@mui/icons-material/ArrowBack"; import { Box, Button, Dialog, DialogActions, DialogContent, DialogContentText, DialogTitle, IconButton, Link, Paper, Radio, Table, TableBody, TableCell, TableContainer, TableHead, TableRow, TextField, Typography, } from "@mui/material"; import { useMutation, useQueryClient, useSuspenseQuery, } from "@tanstack/react-query"; import { createFileRoute, useNavigate } from "@tanstack/react-router"; import { useEffect, useState } from "react"; import PDFViewer from "../components/pdfViewer"; import { fetchPutKPI } from "../util/api"; import { kpiQueryOptions } from "../util/query"; import { redirect } from "@tanstack/react-router"; export const Route = createFileRoute("/extractedResult_/$pitchBook/$kpi")({ component: ExtractedResultsPage, validateSearch: (search: Record) => { return { from: typeof search.from === "string" ? search.from : undefined, }; }, loader: async ({ context: { queryClient }, params: { pitchBook } }) => { try { return await queryClient.ensureQueryData(kpiQueryOptions(pitchBook)); } catch (err) { throw redirect({ to: "/" }); } }, }); function ExtractedResultsPage() { const params = Route.useParams() as { pitchBook: string; kpi: string }; const { pitchBook, kpi } = params; const navigate = useNavigate(); const queryClient = useQueryClient(); const { from } = Route.useSearch(); const { data: kpiData } = useSuspenseQuery(kpiQueryOptions(pitchBook)); const kpiValues = kpiData[kpi.toUpperCase()] || []; const [selectedIndex, setSelectedIndex] = useState(0); const [currentPage, setCurrentPage] = useState(kpiValues[0]?.page || 1); const [showConfirmDialog, setShowConfirmDialog] = useState(false); const [hasChanges, setHasChanges] = useState(false); const [customValue, setCustomValue] = useState(""); const originalValue = kpiValues[0]?.entity || ""; const selectedValue = selectedIndex === -1 ? customValue : kpiValues[selectedIndex]?.entity || ""; useEffect(() => { setHasChanges(selectedValue !== originalValue); }, [selectedValue, originalValue]); const { mutate: updateKPI } = useMutation({ mutationFn: () => { const updatedData = { ...kpiData }; let baseObject; if (selectedIndex >= 0) { baseObject = kpiValues[selectedIndex]; } else { baseObject = { label: kpi.toUpperCase(), entity: selectedValue, page: 0, status: "single-source", source: "manual", }; } updatedData[kpi.toUpperCase()] = [ { ...baseObject, entity: selectedValue, }, ]; return fetchPutKPI(Number(pitchBook), updatedData); }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["pitchBookKPI", pitchBook], }); navigate({ to: "/extractedResult/$pitchBook", params: { pitchBook }, search: from ? { from } : undefined }); }, onError: (error) => { console.error("Error updating KPI:", error); }, }); const handleRadioChange = (event: React.ChangeEvent) => { const value = event.target.value; if (value === "custom") { setSelectedIndex(-1); } else { const index = Number.parseInt(value); setSelectedIndex(index); setCurrentPage(kpiValues[index].page); setCustomValue(""); } }; const handleCustomValueChange = ( event: React.ChangeEvent, ) => { const value = event.target.value; setCustomValue(value); setSelectedIndex(-1); }; const handleRowClick = (index: number) => { setCurrentPage(kpiValues[index].page); setSelectedIndex(index); setCustomValue(""); }; const handleBackClick = () => { if (hasChanges) { setShowConfirmDialog(true); } else { navigate({ to: "/extractedResult/$pitchBook", params: { pitchBook }, search: from ? { from } : undefined }); } }; const handleConfirmDiscard = () => { setShowConfirmDialog(false); navigate({ to: "/extractedResult/$pitchBook", params: { pitchBook }, search: from ? { from } : undefined }); }; const handleCancelDiscard = () => { setShowConfirmDialog(false); }; const handleAcceptReview = () => { updateKPI(); }; return ( Überprüfung der Kennzahl: {kpi} Gefundene Werte Seite {kpiValues.map((item, index) => ( handleRowClick(index)} > {item.entity} { e.stopPropagation(); setCurrentPage(item.page); }} sx={{ cursor: "pointer" }} > {item.page} ))} { setSelectedIndex(-1); }} > { e.stopPropagation(); }} />
({ page: k.page, text: k.entity }))} focusHighlight={{ page: kpiValues.at(selectedIndex)?.page || -1, text: kpiValues.at(selectedIndex)?.entity || "", }} />
Achtung Alle vorgenommenen Änderungen werden verworfen.
); }