Skip to content

Commit

Permalink
Merge pull request #4 from fkleon/feature-confirm-delete
Browse files Browse the repository at this point in the history
Require confirmation before deleting data
  • Loading branch information
fkleon authored May 2, 2024
2 parents c20990c + 8a1a60e commit bdb09e0
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 bdb09e0

Please sign in to comment.