128 lines
4.7 KiB
TypeScript
128 lines
4.7 KiB
TypeScript
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
import { NavLink, Route, Routes, useParams } from "react-router-dom";
|
|
import { api } from "../api";
|
|
import { EmptyState, ErrorState, LoadingState } from "../components/State";
|
|
import { CandidateList } from "./CandidateList";
|
|
|
|
export function ProjectDetailPage() {
|
|
const { projectId = "" } = useParams();
|
|
const project = useQuery({ queryKey: ["project", projectId], queryFn: () => api.project(projectId) });
|
|
if (project.isLoading) return <LoadingState />;
|
|
if (project.isError) return <ErrorState message={(project.error as Error).message} />;
|
|
const data = project.data!;
|
|
return (
|
|
<section>
|
|
<h1>{data.name}</h1>
|
|
<p className="muted">
|
|
{data.product_category_id} · {data.market}/{data.currency} · Version {data.criteria_version}
|
|
</p>
|
|
<div className="tabs">
|
|
<NavLink to={`/projects/${projectId}`} end>
|
|
Übersicht
|
|
</NavLink>
|
|
<NavLink to={`/projects/${projectId}/candidates`}>Kandidaten</NavLink>
|
|
<NavLink to={`/projects/${projectId}/shortlist`}>Shortlist</NavLink>
|
|
<NavLink to={`/projects/${projectId}/criteria`}>Kriterien</NavLink>
|
|
<NavLink to={`/projects/${projectId}/history`}>Historie</NavLink>
|
|
</div>
|
|
<Routes>
|
|
<Route index element={<Overview projectId={projectId} />} />
|
|
<Route path="candidates" element={<CandidateList projectId={projectId} />} />
|
|
<Route path="shortlist" element={<CandidateList projectId={projectId} shortlist />} />
|
|
<Route path="criteria" element={<CriteriaPanel projectId={projectId} />} />
|
|
<Route path="history" element={<HistoryPanel projectId={projectId} />} />
|
|
</Routes>
|
|
</section>
|
|
);
|
|
}
|
|
|
|
function Overview({ projectId }: { projectId: string }) {
|
|
const searches = useQuery({ queryKey: ["searches", projectId], queryFn: () => api.searches(projectId) });
|
|
const run = useMutation({
|
|
mutationFn: async (searchId: string) => {
|
|
await api.generateQuery(searchId);
|
|
return api.runSearch(searchId);
|
|
},
|
|
});
|
|
if (searches.isLoading) return <LoadingState />;
|
|
const items = searches.data?.items || [];
|
|
if (!items.length) return <EmptyState>Noch kein SearchRun ausgeführt.</EmptyState>;
|
|
return (
|
|
<div className="grid">
|
|
{items.map((search) => (
|
|
<div className="card" key={search.id}>
|
|
<p>Status: {search.status}</p>
|
|
{search.status === "OUT_OF_SYNC" ? <p>Suchparameter wurden geändert. Die externe Suche muss aktualisiert werden.</p> : null}
|
|
<button className="button" onClick={() => run.mutate(search.id)}>
|
|
Suche aktualisieren
|
|
</button>
|
|
</div>
|
|
))}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function CriteriaPanel({ projectId }: { projectId: string }) {
|
|
const client = useQueryClient();
|
|
const project = useQuery({ queryKey: ["project", projectId], queryFn: () => api.project(projectId) });
|
|
const save = useMutation({
|
|
mutationFn: () => api.putCriteria(projectId, { items: project.data?.criteria?.items || [] }),
|
|
onSuccess: () => client.invalidateQueries({ queryKey: ["project", projectId] }),
|
|
});
|
|
if (project.isLoading) return <LoadingState />;
|
|
const items = project.data?.criteria?.items || [];
|
|
return (
|
|
<div className="card grid">
|
|
{items.map((item) => (
|
|
<p key={item.id}>
|
|
{item.criterion || item.criterion_definition_id} {item.operator} {String(item.value)}
|
|
</p>
|
|
))}
|
|
<button className="button secondary" onClick={() => save.mutate()}>
|
|
Kriterienversion fortschreiben
|
|
</button>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function HistoryPanel({ projectId }: { projectId: string }) {
|
|
const searches = useQuery({ queryKey: ["searches", projectId], queryFn: () => api.searches(projectId) });
|
|
const searchId = searches.data?.items[0]?.id;
|
|
const runs = useQuery({
|
|
queryKey: ["runs", searchId],
|
|
queryFn: () => api.runs(searchId!),
|
|
enabled: Boolean(searchId),
|
|
});
|
|
if (!searchId) return <EmptyState>Noch kein SearchRun ausgeführt.</EmptyState>;
|
|
if (runs.isLoading) return <LoadingState />;
|
|
const items = runs.data?.items || [];
|
|
return (
|
|
<div className="card">
|
|
<table>
|
|
<thead>
|
|
<tr>
|
|
<th>Zeit</th>
|
|
<th>Status</th>
|
|
<th>Treffer</th>
|
|
<th>NEW</th>
|
|
<th>CHANGED</th>
|
|
<th>MISSING</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{items.map((run) => (
|
|
<tr key={run.id}>
|
|
<td>{run.started_at}</td>
|
|
<td>{run.status}</td>
|
|
<td>{run.result_count}</td>
|
|
<td>{run.new_count}</td>
|
|
<td>{run.changed_count}</td>
|
|
<td>{run.missing_count}</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
);
|
|
}
|