File size: 8,050 Bytes
ddcb8c1 ecff1a6 930e4a4 0fbfbf1 5091a04 6624dea 930e4a4 4743e61 ecff1a6 930e4a4 0fbfbf1 930e4a4 5091a04 6624dea 930e4a4 ddcb8c1 930e4a4 3ad90ac 930e4a4 ecff1a6 ddcb8c1 930e4a4 6624dea 930e4a4 6624dea 930e4a4 0fbfbf1 930e4a4 b9dce26 ecff1a6 b9dce26 ecff1a6 930e4a4 0fbfbf1 930e4a4 3ad90ac 930e4a4 5091a04 6624dea 5091a04 930e4a4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | 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>
);
}
|