weight edit fixes
Deploy / deploy-dev (push) Has been skipped
Deploy / deploy-prod (push) Successful in 2m36s

This commit is contained in:
Corey Blais
2026-06-16 15:28:01 -04:00
parent 1849ecd73b
commit 53b75588a2
3 changed files with 17 additions and 31 deletions
+10 -16
View File
@@ -876,13 +876,8 @@ const formatAuditAction = (value: string) =>
const formatWeight = (value: number | null) => (value ? `${value.toFixed(1)} g` : 'Pending');
const formatRange = (minGrams: number, maxGrams: number) => `${minGrams.toFixed(0)}-${maxGrams.toFixed(0)} g`;
const parseDateValue = (value: string) => new Date(`${value}T00:00:00`);
const WEIGHT_EDIT_WINDOW_DAYS = 3;
const getWeightEditEarliestDate = () => {
const earliestDate = new Date();
earliestDate.setDate(earliestDate.getDate() - (WEIGHT_EDIT_WINDOW_DAYS - 1));
return earliestDate.toISOString().slice(0, 10);
};
const isWeightEditable = (weight: WeightRecord) => weight.recordedOn >= getWeightEditEarliestDate();
const getEditableWeights = (entries: WeightRecord[]) =>
[...entries].sort((left, right) => right.recordedOn.localeCompare(left.recordedOn)).slice(0, 3);
const daysBetweenDates = (startDate: string, endDate: string) =>
Math.abs(parseDateValue(endDate).getTime() - parseDateValue(startDate).getTime()) / (24 * 60 * 60 * 1000);
const addYearsToDate = (date: Date, years: number) => {
@@ -1735,6 +1730,8 @@ function App() {
),
[allBirdWeights, birds, overviewWindowStartDate],
);
const editableWeights = useMemo(() => getEditableWeights(weights), [weights]);
const editableWeightIds = useMemo(() => new Set(editableWeights.map((weight) => weight.id)), [editableWeights]);
const showFlockDetailColumn = bulkWeightOpen || birdEditorOpen || Boolean(selectedBird);
@@ -3418,8 +3415,8 @@ function App() {
};
const handleEditWeight = (weight: WeightRecord) => {
if (!isWeightEditable(weight)) {
setError('Weight entries can only be edited for the last 3 days.');
if (!editableWeightIds.has(weight.id)) {
setError('Only the 3 most recent weight entries can be edited.');
return;
}
@@ -6564,8 +6561,7 @@ function App() {
<input
type="date"
value={weightForm.recordedOn}
min={editingWeightId ? getWeightEditEarliestDate() : undefined}
onChange={(event) => setWeightForm({ ...weightForm, recordedOn: event.target.value })}
onChange={(event) => setWeightForm({ ...weightForm, recordedOn: event.target.value })}
required
/>
</label>
@@ -6590,10 +6586,8 @@ function App() {
</div>
</form>
<div className="recent-list">
{[...weights]
.filter(isWeightEditable)
.sort((left, right) => right.recordedOn.localeCompare(left.recordedOn))
.map((weight) => (
{editableWeights
.map((weight) => (
<article className="vet-visit-card" key={weight.id}>
<strong>{formatWeight(weight.weightGrams)}</strong>
<span>{formatDate(weight.recordedOn)}</span>
@@ -6605,7 +6599,7 @@ function App() {
</div>
</article>
))}
{!weights.some(isWeightEditable) ? <p className="muted">No editable weight entries from the last 3 days.</p> : null}
{!editableWeights.length ? <p className="muted">No weight entries recorded yet.</p> : null}
</div>
</section>
</div>