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:
Vendored
+8
@@ -0,0 +1,8 @@
|
||||
// The last platform bootstrap (modules, permission keys, venue, language), kept per venue so
|
||||
// the waiter app starts and works offline (CS-3, plan step 4.1). <AccessProvider cache={…}>
|
||||
// saves every bootstrap it loads and uses this one, for the same session token, when the
|
||||
// server can't be reached. Rules: src/logic/bootstrapCache.js.
|
||||
import { storageKey } from '../config/server'
|
||||
import { makeBootstrapCache } from '../logic/bootstrapCache'
|
||||
|
||||
export const bootstrapCache = makeBootstrapCache(localStorage, () => storageKey('bootstrap'))
|
||||
@@ -1,27 +1,28 @@
|
||||
// The waiter's HTTP client on the shared @xenia/api (plan step 4.1): the session header, every
|
||||
// error carries err.xenia ({ code, params, message, … } shown in the user's language), and a
|
||||
// request a manager may approve opens the approval dialog (mounted by <AccessProvider>) and is
|
||||
// retried once with their PIN. The backend address follows the active venue (CS-8: same
|
||||
// origin in a browser served by the venue, absolute in the native app).
|
||||
import axios from 'axios'
|
||||
import { createApiClient } from '@xenia/api'
|
||||
import { apiBase, storageKey } from '../config/server'
|
||||
|
||||
const client = axios.create({ baseURL: apiBase() })
|
||||
|
||||
client.interceptors.request.use(config => {
|
||||
const token = localStorage.getItem(storageKey('token'))
|
||||
if (token) config.headers.Authorization = `Bearer ${token}`
|
||||
return config
|
||||
const client = createApiClient({
|
||||
axios,
|
||||
baseURL: () => apiBase(),
|
||||
getToken: () => localStorage.getItem(storageKey('token')),
|
||||
// No answer at all: the connectivity bar and the rediscovery scan listen for this
|
||||
onOffline: () => window.dispatchEvent(new Event('backend-offline')),
|
||||
// A 401 on a normal request = the session ended (expired, ended by a manager, signed in
|
||||
// elsewhere): sign out. The login and PIN forms opt out with { xenia: { ignoreUnauthorized: true } }.
|
||||
onUnauthorized: () => {
|
||||
localStorage.removeItem(storageKey('token'))
|
||||
localStorage.removeItem(storageKey('savedUsername'))
|
||||
window.location.href = '/login'
|
||||
},
|
||||
})
|
||||
|
||||
client.interceptors.response.use(
|
||||
res => res,
|
||||
err => {
|
||||
if (!err.response) {
|
||||
window.dispatchEvent(new Event('backend-offline'))
|
||||
} else if (err.response.status === 401) {
|
||||
// Token expired or user blocked — force logout
|
||||
localStorage.removeItem(storageKey('token'))
|
||||
localStorage.removeItem(storageKey('savedUsername'))
|
||||
window.location.href = '/login'
|
||||
}
|
||||
return Promise.reject(err)
|
||||
}
|
||||
)
|
||||
// Dev builds only: lets the e2e suites drive requests through this client
|
||||
if (import.meta.env.DEV) window.__xeniaClient = client
|
||||
|
||||
export default client
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
// A refused request as a message for the waiter (plan step 4.1): a backend error code is
|
||||
// shown in the user's language (packages/i18n locales errors.json); an older endpoint's own
|
||||
// text as it comes; with no answer at all, the screen's own fallback text.
|
||||
import { errorMessage, parseError } from '@xenia/api'
|
||||
import { i18n } from '../i18n'
|
||||
|
||||
export function apiError(err, fallback = '') {
|
||||
const x = parseError(err)
|
||||
if (x.offline) return fallback || errorMessage(err, i18n.t.bind(i18n))
|
||||
if (!x.code.startsWith('http.')) return errorMessage(err, i18n.t.bind(i18n))
|
||||
return (typeof x.detail === 'string' && x.detail) || fallback || x.message
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// The venue's settings the waiter app reads (shift self-start, card payments, courses, quick
|
||||
// notes, table colours…), plan step 4.1: one request shared by every screen instead of a
|
||||
// GET /api/settings/ per page, kept per venue so the screens keep their options offline (CS-3).
|
||||
//
|
||||
// const { data } = await getVenueSettings() // same shape as the old client.get(...)
|
||||
// data?.['payments.card_enabled']?.value
|
||||
//
|
||||
// Fresh for FRESH_MS; concurrent callers share one request; without a server the last copy
|
||||
// saved for this venue answers (or {} on a first start offline).
|
||||
import client from './client'
|
||||
import { storageKey } from '../config/server'
|
||||
|
||||
const FRESH_MS = 30_000
|
||||
let memo = null // { at, data }
|
||||
let inflight = null
|
||||
|
||||
const KEY = () => storageKey('venueSettings')
|
||||
|
||||
function saved() {
|
||||
try { return JSON.parse(localStorage.getItem(KEY()) || 'null') } catch { return null }
|
||||
}
|
||||
|
||||
export function getVenueSettings({ fresh = false } = {}) {
|
||||
if (!fresh && memo && Date.now() - memo.at < FRESH_MS) return Promise.resolve({ data: memo.data })
|
||||
if (inflight) return inflight
|
||||
inflight = client.get('/api/settings/')
|
||||
.then(r => {
|
||||
memo = { at: Date.now(), data: r.data }
|
||||
try { localStorage.setItem(KEY(), JSON.stringify(r.data)) } catch { /* storage full: still works online */ }
|
||||
return { data: r.data }
|
||||
})
|
||||
.catch(err => {
|
||||
const copy = saved()
|
||||
if (copy && !err?.response) return { data: copy }
|
||||
throw err
|
||||
})
|
||||
.finally(() => { inflight = null })
|
||||
return inflight
|
||||
}
|
||||
Reference in New Issue
Block a user