From 8a1a60e6a13746c5d82552573a31bea0ad2083cb Mon Sep 17 00:00:00 2001 From: Frederik Leonhardt Date: Thu, 2 May 2024 23:01:23 +1200 Subject: [PATCH] Require user confirmation before deleting data --- src/views/child.ts | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/src/views/child.ts b/src/views/child.ts index 747f46e..aac1434 100644 --- a/src/views/child.ts +++ b/src/views/child.ts @@ -9,7 +9,7 @@ import { MeasurementActions, Sex, } from '../models/state'; -import {LocalDate, Period} from '@js-joda/core'; +import {LocalDate, Period, convert} from '@js-joda/core'; const formatAge = (period: Period) => { const parts = []; @@ -57,7 +57,7 @@ const ChildComponent: m.Component> = { }, view({attrs: {state, actions}}) { const name = state.name ?? 'Unnnamed'; - const age = state.age ? '(' + formatAge(state.age) + ' old)' : ''; + const age = state.age ? `(${formatAge(state.age)} old)` : ''; return m( 'details', @@ -78,7 +78,17 @@ const ChildComponent: m.Component> = { class: 'icon', onclick: (e: Event) => { e.preventDefault(); - actions.remove(); + + // Require user confirmation if state contains data + const needConfirm = + state.dateOfBirth || + state.measurements.length || + state.name || + state.sex; + + if (!needConfirm || confirm(`Delete all data for '${name}'?`)) { + actions.remove(); + } }, }, '✖' @@ -353,7 +363,20 @@ const MeasurementRowComponent: m.Component< class: 'icon', onclick: (e: Event) => { e.preventDefault(); - actions.remove(); + + // Require user confirmation if state contains data + const needConfirm = state.head || state.length || state.weight; + + if ( + !needConfirm || + confirm( + `Delete measurements for '${convert(state.date) + .toDate() + .toLocaleDateString()}'?` + ) + ) { + actions.remove(); + } }, }, '✖'