Skip to content

Commit

Permalink
Require user confirmation before deleting data
Browse files Browse the repository at this point in the history
  • Loading branch information
fkleon committed May 2, 2024
1 parent c20990c commit 8a1a60e
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions src/views/child.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -57,7 +57,7 @@ const ChildComponent: m.Component<MitosisAttr<Child, IChildActions>> = {
},
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',
Expand All @@ -78,7 +78,17 @@ const ChildComponent: m.Component<MitosisAttr<Child, IChildActions>> = {
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();
}
},
},
'✖'
Expand Down Expand Up @@ -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();
}
},
},
'✖'
Expand Down

0 comments on commit 8a1a60e

Please sign in to comment.