Sadum1's picture
Update frontend/src/components/ProtocolForm.tsx
3ad90ac verified
Raw History Blame Contribute Delete
8.05 kB
import { useEffect, useState, type FormEvent } from "react";
import {
ALL_SOURCES,
type LiteratureSource,
type ProtocolDraft,
type ReviewOptions,
type ReviewType,
} from "../types";
import { getReviewOptions } from "../api/protocols";
import { DomainMultiSelect } from "./DomainMultiSelect";
import { CriteriaListEditor } from "./CriteriaListEditor";
import { SearchStrategyEditor } from "./SearchStrategyEditor";
import { ReviewModeSelector } from "./ReviewModeSelector";
import { RobSettings } from "./RobSettings";
interface ProtocolFormProps {
onSubmit: (draft: ProtocolDraft) => Promise<void>;
onCancel: () => void;
}
const emptyDraft: ProtocolDraft = {
title: "",
population: "",
review_type: null,
review_domain: [],
intervention: "",
comparator: "",
outcome: "",
inclusion_criteria: [],
exclusion_criteria: [],
intervention_terms: [],
outcome_terms: [],
sources: [],
reviewers_required: 1,
rob_key_questions_observational: [4, 8, 9],
rob_key_questions_experimental: [8, 9],
rob_criteria: null,
};
export function ProtocolForm({ onSubmit, onCancel }: ProtocolFormProps) {
const [draft, setDraft] = useState<ProtocolDraft>(emptyDraft);
const [submitting, setSubmitting] = useState(false);
const [error, setError] = useState<string | null>(null);
const [options, setOptions] = useState<ReviewOptions | null>(null);
useEffect(() => {
let cancelled = false;
getReviewOptions()
.then((o) => {
if (!cancelled) setOptions(o);
})
.catch(() => {
if (!cancelled) setError("Could not load review type and domain options.");
});
return () => {
cancelled = true;
};
}, []);
function toggleSource(source: LiteratureSource) {
setDraft((d) => ({
...d,
sources: d.sources.includes(source)
? d.sources.filter((s) => s !== source)
: [...d.sources, source],
}));
}
// --- "All" checkbox for Literature sources ---
const allSourceValues = ALL_SOURCES.map((s) => s.value);
const allSourcesSelected = allSourceValues.every((v) => draft.sources.includes(v));
const someSourcesSelected = draft.sources.length > 0 && !allSourcesSelected;
function toggleAllSources(checked: boolean) {
setDraft((d) => ({ ...d, sources: checked ? allSourceValues : [] }));
}
async function handleSubmit(e: FormEvent) {
e.preventDefault();
setError(null);
if (!draft.review_type || draft.review_domain.length === 0) {
setError("Choose a review type and a review domain.");
return;
}
if (draft.sources.length === 0) {
setError("Select at least one literature source.");
return;
}
if (!draft.rob_key_questions_observational.length || !draft.rob_key_questions_experimental.length) {
setError("Choose at least one key risk-of-bias question for each study type.");
return;
}
setSubmitting(true);
try {
await onSubmit({
...draft,
comparator: draft.comparator || null,
rob_criteria: draft.rob_criteria?.trim() ? draft.rob_criteria : null,
});
} catch (err) {
setError(err instanceof Error ? err.message : "Failed to save protocol.");
} finally {
setSubmitting(false);
}
}
return (
<form className="protocol-form" onSubmit={handleSubmit}>
<h2>New review protocol</h2>
<p className="form-hint">
Declare your research question, criteria, and search strategy before
searching — this is Phase 1 of the PRISMA workflow. Once locked, these
fields can't be changed, so the search that follows can't be biased by
early results.
</p>
<label className="field-label">Title</label>
<input
type="text"
value={draft.title}
onChange={(e) => setDraft({ ...draft, title: e.target.value })}
placeholder="e.g. Hepatotoxicity of osimertinib in EGFR-mutant NSCLC"
required
/>
<div className="pico-grid">
<div>
<label className="field-label">Review type</label>
<select
value={draft.review_type ?? ""}
onChange={(e) =>
setDraft({ ...draft, review_type: (e.target.value || null) as ReviewType | null })
}
required
>
<option value="">Choose review type</option>
{options?.review_types.map((o) => (
<option key={o.value} value={o.value}>
{o.label}
</option>
))}
</select>
</div>
<div>
<label className="field-label">Review domain(s)</label>
<DomainMultiSelect
options={options?.review_domains ?? []}
value={draft.review_domain}
onChange={(v) => setDraft({ ...draft, review_domain: v })}
/>
</div>
</div>
<div className="pico-grid">
<div>
<label className="field-label">Population</label>
<textarea
value={draft.population}
onChange={(e) => setDraft({ ...draft, population: e.target.value })}
placeholder="Who or what is being studied?"
required
/>
</div>
<div>
<label className="field-label">Intervention / Exposure</label>
<textarea
value={draft.intervention}
onChange={(e) => setDraft({ ...draft, intervention: e.target.value })}
placeholder="The compound, drug, or exposure of interest"
required
/>
</div>
<div>
<label className="field-label">Comparator (optional)</label>
<textarea
value={draft.comparator ?? ""}
onChange={(e) => setDraft({ ...draft, comparator: e.target.value })}
placeholder="Comparison condition, if any"
/>
</div>
<div>
<label className="field-label">Outcome</label>
<textarea
value={draft.outcome}
onChange={(e) => setDraft({ ...draft, outcome: e.target.value })}
placeholder="The endpoint or outcome of interest"
required
/>
</div>
</div>
<SearchStrategyEditor draft={draft} onChange={setDraft} />
<CriteriaListEditor
label="Inclusion criteria"
items={draft.inclusion_criteria}
onChange={(items) => setDraft({ ...draft, inclusion_criteria: items })}
/>
<CriteriaListEditor
label="Exclusion criteria"
items={draft.exclusion_criteria}
onChange={(items) => setDraft({ ...draft, exclusion_criteria: items })}
/>
<label className="field-label">Literature sources</label>
<div className="source-checkboxes">
<label className="source-checkbox">
<input
type="checkbox"
checked={allSourcesSelected}
ref={(el) => {
if (el) el.indeterminate = someSourcesSelected;
}}
onChange={(e) => toggleAllSources(e.target.checked)}
/>
All
</label>
{ALL_SOURCES.map((s) => (
<label key={s.value} className="source-checkbox">
<input
type="checkbox"
checked={draft.sources.includes(s.value)}
onChange={() => toggleSource(s.value)}
/>
{s.label}
</label>
))}
</div>
<ReviewModeSelector
value={draft.reviewers_required}
onChange={(value) => setDraft({ ...draft, reviewers_required: value })}
/>
<RobSettings draft={draft} onChange={setDraft} />
{error && <div className="form-error">{error}</div>}
<div className="form-actions">
<button type="button" className="secondary-btn" onClick={onCancel}>
Cancel
</button>
<button type="submit" className="primary-btn" disabled={submitting}>
{submitting ? "Saving…" : "Save draft"}
</button>
</div>
</form>
);
}