feat(waiter): the waiter app on the shared foundations (api client, access, i18n), lint clean, starts offline
Plan step 4.1. No visible change to layout or flows. - api/client.js on @xenia/api createApiClient (CS-8 baseURL, 401 → sign out, no answer → backend-offline, err.xenia, the manager-approval retry); the PIN login handles its own 401 (before, a wrong PIN reloaded the page). - <AccessProvider> at the root with an i18n instance: modules, permission keys, venue, language from bootstrap; who is signed in comes with it (no more /api/auth/me polling). packages/access: AccessProvider gains `cache` (the last bootstrap per session; used when the server can't be reached, `offline` in the context, keeps retrying) and `failed` (the app's own screen when there's nothing cached). The waiter caches per venue, shows its offline page (server search) on a first start without a server, retries every 5 s; ServerRediscovery and BundleSync run outside the bootstrap. - Role names → keys: the shift gate and the user menu use core.dashboard.access. - One shared, offline-cached read of the venue settings (api/venueSettings.js) instead of seven GET /api/settings/ calls; apiError() shows backend error codes in the user's language (17 places). - Lint: 78 errors → 0 (unused code, empty catches, two duplicate style keys, a component created during render, refs read during render → state, hooks after an early return); set-state-in-effect is a warning like the manager/terminal; lint in run-all. - Tests: tests/bootstrapCache.test.mjs; e2e browser: an offline restart stays signed in. Android rediscovery waits for what the screen shows instead of fixed sleeps: 10/10 three runs in a row (KI-009). Terminal 3.4 waits for both displays before the sale. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+43
-36
@@ -1,6 +1,8 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
import { BrowserRouter, Routes, Route, Navigate, Outlet, useNavigate } from 'react-router-dom'
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import { AccessProvider, useAccess, useCan } from '@xenia/access'
|
||||
import { i18n } from './i18n'
|
||||
import useAuthStore from './store/authStore'
|
||||
import useShiftStore from './store/shiftStore'
|
||||
import useThemeStore from './store/themeStore'
|
||||
@@ -8,6 +10,9 @@ import useTableColourStore from './store/tableColourStore'
|
||||
import useConnectionStore from './store/connectionStore'
|
||||
import useWaiterFavoritesStore from './store/waiterFavoritesStore'
|
||||
import client from './api/client'
|
||||
import { apiError } from './api/errors'
|
||||
import { getVenueSettings } from './api/venueSettings'
|
||||
import { bootstrapCache } from './api/bootstrapCache'
|
||||
import LoginPage from './pages/LoginPage'
|
||||
import TableListPage from './pages/TableListPage'
|
||||
import TableDetailPage from './pages/TableDetailPage'
|
||||
@@ -111,7 +116,7 @@ function StartShiftScreen({ username, onStart, onLogout }) {
|
||||
try {
|
||||
await onStart(startingCash ? parseFloat(startingCash) : null)
|
||||
} catch (e) {
|
||||
setError(e.response?.data?.detail || 'Σφάλμα εκκίνησης βάρδιας')
|
||||
setError(apiError(e, 'Σφάλμα εκκίνησης βάρδιας'))
|
||||
setStarting(false)
|
||||
}
|
||||
}
|
||||
@@ -173,15 +178,13 @@ function AppLayout() {
|
||||
const { token, user, logout } = useAuthStore()
|
||||
const navigate = useNavigate()
|
||||
const {
|
||||
shift, businessDay,
|
||||
setShift, setBusinessDay,
|
||||
setSelfStartAllowed, setSelfEndAllowed,
|
||||
gateStatus, setGateStatus,
|
||||
} = useShiftStore()
|
||||
|
||||
if (!token) return <Navigate to="/login" replace />
|
||||
|
||||
const isManager = user?.role && user.role !== 'waiter'
|
||||
// People with the manager dashboard work without a shift of their own (plan 4.1: the key,
|
||||
// not the role name)
|
||||
const isManager = useCan('core.dashboard.access')
|
||||
|
||||
async function checkGate() {
|
||||
if (!user) return
|
||||
@@ -203,7 +206,7 @@ function AppLayout() {
|
||||
|
||||
// No active shift — check self-start setting
|
||||
try {
|
||||
const settingsRes = await client.get('/api/settings/')
|
||||
const settingsRes = await getVenueSettings()
|
||||
const canStart = settingsRes.data?.['shifts.waiter_self_start']?.value !== 'false'
|
||||
const canEnd = settingsRes.data?.['shifts.waiter_self_end']?.value !== 'false'
|
||||
setSelfStartAllowed(canStart)
|
||||
@@ -222,7 +225,7 @@ function AppLayout() {
|
||||
|
||||
useEffect(() => {
|
||||
if (user) checkGate()
|
||||
}, [user?.id])
|
||||
}, [user?.id]) // eslint-disable-line react-hooks/exhaustive-deps -- once per signed-in user
|
||||
|
||||
// Poll every 15s to detect shift-end or business-day-close triggered by manager
|
||||
useEffect(() => {
|
||||
@@ -241,7 +244,7 @@ function AppLayout() {
|
||||
}
|
||||
}, 15_000)
|
||||
return () => clearInterval(id)
|
||||
}, [user, isManager, gateStatus])
|
||||
}, [user, isManager, gateStatus]) // eslint-disable-line react-hooks/exhaustive-deps -- checkGate reads the latest store
|
||||
|
||||
async function handleStartShift(startingCash) {
|
||||
const res = await client.post('/api/shifts/start', { starting_cash: startingCash })
|
||||
@@ -254,6 +257,7 @@ function AppLayout() {
|
||||
navigate('/login')
|
||||
}
|
||||
|
||||
if (!token) return <Navigate to="/login" replace />
|
||||
if (!user || gateStatus === 'loading') return <Spinner />
|
||||
|
||||
if (gateStatus === 'closed') return <ClosedScreen onRefresh={checkGate} onLogout={handleLogout} />
|
||||
@@ -273,34 +277,17 @@ function AppLayout() {
|
||||
|
||||
// ─── Global helpers ───────────────────────────────────────────────────────────
|
||||
|
||||
/** Who is signed in comes with the bootstrap (plan 4.1): online it's the server's answer;
|
||||
* offline the bootstrap cached for this token keeps the waiter signed in (CS-3). A token the
|
||||
* server no longer knows comes back with no user: sign out. */
|
||||
function AuthRehydrator() {
|
||||
const { token, user, login, logout } = useAuthStore()
|
||||
const access = useAccess()
|
||||
useEffect(() => {
|
||||
if (!token || user) return
|
||||
let cancelled = false
|
||||
let timer = null
|
||||
async function load() {
|
||||
try {
|
||||
const r = await client.get('/api/auth/me')
|
||||
if (!cancelled) login(r.data, token)
|
||||
} catch (err) {
|
||||
if (cancelled) return
|
||||
// Server unreachable (flaky WiFi, server restarting, IP change being
|
||||
// rediscovered): keep the login and try again. Only a real answer from
|
||||
// the server ends the session — 401 is also handled by the axios interceptor.
|
||||
if (!err.response) {
|
||||
timer = setTimeout(load, 5000)
|
||||
return
|
||||
}
|
||||
logout()
|
||||
}
|
||||
}
|
||||
load()
|
||||
return () => {
|
||||
cancelled = true
|
||||
clearTimeout(timer)
|
||||
}
|
||||
}, [token, user])
|
||||
if (access.user) login(access.user, token)
|
||||
else if (!access.offline) logout()
|
||||
}, [token, user, access.user, access.offline, login, logout])
|
||||
return null
|
||||
}
|
||||
|
||||
@@ -376,7 +363,7 @@ function AndroidBackGuard() {
|
||||
function ColourLoader() {
|
||||
const loadFromBackend = useTableColourStore(s => s.loadFromBackend)
|
||||
useEffect(() => {
|
||||
client.get('/api/settings/')
|
||||
getVenueSettings()
|
||||
.then(r => {
|
||||
const raw = r.data?.['ui.table_colours']?.value
|
||||
if (raw) loadFromBackend(raw)
|
||||
@@ -432,7 +419,29 @@ export default function App() {
|
||||
if (deliveryMode() === 'native' && !getActiveVenue()) {
|
||||
return <VenuesPage firstRun />
|
||||
}
|
||||
return (
|
||||
<>
|
||||
{/* outside the bootstrap: they must run even when the server can't be reached yet */}
|
||||
{deliveryMode() === 'native' && <ServerRediscovery />}
|
||||
{deliveryMode() === 'native' && <BundleSync />}
|
||||
<Access><Routed /></Access>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
/** The platform bootstrap for the current session (plan step 4.1): modules, permission keys,
|
||||
* venue and language for every screen; cached per venue so the app also starts offline. */
|
||||
function Access({ children }) {
|
||||
const token = useAuthStore(s => s.token)
|
||||
return (
|
||||
<AccessProvider client={client} token={token} i18n={i18n} cache={bootstrapCache} loading={<Spinner />} autoRetryMs={5000}
|
||||
failed={error => (error.offline || error.status >= 500 ? <OfflinePage /> : undefined)}>
|
||||
{children}
|
||||
</AccessProvider>
|
||||
)
|
||||
}
|
||||
|
||||
function Routed() {
|
||||
return (
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<BrowserRouter>
|
||||
@@ -448,8 +457,6 @@ export default function App() {
|
||||
{/* Native builds ship their UI in the app bundle — no service worker to update */}
|
||||
{deliveryMode() !== 'native' && <UpdatePrompt />}
|
||||
<InstallAppDialog />
|
||||
{deliveryMode() === 'native' && <ServerRediscovery />}
|
||||
{deliveryMode() === 'native' && <BundleSync />}
|
||||
<Routes>
|
||||
<Route path="/login" element={<LoginGuard><LoginPage /></LoginGuard>} />
|
||||
<Route path="/offline" element={<OfflinePage />} />
|
||||
|
||||
Reference in New Issue
Block a user