Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

Workout editor: one save prompt at a time; a second ask while it is open joins it instead of asking again after Save changes

Duarte Santosdc39325174f6
diff --git a/frontend/src/sheets.jsx b/frontend/src/sheets.jsx
index 120444715..551d3726c 100644
--- a/frontend/src/sheets.jsx
+++ b/frontend/src/sheets.jsx
@@ -2749,27 +2749,38 @@ export function saveWorkoutEdits(onExit = () => nav('/history')) {
} catch (error) { toast(t(error.message)) }
}
+// The one save prompt that may be open: { id, onExit }. Closing the editor and a route exit
+// can both ask in the same moment (the X, then the route change its tap set off); a second
+// prompt stacked under the first came back after Save changes and asked all over again
+// (QA 10-06). The second ask now joins the open prompt and only says where to go after it.
+let savePrompt = null
+const promptOpen = () => !!savePrompt && useUI.getState().sheets.some(x => x.id === savePrompt.id)
+
export function exitWorkoutEdit(onExit = () => nav('/history')) {
if (!S().active?.editingWorkoutId) { onExit(); return }
+ if (promptOpen()) { savePrompt.onExit = onExit; return }
+ const prompt = { id: null, onExit }
+ const done = () => prompt.onExit()
const leave = () => {
// A stale prompt's Don't save must not throw away whatever is in S.active by now.
- if (!S().active?.editingWorkoutId) { onExit(); return }
+ if (!S().active?.editingWorkoutId) { done(); return }
useStore.getState().discardHistoryEdit()
useUI.getState().stopRest()
useUI.getState().stopWork()
- onExit()
+ done()
}
// Nothing to save: closing just closes, as it does for a workout only looked at.
if (editChangesNothing(S())) { leave(); return }
- ui().openSheet(close => <>
+ prompt.id = ui().openSheet(close => <>
<h3>{t('Save workout changes?')}</h3>
<p className="muted">{t('Save your edits to this workout, or keep the original record.')}</p>
- <Button variant="primary" onClick={() => { close(); saveWorkoutEdits(onExit) }}>{t('Save changes')}</Button>
+ <Button variant="primary" onClick={() => { close(); saveWorkoutEdits(done) }}>{t('Save changes')}</Button>
<div style={{ height: 8 }} />
<Button onClick={() => { close(); leave() }}>{t("Don't save")}</Button>
<div style={{ height: 8 }} />
<Button variant="ghost" className="dim" onClick={close}>{t('Keep editing')}</Button>
- </>, { kind: 'center' })
+ </>, { kind: 'center' })?.id
+ savePrompt = prompt
}
function FinishSummary({ w, prs, e1prs = [], close }) {
diff --git a/frontend/src/sheets.workout-edit-exit.test.jsx b/frontend/src/sheets.workout-edit-exit.test.jsx
index c382eb386..075dc4854 100644
--- a/frontend/src/sheets.workout-edit-exit.test.jsx
+++ b/frontend/src/sheets.workout-edit-exit.test.jsx
@@ -42,29 +42,42 @@ describe('closing the saved-workout editor', () => {
})
afterEach(() => { act(() => { mounted.splice(0).forEach(root => root.unmount()) }) })
- it('a second, stale save prompt only leaves: no delete offer, no false toast', () => {
+ it('a second ask while the prompt is open joins it: one prompt, one save, the latest way out', () => {
exitWorkoutEdit()
- exitWorkoutEdit() // the route-exit prompt that raced the first one
- expect(useUI.getState().sheets.length).toBe(2)
- const second = renderTop()
- const first = useUI.getState().sheets[0]
- act(() => { useUI.getState().closeSheet(useUI.getState().sheets.at(-1).id) })
- // Save on the first prompt saves and closes the editor.
- const host = document.createElement('div'); document.body.appendChild(host)
- const root = createRoot(host); mounted.push(root)
- act(() => root.render(first.render(() => useUI.getState().closeSheet(first.id))))
+ exitWorkoutEdit(() => navigated.push('/stats')) // the route exit that raced the X
+ expect(useUI.getState().sheets.length).toBe(1)
+ const host = renderTop()
act(() => { button(host, 'Save changes').click() })
expect(useStore.getState().S.active).toBeNull()
expect(useStore.getState().S.workouts[0].entries[0].sets[0].w).toBe(50)
expect(useUI.getState().toastMsg).toBe('Workout updated')
- // The stale one's Save now just finishes leaving.
- act(() => { button(second, 'Save changes').click() })
+ expect(useUI.getState().sheets.length).toBe(0)
+ expect(useStore.getState().S.workouts).toHaveLength(1)
+ expect(navigated).toEqual(['/stats'])
+ })
+
+ it('a stale prompt left open after the editor closed only leaves: no delete offer, no false toast', () => {
+ exitWorkoutEdit()
+ const stale = renderTop()
+ // The editor closes some other way (a save from the bottom button) while the prompt is up.
+ act(() => { saveWorkoutEdits(() => navigated.push('/history')) })
+ expect(useUI.getState().toastMsg).toBe('Workout updated')
+ act(() => { button(stale, 'Save changes').click() })
expect(useUI.getState().sheets.length).toBe(0)
expect(useUI.getState().toastMsg).toBe('Workout updated')
expect(useStore.getState().S.workouts).toHaveLength(1)
expect(navigated).toEqual(['/history', '/history'])
})
+ it('after a prompt was answered, the next close asks afresh', () => {
+ exitWorkoutEdit()
+ const h = renderTop()
+ act(() => { button(h, 'Keep editing').click() })
+ expect(useUI.getState().sheets.length).toBe(0)
+ exitWorkoutEdit()
+ expect(useUI.getState().sheets.length).toBe(1)
+ })
+
it('with no editor open, closing and saving open nothing', () => {
useStore.getState().discardHistoryEdit()
exitWorkoutEdit()