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.

Admin: users that cannot be loaded show an error and a retry instead of an endless 'Loading…'

Duarte Santosa84ec9101c31
diff --git a/frontend/src/views/Admin.jsx b/frontend/src/views/Admin.jsx
index cda0978dc..7af4e69f8 100644
--- a/frontend/src/views/Admin.jsx
+++ b/frontend/src/views/Admin.jsx
@@ -216,14 +216,23 @@ function AuditCard({ tick }) {
export default function Admin() {
const nav = useNavigate()
const user = useStore(s => s.user)
- const toast = useUI(s => s.toast)
const openSheet = useUI(s => s.openSheet)
const [users, setUsers] = useState(null)
+ const [usersErr, setUsersErr] = useState(null) // why the last load failed, until one succeeds
const [invites, setInvites] = useState(null)
const [inviteOnly, setInviteOnly] = useState(false)
const [tick, setTick] = useState(0) // the ↻ button; the activity log listens to it
- const loadUsers = () => api('/api/admin/users').then(d => { setUsers(d.users); setInviteOnly(d.invite_only) }).catch(e => toast(e.message || 'Failed to load'))
+ // A failed load, or an answer without a list, used to leave the page on "Loading…" for good —
+ // with a toast every 15 seconds from the poll, or with nothing at all when the answer was
+ // someone else's page. It says so where the list would be instead, and the poll (or ↻) keeps
+ // trying; a list that did load stays up, marked as the last one that came through.
+ const loadUsers = () => api('/api/admin/users')
+ .then(d => {
+ if (!Array.isArray(d?.users)) throw new Error('The server answered without a list of users.')
+ setUsers(d.users); setInviteOnly(!!d.invite_only); setUsersErr(null)
+ })
+ .catch(e => setUsersErr(e.message || 'Failed to load'))
const loadInvites = () => api('/api/admin/invites').then(d => setInvites(d.invites)).catch(() => {})
// poll every 15s so the "training now" section stays live without a manual refresh
useEffect(() => { if (!user?.admin) return; loadUsers(); loadInvites(); const iv = setInterval(loadUsers, 15000); return () => clearInterval(iv) }, [])
@@ -238,13 +247,21 @@ export default function Admin() {
<div className="hdr">
<button className="iconbtn" onClick={() => nav('/settings')} aria-label="Back"><Icon name="chevronLeft" /></button>
<div style={{ flex: 1, marginLeft: 8 }}><h1 style={{ margin: 0 }}>Admin</h1>
- <div className="sub">{users ? users.length + ' users · ' + activeCount + ' active this week' : 'Loading…'}</div></div>
+ <div className="sub">{users ? users.length + ' users · ' + activeCount + ' active this week' : usersErr ? 'Could not load' : 'Loading…'}</div></div>
<button className="iconbtn" onClick={() => { loadUsers(); loadInvites(); setTick(n => n + 1) }} aria-label="refresh">↻</button>
</div>
<div className="adm-intro">
Everything about running this instance: who uses it, how they get in, the AI Coach, and what has happened on it. Nothing here shows anyone's training data beyond counts.
</div>
+ {usersErr && <div className="card" role="alert" style={{ borderColor: 'var(--red)' }}>
+ <div className="row between"><h2 style={{ margin: 0 }}>{users ? 'The last update failed' : 'Could not load the users'}</h2>
+ <Button size="sm" icon="reset" onClick={loadUsers}>Try again</Button></div>
+ <div className="adm-lead" style={{ marginBottom: 0 }}>
+ {usersErr} {users ? 'The list below is the last one that loaded.' : 'It tries again every 15 seconds.'}
+ </div>
+ </div>}
+
<div className="tiles" style={{ marginBottom: 12 }}>
<div className="tile"><div className="l">Users</div><div className="v">{users ? users.length : '—'}</div></div>
<div className="tile"><div className="l">Training now</div><div className="v" style={{ color: liveUsers.length ? 'var(--acc)' : undefined }}>{users ? liveUsers.length : '—'}</div></div>
diff --git a/frontend/src/views/Admin.test.jsx b/frontend/src/views/Admin.test.jsx
index d3cdba6d4..54c4e9e4c 100644
--- a/frontend/src/views/Admin.test.jsx
+++ b/frontend/src/views/Admin.test.jsx
@@ -82,3 +82,43 @@ describe('Admin user drill-down', () => {
expect(rows[0].textContent).toContain('1 sets')
})
})
+
+// The users call failing, or answering without a list, used to leave "Loading…" up for good —
+// what a paired phone that had lost its pairing showed its admin, and all it showed.
+describe('Admin when the users cannot be loaded', () => {
+ const text = page => page.textContent
+ it('an answer without a list is an error, not an endless "Loading…"', async () => {
+ mocks.answers['/api/admin/users'] = {}
+ const page = render(<Admin />)
+ await settle()
+ expect(text(page)).not.toContain('Loading…')
+ expect(text(page)).toContain('Could not load the users')
+ expect(text(page)).toContain('The server answered without a list of users.')
+ })
+
+ it('a failed call says why, and "Try again" loads the list once the server answers', async () => {
+ delete mocks.answers['/api/admin/users']
+ const page = render(<Admin />)
+ await settle()
+ expect(page.querySelector('[role="alert"]').textContent).toContain('not found')
+
+ mocks.answers['/api/admin/users'] = { users: [{ id: 'u1', name: 'Mallory', workouts: 2, lastSync: null, disabled: false }], invite_only: false }
+ const retry = [...page.querySelectorAll('button')].find(b => b.textContent === 'Try again')
+ act(() => retry.click())
+ await settle()
+ expect(page.querySelector('[role="alert"]')).toBeNull()
+ expect(text(page)).toContain('1 users')
+ expect([...page.querySelectorAll('.item')].some(el => el.textContent.includes('Mallory'))).toBe(true)
+ })
+
+ it('a list that loaded stays up when a later update fails, marked as the last one', async () => {
+ const page = render(<Admin />)
+ await settle()
+ delete mocks.answers['/api/admin/users']
+ const refresh = page.querySelector('[aria-label="refresh"]')
+ act(() => refresh.click())
+ await settle()
+ expect(text(page)).toContain('The last update failed')
+ expect([...page.querySelectorAll('.item')].some(el => el.textContent.includes('Mallory'))).toBe(true)
+ })
+})