adding weight edits

This commit is contained in:
Corey Blais
2026-06-16 09:31:16 -04:00
parent cc4a2382c6
commit f65a4bed24
3 changed files with 135 additions and 10 deletions
+58 -10
View File
@@ -1574,6 +1574,7 @@ function App() {
recordedOn: new Date().toISOString().slice(0, 10),
notes: '',
});
const [editingWeightId, setEditingWeightId] = useState('');
const [vetVisitForm, setVetVisitForm] = useState({
visitedOn: new Date().toISOString().slice(0, 10),
clinicName: '',
@@ -3355,8 +3356,9 @@ function App() {
setError('');
try {
const response = await apiFetch(`/birds/${selectedBird.id}/weights`, authToken, {
method: 'POST',
const isEditingWeight = Boolean(editingWeightId);
const response = await apiFetch(isEditingWeight ? `/birds/${selectedBird.id}/weights/${editingWeightId}` : `/birds/${selectedBird.id}/weights`, authToken, {
method: isEditingWeight ? 'PUT' : 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
weightGrams: Number(weightForm.weightGrams),
@@ -3373,7 +3375,13 @@ function App() {
if (!data?.weight) {
throw new Error('Unable to save weight.');
}
const nextWeights = [...weights, data.weight].sort((left, right) => left.recordedOn.localeCompare(right.recordedOn));
const nextWeights = (
isEditingWeight ? weights.map((weight) => (weight.id === data.weight.id ? data.weight : weight)) : [...weights, data.weight]
).sort((left, right) => left.recordedOn.localeCompare(right.recordedOn));
const latestWeight = nextWeights.reduce<WeightRecord | null>(
(latest, weight) => (!latest || weight.recordedOn >= latest.recordedOn ? weight : latest),
null,
);
setWeights(nextWeights);
setAllBirdWeights((current) => ({
@@ -3389,18 +3397,34 @@ function App() {
bird.id === selectedBird.id
? {
...bird,
latestWeightGrams: data.weight.weightGrams,
latestRecordedOn: data.weight.recordedOn,
latestWeightGrams: latestWeight?.weightGrams ?? null,
latestRecordedOn: latestWeight?.recordedOn ?? null,
}
: bird,
),
);
setWeightForm({ weightGrams: '', recordedOn: new Date().toISOString().slice(0, 10), notes: '' });
setEditingWeightId('');
} catch (submitError) {
setError(submitError instanceof Error ? submitError.message : 'Unable to save weight.');
}
};
const handleEditWeight = (weight: WeightRecord) => {
setEditingWeightId(weight.id);
setWeightForm({
weightGrams: String(weight.weightGrams),
recordedOn: weight.recordedOn,
notes: weight.notes ?? '',
});
setError('');
};
const handleCancelWeightEdit = () => {
setEditingWeightId('');
setWeightForm({ weightGrams: '', recordedOn: new Date().toISOString().slice(0, 10), notes: '' });
};
const handleBulkWeightValueChange = (birdId: string, weightGrams: string) => {
setBulkWeightRows((current) => ({
...current,
@@ -6541,11 +6565,35 @@ function App() {
placeholder="Optional notes about appetite, molt, meds, or behavior"
/>
</label>
<button className="primary-button" type="submit">
Save weight
</button>
</form>
</section>
<div className="button-row care-form-actions">
<button className="primary-button" type="submit">
{editingWeightId ? 'Save weight changes' : 'Save weight'}
</button>
{editingWeightId ? (
<button className="secondary-button" onClick={handleCancelWeightEdit} type="button">
Cancel edit
</button>
) : null}
</div>
</form>
<div className="recent-list">
{[...weights]
.sort((left, right) => right.recordedOn.localeCompare(left.recordedOn))
.map((weight) => (
<article className="vet-visit-card" key={weight.id}>
<strong>{formatWeight(weight.weightGrams)}</strong>
<span>{formatDate(weight.recordedOn)}</span>
<small>{weight.notes || 'No notes recorded.'}</small>
<div className="button-row">
<button className="secondary-button" onClick={() => handleEditWeight(weight)} type="button">
Edit
</button>
</div>
</article>
))}
{!weights.length ? <p className="muted">No weight entries recorded yet.</p> : null}
</div>
</section>
</div>
) : null}