@xeplr/cli 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/lib/generate/index.js +17 -6
- package/package.json +1 -1
- package/templates/README.md +1 -1
- package/templates/mobile/README.md +5 -3
- package/templates/mobile/package.json +2 -2
- package/templates/mobile/src/app/_layout.jsx +7 -1
- package/templates/mobile/src/app/forms/[form]/index.jsx +2 -2
- package/templates/mobile/src/app/index.jsx +3 -2
- package/templates/mobile/src/lib/app.js +11 -0
- package/templates/ui/package.json +1 -1
- package/templates/ui/pages.json +2 -3
- package/templates-tenancy/mobile/src/app/select.jsx +16 -0
- package/templates-tenancy/ui/src/pages/SelectScope.jsx +38 -113
package/README.md
CHANGED
|
@@ -50,7 +50,7 @@ Already working, with nothing to wire up:
|
|
|
50
50
|
It follows the app's tenancy (none, or the levels you chose) and never creates
|
|
51
51
|
companies or workspaces of its own. Approvals (another person's turn) are not
|
|
52
52
|
part of it.
|
|
53
|
-
- **A phone app** — `mobile/` (Expo): the same app drawn natively on Android and iOS — every form added to the menu, its list, add / edit, its buttons and the journeys — with no screen code of its own: it draws what the server holds (`@xeplr/ui-factory/native`, `@xeplr/ui-account/native`). Try it with Expo Go, build an APK with `npm run build:apk`.
|
|
53
|
+
- **A phone app** — `mobile/` (Expo): the same app drawn natively on Android and iOS — every form added to the menu, its list, add / edit, its buttons and the journeys — with no screen code of its own: it draws what the server holds (`@xeplr/ui-factory/native`, `@xeplr/ui-account/native`). Try it with Expo Go, build an APK with `npm run build:apk`. An app whose data is split by company asks for the company first, on the phone as on the web.
|
|
54
54
|
- **Functions** — `api/functions/`: the app's own code, registered with
|
|
55
55
|
`@xeplr/actions` by the event it is for (`beforeSave`, `afterSave`, …,
|
|
56
56
|
`decide`) and picked by name in a screen's events or a flow step. Two to start
|
package/lib/generate/index.js
CHANGED
|
@@ -120,8 +120,22 @@ function tenancySubstitutions(levels) {
|
|
|
120
120
|
|
|
121
121
|
// ui/src/App.jsx
|
|
122
122
|
__MT_APP_UI_IMPORTS__: on ? "import SelectScope, { ScopeGate } from './pages/SelectScope.jsx'" : '',
|
|
123
|
-
// ui/pages.json: the company picker is
|
|
124
|
-
__MT_PAGES_SELECT__: on ? ' { "path": "/select", "menu": "Switch ' + first.label.toLowerCase() + '", "
|
|
123
|
+
// ui/pages.json: the company picker is on the phone too (NativeScopePicker), as a page of its own.
|
|
124
|
+
__MT_PAGES_SELECT__: on ? ',\n { "path": "/select", "menu": "Switch ' + first.label.toLowerCase() + '", "screen": "the ' + levels.map(function (l) { return l.label.toLowerCase(); }).join(' and ') + ' picker — NativeScopePicker on a phone" }' : '',
|
|
125
|
+
// mobile/: the same levels on the phone, the picker first, Switch on the menu.
|
|
126
|
+
__MT_MOBILE_TENANCY__: on ? [
|
|
127
|
+
'',
|
|
128
|
+
'/**',
|
|
129
|
+
' * Who the data belongs to — the same levels as the API and the website. The',
|
|
130
|
+
' * first page after signing in is choosing them (app/select.jsx), and every',
|
|
131
|
+
' * request carries the choice.',
|
|
132
|
+
' */',
|
|
133
|
+
'export const TENANCY = ' + JSON.stringify({ levels: levels, slots: slots }, null, 2)
|
|
134
|
+
].join('\n') : '',
|
|
135
|
+
__MT_MOBILE_IMPORTS__: on ? "import { registerMTs, scopeChosen } from '@xeplr/ui-account/native'\nimport { TENANCY } from '../lib/app.js'" : '',
|
|
136
|
+
__MT_MOBILE_GATE__: on ? " // The company first (once — the choice is remembered).\n if (signedIn && !scopeChosen(TENANCY.levels) && segments[0] !== 'select') return <Redirect href=\"/select\" />" : '',
|
|
137
|
+
__MT_MOBILE_REGISTER__: on ? ' registerMTs(TENANCY.slots)' : '',
|
|
138
|
+
__MT_MOBILE_SWITCH__: on ? " <View style={{ marginBottom: 10 }}><Btn label=\"" + 'Switch ' + first.label.toLowerCase() + "\" onPress={() => router.push('/select')} /></View>" : '',
|
|
125
139
|
__MT_MENU_KEY__: on ? " 'Switch " + first.label.toLowerCase() + "'," : '',
|
|
126
140
|
__MT_DRAWER_ITEM__: on ? " { key: 'Switch " + first.label.toLowerCase() + "', icon: SwitchIcon, clickHandler: () => navigate('/select') }," : '',
|
|
127
141
|
__MT_SWITCH_ICON__: on ? [
|
|
@@ -329,10 +343,7 @@ function generate(answers, target) {
|
|
|
329
343
|
|
|
330
344
|
Object.keys(sources).forEach(function (rel) {
|
|
331
345
|
var src = sources[rel];
|
|
332
|
-
|
|
333
|
-
// company on a phone is not built yet, and a phone app that cannot pick one
|
|
334
|
-
// would only be refused by the API. Left out rather than half there.
|
|
335
|
-
if (answers.tenancy && answers.tenancy.length && rel.split(path.sep)[0] === 'mobile') return;
|
|
346
|
+
|
|
336
347
|
var dir = path.dirname(rel);
|
|
337
348
|
var base = path.basename(rel);
|
|
338
349
|
var outName = RENAME[base] || base;
|
package/package.json
CHANGED
package/templates/README.md
CHANGED
|
@@ -65,7 +65,7 @@ Every one of these checks the same sign-in token, so one login covers them all.
|
|
|
65
65
|
- **Tasks** — a sample built from screens: a list, and a form it opens on a page of its own, saved into an ordinary `tasks` table. The form is filled in over three steps and uses every control there is — text, a dropdown, radio buttons, a tick box, a date, a date and time, several tags at once, and a file.
|
|
66
66
|
- **Configure UI** — in the settings menu, for Super Admin: every form (make one, design it, publish it, add it to the side rail), every flow (screens one after another — each screen's events, and where it goes next) and the side rail itself (rename, reorder, hide).
|
|
67
67
|
- **Flows** — in the side rail: the journeys people can start, run by `@xeplr/workflow-api` inside the API.
|
|
68
|
-
- **A phone app** — `mobile/` (Expo): every form added to the menu, its list, add / edit, its buttons and the journeys, drawn natively from what the server holds — no screen code, nothing to change when a form is added (see `mobile/README.md`).
|
|
68
|
+
- **A phone app** — `mobile/` (Expo): every form added to the menu, its list, add / edit, its buttons and the journeys, drawn natively from what the server holds — no screen code, nothing to change when a form is added (see `mobile/README.md`).
|
|
69
69
|
- **Functions** — `api/functions/`: your code, registered by event and picked by name in a screen's events or a flow step (see "Functions" below).
|
|
70
70
|
|
|
71
71
|
## Create a new UI
|
|
@@ -52,9 +52,11 @@ Designing (Configure UI) and the admin screens stay on a computer.
|
|
|
52
52
|
| **Dates** | the phone's own date picker |
|
|
53
53
|
| **Journeys** | Start, then each screen with **Next**, as on the website |
|
|
54
54
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
company
|
|
55
|
+
Every control a form can hold is on the phone — a `table` as a card per row
|
|
56
|
+
with a tick, a `subform` drawn inside its form. When the app's data is split by
|
|
57
|
+
company, the first page after signing in is choosing it (and the workspace),
|
|
58
|
+
as on the website; **Switch company** is on the menu. Designing (Configure UI)
|
|
59
|
+
and the admin screens are for a computer.
|
|
58
60
|
|
|
59
61
|
## Layout
|
|
60
62
|
|
|
@@ -13,8 +13,8 @@
|
|
|
13
13
|
"dependencies": {
|
|
14
14
|
"@react-native-async-storage/async-storage": "2.2.0",
|
|
15
15
|
"@react-native-community/datetimepicker": "9.2.1",
|
|
16
|
-
"@xeplr/ui-account": "^1.
|
|
17
|
-
"@xeplr/ui-factory": "^1.
|
|
16
|
+
"@xeplr/ui-account": "^1.8.0",
|
|
17
|
+
"@xeplr/ui-factory": "^1.21.0",
|
|
18
18
|
"expo": "~58.0.26",
|
|
19
19
|
"expo-build-properties": "~58.0.12",
|
|
20
20
|
"expo-camera": "~58.0.14",
|
|
@@ -14,6 +14,7 @@ import { CameraView, Camera } from 'expo-camera'
|
|
|
14
14
|
import { hydrateStorage, startSession, useSession } from '@xeplr/ui-account/native'
|
|
15
15
|
import { NativeHostProvider, NativeInsets, NativeTheme, DARK, LIGHT, useNativeTheme } from '@xeplr/ui-factory/native'
|
|
16
16
|
import { APP_NAME, BUILT_IN_SERVER, devServer } from '../lib/app.js'
|
|
17
|
+
__MT_MOBILE_IMPORTS__
|
|
17
18
|
|
|
18
19
|
const PICKERS = { ImagePicker, DocumentPicker, DateTimePicker, DateTimePickerAndroid, CameraView, Camera }
|
|
19
20
|
|
|
@@ -23,6 +24,7 @@ function Frame() {
|
|
|
23
24
|
const { signedIn, server } = useSession()
|
|
24
25
|
const segments = useSegments()
|
|
25
26
|
if (!signedIn && segments[0] !== 'login') return <Redirect href="/login" />
|
|
27
|
+
__MT_MOBILE_GATE__
|
|
26
28
|
return (
|
|
27
29
|
<NativeInsets value={insets}>
|
|
28
30
|
<NativeHostProvider modules={PICKERS} baseUrl={server} appName={APP_NAME}>
|
|
@@ -39,7 +41,11 @@ export default function RootLayout() {
|
|
|
39
41
|
const [failed, setFailed] = useState(null)
|
|
40
42
|
useEffect(() => {
|
|
41
43
|
hydrateStorage({ AsyncStorage, prefix: '__NAME__', web: Platform.OS === 'web' })
|
|
42
|
-
.then(() => {
|
|
44
|
+
.then(() => {
|
|
45
|
+
__MT_MOBILE_REGISTER__
|
|
46
|
+
startSession({ builtInServer: BUILT_IN_SERVER, suggested: devServer() })
|
|
47
|
+
setReady(true)
|
|
48
|
+
}, (e) => setFailed(e.message || 'Could not start'))
|
|
43
49
|
}, [])
|
|
44
50
|
if (failed) return <View style={{ flex: 1, padding: 24, justifyContent: 'center' }}><Text>{failed}</Text></View>
|
|
45
51
|
if (!ready) return null
|
|
@@ -3,13 +3,13 @@ import { useCallback, useState } from 'react'
|
|
|
3
3
|
import { Stack, router, useFocusEffect, useLocalSearchParams } from 'expo-router'
|
|
4
4
|
import { useSession } from '@xeplr/ui-account/native'
|
|
5
5
|
import { NativeFormRecords, phoneMenu } from '@xeplr/ui-factory/native'
|
|
6
|
-
import { factory } from '../../../lib/app.js'
|
|
6
|
+
import { factory, PAGES } from '../../../lib/app.js'
|
|
7
7
|
|
|
8
8
|
export default function FormRecords() {
|
|
9
9
|
const { form } = useLocalSearchParams()
|
|
10
10
|
const { access } = useSession()
|
|
11
11
|
const key = String(form)
|
|
12
|
-
const item = phoneMenu(access).find((m) => m.form === key)
|
|
12
|
+
const item = phoneMenu(access, { pages: PAGES }).find((m) => m.form === key)
|
|
13
13
|
// Coming back from a record reads the list again: what was done there shows here.
|
|
14
14
|
const [tick, setTick] = useState(0)
|
|
15
15
|
useFocusEffect(useCallback(() => { setTick((n) => n + 1) }, []))
|
|
@@ -4,7 +4,7 @@ import { Text, View } from 'react-native'
|
|
|
4
4
|
import { Stack, router } from 'expo-router'
|
|
5
5
|
import { useSession, signOut } from '@xeplr/ui-account/native'
|
|
6
6
|
import { Btn, NativeMenu, phoneMenu, useNativeTheme } from '@xeplr/ui-factory/native'
|
|
7
|
-
import { APP_NAME } from '../lib/app.js'
|
|
7
|
+
import { APP_NAME, PAGES } from '../lib/app.js'
|
|
8
8
|
|
|
9
9
|
export default function Menu() {
|
|
10
10
|
const t = useNativeTheme()
|
|
@@ -13,7 +13,7 @@ export default function Menu() {
|
|
|
13
13
|
return (
|
|
14
14
|
<>
|
|
15
15
|
<Stack.Screen options={{ title: APP_NAME }} />
|
|
16
|
-
<NativeMenu items={phoneMenu(access)} onOpen={open}
|
|
16
|
+
<NativeMenu items={phoneMenu(access, { pages: PAGES })} onOpen={open}
|
|
17
17
|
header={(
|
|
18
18
|
<View style={{ marginBottom: 16 }}>
|
|
19
19
|
<Text style={{ color: t.ink, fontSize: 22, fontWeight: '800' }}>{APP_NAME}</Text>
|
|
@@ -23,6 +23,7 @@ export default function Menu() {
|
|
|
23
23
|
footer={(
|
|
24
24
|
<View style={{ marginTop: 24 }}>
|
|
25
25
|
<Text style={{ color: t.muted, marginBottom: 8, fontSize: 12 }}>{server}</Text>
|
|
26
|
+
__MT_MOBILE_SWITCH__
|
|
26
27
|
<Btn label="Sign out" onPress={() => { signOut(); router.replace('/login') }} />
|
|
27
28
|
</View>
|
|
28
29
|
)} />
|
|
@@ -28,3 +28,14 @@ export function devServer() {
|
|
|
28
28
|
// when they are made, so one of each is enough.
|
|
29
29
|
export const factory = createFactoryApi({ fetch: authFetch, fetchFile: authFetchFile, base: '/api' })
|
|
30
30
|
export const flows = createFlowsApi({ fetch: authFetch, base: '/api/workflow' })
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The app's own pages that are drawn from a form, by their menu key — on the
|
|
34
|
+
* phone as that form's list. Forms added in Configure UI need nothing here;
|
|
35
|
+
* a page added in code with its own menu key is listed like Tasks.
|
|
36
|
+
*/
|
|
37
|
+
export const PAGES = {
|
|
38
|
+
Tasks: { form: 'task' }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
__MT_MOBILE_TENANCY__
|
package/templates/ui/pages.json
CHANGED
|
@@ -6,12 +6,11 @@
|
|
|
6
6
|
{ "path": "/tasks/:id", "screen": "task_edit" },
|
|
7
7
|
{ "path": "/forms/:form", "screen": "every form's list" },
|
|
8
8
|
{ "path": "/journey/:flow", "screen": "a flow's screens" },
|
|
9
|
-
{ "path": "/journey/:flow/:run", "screen": "a flow's screens" }
|
|
9
|
+
{ "path": "/journey/:flow/:run", "screen": "a flow's screens" },
|
|
10
|
+
{ "path": "/flows", "menu": "Flows", "screen": "the journeys to start — NativeFlows on a phone" }__MT_PAGES_SELECT__
|
|
10
11
|
],
|
|
11
12
|
"webOnly": [
|
|
12
|
-
__MT_PAGES_SELECT__
|
|
13
13
|
{ "path": "/home", "menu": "Home", "reason": "The starter page that proves sign-in and the API work — replace it with your first page." },
|
|
14
|
-
{ "path": "/flows", "menu": "Flows", "reason": "The flows list and designer are tools for building, used on a computer." },
|
|
15
14
|
{ "path": "/configure/flows/:flow", "reason": "The flow designer is a tool for building, used on a computer." },
|
|
16
15
|
{ "path": "/configure", "menu": "Configure UI", "reason": "The form designer and menu settings are tools for building, used on a computer." },
|
|
17
16
|
{ "path": "/configure/forms/:form/design/:part", "reason": "The form designer is a tool for building, used on a computer." }
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// WHERE ARE YOU WORKING? — the company (and workspace) this app's data belongs
|
|
2
|
+
// to, for apps whose data is split by company: @xeplr/ui-account's own picker,
|
|
3
|
+
// the same picking as the website's. What the person may see depends on the
|
|
4
|
+
// company (its admin there, a viewer elsewhere), so the menu is read again.
|
|
5
|
+
import { Stack, router } from 'expo-router'
|
|
6
|
+
import { NativeScopePicker, refreshAccess } from '@xeplr/ui-account/native'
|
|
7
|
+
import { TENANCY } from '../lib/app.js'
|
|
8
|
+
|
|
9
|
+
export default function Select() {
|
|
10
|
+
return (
|
|
11
|
+
<>
|
|
12
|
+
<Stack.Screen options={{ title: 'Choose', headerBackVisible: router.canGoBack() }} />
|
|
13
|
+
<NativeScopePicker levels={TENANCY.levels} onChosen={() => { refreshAccess().finally(() => router.replace('/')) }} />
|
|
14
|
+
</>
|
|
15
|
+
)
|
|
16
|
+
}
|
|
@@ -4,14 +4,13 @@
|
|
|
4
4
|
// authFetch sends it as a header on every request, the API stamps new rows
|
|
5
5
|
// with it and reads only its rows.
|
|
6
6
|
//
|
|
7
|
-
//
|
|
8
|
-
//
|
|
9
|
-
//
|
|
10
|
-
|
|
7
|
+
// The picking is @xeplr/ui-account's (useScopeChoice) — the same as the phone
|
|
8
|
+
// app's (NativeScopePicker): each level's list, the last choice already
|
|
9
|
+
// chosen, a list of one chosen for you, adding a new one. This page only
|
|
10
|
+
// draws it. Shown once per browser tab (ScopeGate below); "__MT_SWITCH_NAME__"
|
|
11
|
+
// in the side rail comes back here.
|
|
11
12
|
import { Navigate, useLocation, useNavigate } from 'react-router-dom'
|
|
12
|
-
import {
|
|
13
|
-
authFetch, getActiveScope, setActiveScope, getLastScope, setLastScope, saveReturnTo, consumeReturnTo
|
|
14
|
-
} from '@xeplr/ui-account'
|
|
13
|
+
import { useScopeChoice, scopeChosen, saveReturnTo, consumeReturnTo } from '@xeplr/ui-account'
|
|
15
14
|
import { tenancy } from '../tenancy.js'
|
|
16
15
|
import './SelectScope.css'
|
|
17
16
|
|
|
@@ -23,8 +22,7 @@ const setConfirmed = (yes) => { try { yes ? sessionStorage.setItem(CONFIRMED, '1
|
|
|
23
22
|
/** Lets the app's pages render only once every level is chosen in this tab. */
|
|
24
23
|
export function ScopeGate({ children }) {
|
|
25
24
|
const location = useLocation()
|
|
26
|
-
|
|
27
|
-
if (!complete || !confirmed()) {
|
|
25
|
+
if (!scopeChosen(tenancy.levels) || !confirmed()) {
|
|
28
26
|
saveReturnTo(location)
|
|
29
27
|
return <Navigate to="/select" replace />
|
|
30
28
|
}
|
|
@@ -33,32 +31,10 @@ export function ScopeGate({ children }) {
|
|
|
33
31
|
|
|
34
32
|
export default function SelectScope() {
|
|
35
33
|
const navigate = useNavigate()
|
|
36
|
-
|
|
37
|
-
// Made active straight away — before the lists below load, since a level's
|
|
38
|
-
// list is asked for with the level above it as a header.
|
|
39
|
-
const [chosen, setChosen] = useState(() => {
|
|
40
|
-
const start = Object.fromEntries(tenancy.levels.map((l) => [l.key, getActiveScope(l.key) || getLastScope(l.key) || null]))
|
|
41
|
-
tenancy.levels.forEach((l) => setActiveScope(l.key, start[l.key]))
|
|
42
|
-
return start
|
|
43
|
-
})
|
|
44
|
-
|
|
45
|
-
const choose = useCallback((index, item) => {
|
|
46
|
-
setChosen((c) => {
|
|
47
|
-
const next = { ...c, [tenancy.levels[index].key]: item }
|
|
48
|
-
// A different parent: whatever was chosen below it belonged to the old one.
|
|
49
|
-
tenancy.levels.slice(index + 1).forEach((l) => { next[l.key] = null })
|
|
50
|
-
return next
|
|
51
|
-
})
|
|
52
|
-
// Set as it is chosen, so the level below lists this one's children.
|
|
53
|
-
setActiveScope(tenancy.levels[index].key, item)
|
|
54
|
-
tenancy.levels.slice(index + 1).forEach((l) => setActiveScope(l.key, null))
|
|
55
|
-
setConfirmed(false)
|
|
56
|
-
}, [])
|
|
57
|
-
|
|
58
|
-
const complete = tenancy.levels.every((l) => chosen[l.key])
|
|
34
|
+
const choice = useScopeChoice({ levels: tenancy.levels })
|
|
59
35
|
|
|
60
36
|
const go = () => {
|
|
61
|
-
|
|
37
|
+
if (!choice.finish()) return
|
|
62
38
|
setConfirmed(true)
|
|
63
39
|
navigate(consumeReturnTo() || '/home', { replace: true })
|
|
64
40
|
}
|
|
@@ -68,87 +44,36 @@ export default function SelectScope() {
|
|
|
68
44
|
<h1>Where are you working?</h1>
|
|
69
45
|
<p className="app-page-lead">Everything you see and save next belongs to this choice.</p>
|
|
70
46
|
|
|
71
|
-
{
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
<
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
47
|
+
{choice.levels.map((level, i) => (
|
|
48
|
+
<section key={level.key} className={'app-select-level' + (level.enabled ? '' : ' is-disabled')}>
|
|
49
|
+
<h2>{level.label}</h2>
|
|
50
|
+
{!level.enabled && <p className="app-sub">Choose the one above first.</p>}
|
|
51
|
+
{level.enabled && level.items === null && !level.error && <p className="app-sub">Loading…</p>}
|
|
52
|
+
{level.error && <div className="app-error">{level.error}</div>}
|
|
53
|
+
{level.enabled && level.items && (
|
|
54
|
+
<>
|
|
55
|
+
{level.items.length === 0 && <p className="app-sub">No {level.noun} yet — add the first one.</p>}
|
|
56
|
+
<div className="app-select-items">
|
|
57
|
+
{level.items.map((item) => {
|
|
58
|
+
const on = !!level.value && level.value.id === item.id
|
|
59
|
+
return (
|
|
60
|
+
<button key={item.id} type="button" className={'app-select-item' + (on ? ' is-chosen' : '')} aria-pressed={on}
|
|
61
|
+
onClick={() => { setConfirmed(false); choice.choose(i, item) }}>
|
|
62
|
+
{item.name}
|
|
63
|
+
</button>
|
|
64
|
+
)
|
|
65
|
+
})}
|
|
66
|
+
</div>
|
|
67
|
+
<form className="app-select-add" onSubmit={(e) => { e.preventDefault(); setConfirmed(false); choice.add(i) }}>
|
|
68
|
+
<input value={level.adding} onChange={(e) => choice.setAdding(i, e.target.value)} placeholder={'New ' + level.noun + ' name'} aria-label={'New ' + level.noun + ' name'} />
|
|
69
|
+
<button type="submit" className="app-btn app-btn-quiet" disabled={level.busy || !level.adding.trim()}>Add {level.noun}</button>
|
|
70
|
+
</form>
|
|
71
|
+
</>
|
|
72
|
+
)}
|
|
73
|
+
</section>
|
|
74
|
+
))}
|
|
83
75
|
|
|
84
|
-
<button type="button" className="app-btn" disabled={!complete} onClick={go}>Continue</button>
|
|
76
|
+
<button type="button" className="app-btn" disabled={!choice.complete} onClick={go}>Continue</button>
|
|
85
77
|
</div>
|
|
86
78
|
)
|
|
87
79
|
}
|
|
88
|
-
|
|
89
|
-
function LevelChoice({ level, enabled, value, onChoose }) {
|
|
90
|
-
const [items, setItems] = useState(null)
|
|
91
|
-
const [error, setError] = useState(null)
|
|
92
|
-
const [adding, setAdding] = useState('')
|
|
93
|
-
const [busy, setBusy] = useState(false)
|
|
94
|
-
const noun = level.label.toLowerCase()
|
|
95
|
-
|
|
96
|
-
useEffect(() => {
|
|
97
|
-
if (!enabled) return
|
|
98
|
-
authFetch('/api/tenants/' + level.key).then((body) => {
|
|
99
|
-
const rows = body.dataArray || []
|
|
100
|
-
setItems(rows)
|
|
101
|
-
// A remembered choice that is no longer on the list is not a choice.
|
|
102
|
-
if (value && !rows.some((r) => r.id === value.id)) onChoose(null)
|
|
103
|
-
else if (!value && rows.length === 1) onChoose(rows[0])
|
|
104
|
-
}, (e) => setError(e.message))
|
|
105
|
-
}, [enabled, level.key]) // eslint-disable-line react-hooks/exhaustive-deps
|
|
106
|
-
|
|
107
|
-
const add = async (e) => {
|
|
108
|
-
e.preventDefault()
|
|
109
|
-
setBusy(true)
|
|
110
|
-
try {
|
|
111
|
-
const body = await authFetch('/api/tenants/' + level.key, { method: 'POST', body: JSON.stringify({ name: adding }) })
|
|
112
|
-
const created = body.dataArray[0]
|
|
113
|
-
setItems((list) => [...(list || []), created].sort((a, b) => a.name.localeCompare(b.name)))
|
|
114
|
-
setAdding('')
|
|
115
|
-
setError(null)
|
|
116
|
-
onChoose(created)
|
|
117
|
-
} catch (err) {
|
|
118
|
-
setError(err.message)
|
|
119
|
-
} finally {
|
|
120
|
-
setBusy(false)
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
return (
|
|
125
|
-
<section className={'app-select-level' + (enabled ? '' : ' is-disabled')}>
|
|
126
|
-
<h2>{level.label}</h2>
|
|
127
|
-
{!enabled && <p className="app-sub">Choose the one above first.</p>}
|
|
128
|
-
{enabled && items === null && !error && <p className="app-sub">Loading…</p>}
|
|
129
|
-
{error && <div className="app-error">{error}</div>}
|
|
130
|
-
{enabled && items && (
|
|
131
|
-
<>
|
|
132
|
-
{items.length === 0 && <p className="app-sub">No {noun} yet — add the first one.</p>}
|
|
133
|
-
<div className="app-select-items">
|
|
134
|
-
{items.map((item) => (
|
|
135
|
-
<button
|
|
136
|
-
key={item.id}
|
|
137
|
-
type="button"
|
|
138
|
-
className={'app-select-item' + (value && value.id === item.id ? ' is-chosen' : '')}
|
|
139
|
-
aria-pressed={!!value && value.id === item.id}
|
|
140
|
-
onClick={() => onChoose(item)}
|
|
141
|
-
>
|
|
142
|
-
{item.name}
|
|
143
|
-
</button>
|
|
144
|
-
))}
|
|
145
|
-
</div>
|
|
146
|
-
<form className="app-select-add" onSubmit={add}>
|
|
147
|
-
<input value={adding} onChange={(e) => setAdding(e.target.value)} placeholder={'New ' + noun + ' name'} aria-label={'New ' + noun + ' name'} />
|
|
148
|
-
<button type="submit" className="app-btn app-btn-quiet" disabled={busy || !adding.trim()}>Add {noun}</button>
|
|
149
|
-
</form>
|
|
150
|
-
</>
|
|
151
|
-
)}
|
|
152
|
-
</section>
|
|
153
|
-
)
|
|
154
|
-
}
|