trimmed weight edit
This commit is contained in:
+40
-19
@@ -917,6 +917,13 @@ 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 daysBetweenDates = (startDate: string, endDate: string) =>
|
||||
Math.abs(parseDateValue(endDate).getTime() - parseDateValue(startDate).getTime()) / (24 * 60 * 60 * 1000);
|
||||
const addYearsToDate = (date: Date, years: number) => {
|
||||
@@ -3685,6 +3692,11 @@ function App() {
|
||||
};
|
||||
|
||||
const handleEditWeight = (weight: WeightRecord) => {
|
||||
if (!isWeightEditable(weight)) {
|
||||
setError('Weight entries can only be edited for the last 3 days.');
|
||||
return;
|
||||
}
|
||||
|
||||
setEditingWeightId(weight.id);
|
||||
setWeightForm({
|
||||
weightGrams: String(weight.weightGrams),
|
||||
@@ -7054,11 +7066,12 @@ function App() {
|
||||
<label>
|
||||
Recorded on
|
||||
<input
|
||||
type="date"
|
||||
value={weightForm.recordedOn}
|
||||
onChange={(event) => setWeightForm({ ...weightForm, recordedOn: event.target.value })}
|
||||
required
|
||||
/>
|
||||
type="date"
|
||||
value={weightForm.recordedOn}
|
||||
min={editingWeightId ? getWeightEditEarliestDate() : undefined}
|
||||
onChange={(event) => setWeightForm({ ...weightForm, recordedOn: event.target.value })}
|
||||
required
|
||||
/>
|
||||
</label>
|
||||
<label className="wide-field">
|
||||
Notes
|
||||
@@ -7081,20 +7094,28 @@ function App() {
|
||||
</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]
|
||||
.sort((left, right) => right.recordedOn.localeCompare(left.recordedOn))
|
||||
.map((weight) => {
|
||||
const weightCanBeEdited = isWeightEditable(weight);
|
||||
|
||||
return (
|
||||
<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>
|
||||
{weightCanBeEdited ? (
|
||||
<div className="button-row">
|
||||
<button className="secondary-button" onClick={() => handleEditWeight(weight)} type="button">
|
||||
Edit
|
||||
</button>
|
||||
</div>
|
||||
) : (
|
||||
<small>Editing available for the last 3 days only.</small>
|
||||
)}
|
||||
</article>
|
||||
);
|
||||
})}
|
||||
{!weights.length ? <p className="muted">No weight entries recorded yet.</p> : null}
|
||||
</div>
|
||||
</section>
|
||||
|
||||
Reference in New Issue
Block a user