@terreno/admin-frontend 0.16.1 → 0.17.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/dist/AdminFieldRenderer.js +3 -3
- package/dist/AdminFieldRenderer.js.map +1 -1
- package/dist/AdminFieldRendererCore.js +4 -4
- package/dist/AdminFieldRendererCore.js.map +1 -1
- package/dist/AdminModelForm.d.ts +6 -1
- package/dist/AdminModelForm.js +9 -4
- package/dist/AdminModelForm.js.map +1 -1
- package/dist/AdminModelList.d.ts +7 -2
- package/dist/AdminModelList.js +10 -4
- package/dist/AdminModelList.js.map +1 -1
- package/dist/AdminModelTable.d.ts +7 -2
- package/dist/AdminModelTable.js +14 -6
- package/dist/AdminModelTable.js.map +1 -1
- package/dist/AdminNestedArrayField.d.ts +6 -1
- package/dist/AdminNestedArrayField.js +8 -4
- package/dist/AdminNestedArrayField.js.map +1 -1
- package/dist/AdminPrimitiveArrayField.d.ts +6 -1
- package/dist/AdminPrimitiveArrayField.js +3 -3
- package/dist/AdminPrimitiveArrayField.js.map +1 -1
- package/dist/AdminScriptList.d.ts +7 -2
- package/dist/AdminScriptList.js +9 -3
- package/dist/AdminScriptList.js.map +1 -1
- package/dist/AdminScriptRunModal.d.ts +7 -2
- package/dist/AdminScriptRunModal.js +4 -2
- package/dist/AdminScriptRunModal.js.map +1 -1
- package/dist/AdminVersionConfig.d.ts +7 -2
- package/dist/AdminVersionConfig.js +6 -4
- package/dist/AdminVersionConfig.js.map +1 -1
- package/dist/ConsentFormEditor.d.ts +7 -2
- package/dist/ConsentFormEditor.js +4 -2
- package/dist/ConsentFormEditor.js.map +1 -1
- package/dist/ConsentFormList.d.ts +7 -2
- package/dist/ConsentFormList.js +4 -2
- package/dist/ConsentFormList.js.map +1 -1
- package/dist/ConsentResponseViewer.d.ts +7 -2
- package/dist/ConsentResponseViewer.js +4 -2
- package/dist/ConsentResponseViewer.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +41 -2
- package/dist/types.js +12 -0
- package/dist/types.js.map +1 -1
- package/dist/useAdminConfig.d.ts +2 -2
- package/dist/useAdminConfig.js +4 -4
- package/dist/useAdminScripts.d.ts +1 -1
- package/dist/useAdminScripts.js +5 -5
- package/package.json +2 -2
- package/src/AdminFieldRenderer.tsx +6 -0
- package/src/AdminFieldRendererCore.tsx +8 -0
- package/src/AdminModelForm.tsx +17 -4
- package/src/AdminModelList.test.tsx +41 -5
- package/src/AdminModelList.tsx +22 -5
- package/src/AdminModelTable.test.tsx +58 -5
- package/src/AdminModelTable.tsx +27 -7
- package/src/AdminNestedArrayField.tsx +21 -2
- package/src/AdminPrimitiveArrayField.tsx +12 -1
- package/src/AdminScriptList.tsx +22 -5
- package/src/AdminScriptRunModal.tsx +11 -3
- package/src/AdminVersionConfig.tsx +17 -6
- package/src/ConsentFormEditor.tsx +11 -3
- package/src/ConsentFormList.tsx +11 -3
- package/src/ConsentResponseViewer.tsx +16 -4
- package/src/index.tsx +1 -1
- package/src/isolated/AdminNestedArrayField.isolated.tsx +2 -2
- package/src/isolated/AdminScriptRunModal.isolated.tsx +108 -28
- package/src/types.ts +48 -2
- package/src/useAdminConfig.ts +4 -4
- package/src/useAdminScripts.ts +5 -5
- package/src/AdminScriptRunModal.test.tsx +0 -132
package/dist/useAdminConfig.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ import type { AdminApi, AdminConfigResponse } from "./types";
|
|
|
7
7
|
* field types, required fields, references, and display settings.
|
|
8
8
|
*
|
|
9
9
|
* @param api - RTK Query API instance to inject the endpoint into
|
|
10
|
-
* @param
|
|
10
|
+
* @param apiBase - Base URL where admin API requests are sent (e.g., "/admin")
|
|
11
11
|
* @returns Object with `config` (model metadata), `isLoading`, and `error`
|
|
12
12
|
*
|
|
13
13
|
* @example
|
|
@@ -28,7 +28,7 @@ import type { AdminApi, AdminConfigResponse } from "./types";
|
|
|
28
28
|
* @see AdminConfigResponse for the returned configuration structure
|
|
29
29
|
* @see AdminModelList for usage in the model list screen
|
|
30
30
|
*/
|
|
31
|
-
export declare const useAdminConfig: (api: AdminApi,
|
|
31
|
+
export declare const useAdminConfig: (api: AdminApi, apiBase: string) => {
|
|
32
32
|
config: AdminConfigResponse | null;
|
|
33
33
|
error: any;
|
|
34
34
|
isLoading: any;
|
package/dist/useAdminConfig.js
CHANGED
|
@@ -8,7 +8,7 @@ const ENDPOINT_NAME = "adminConfig";
|
|
|
8
8
|
* field types, required fields, references, and display settings.
|
|
9
9
|
*
|
|
10
10
|
* @param api - RTK Query API instance to inject the endpoint into
|
|
11
|
-
* @param
|
|
11
|
+
* @param apiBase - Base URL where admin API requests are sent (e.g., "/admin")
|
|
12
12
|
* @returns Object with `config` (model metadata), `isLoading`, and `error`
|
|
13
13
|
*
|
|
14
14
|
* @example
|
|
@@ -29,20 +29,20 @@ const ENDPOINT_NAME = "adminConfig";
|
|
|
29
29
|
* @see AdminConfigResponse for the returned configuration structure
|
|
30
30
|
* @see AdminModelList for usage in the model list screen
|
|
31
31
|
*/
|
|
32
|
-
export const useAdminConfig = (api,
|
|
32
|
+
export const useAdminConfig = (api, apiBase) => {
|
|
33
33
|
const enhancedApi = useMemo(() => {
|
|
34
34
|
return api.injectEndpoints({
|
|
35
35
|
endpoints: (build) => ({
|
|
36
36
|
[ENDPOINT_NAME]: build.query({
|
|
37
37
|
query: () => ({
|
|
38
38
|
method: "GET",
|
|
39
|
-
url: `${
|
|
39
|
+
url: `${apiBase}/config`,
|
|
40
40
|
}),
|
|
41
41
|
}),
|
|
42
42
|
}),
|
|
43
43
|
overrideExisting: true,
|
|
44
44
|
});
|
|
45
|
-
}, [api,
|
|
45
|
+
}, [api, apiBase]);
|
|
46
46
|
// noExplicitAny: RTK Query generates hook names dynamically; not statically expressible
|
|
47
47
|
// biome-ignore lint/suspicious/noExplicitAny: dynamic hook lookup on RTK Query enhanced API
|
|
48
48
|
const useConfigQuery = enhancedApi.useAdminConfigQuery;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { AdminApi, BackgroundTask } from "./types";
|
|
2
|
-
export declare const useAdminScripts: (api: AdminApi,
|
|
2
|
+
export declare const useAdminScripts: (api: AdminApi, apiBase: string) => {
|
|
3
3
|
useCancelScriptTaskMutation: () => [(taskId: string) => {
|
|
4
4
|
unwrap: () => Promise<{
|
|
5
5
|
task: BackgroundTask;
|
package/dist/useAdminScripts.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useMemo } from "react";
|
|
2
|
-
export const useAdminScripts = (api,
|
|
2
|
+
export const useAdminScripts = (api, apiBase) => {
|
|
3
3
|
const enhancedApi = useMemo(() => {
|
|
4
4
|
return api.injectEndpoints({
|
|
5
5
|
endpoints: (build) => ({
|
|
@@ -7,26 +7,26 @@ export const useAdminScripts = (api, baseUrl) => {
|
|
|
7
7
|
invalidatesTags: ["admin_scriptTask"],
|
|
8
8
|
query: (taskId) => ({
|
|
9
9
|
method: "DELETE",
|
|
10
|
-
url: `${
|
|
10
|
+
url: `${apiBase}/scripts/tasks/${taskId}`,
|
|
11
11
|
}),
|
|
12
12
|
}),
|
|
13
13
|
adminGetScriptTask: build.query({
|
|
14
14
|
providesTags: ["admin_scriptTask"],
|
|
15
15
|
query: (taskId) => ({
|
|
16
16
|
method: "GET",
|
|
17
|
-
url: `${
|
|
17
|
+
url: `${apiBase}/scripts/tasks/${taskId}`,
|
|
18
18
|
}),
|
|
19
19
|
}),
|
|
20
20
|
adminRunScript: build.mutation({
|
|
21
21
|
query: ({ name, wetRun }) => ({
|
|
22
22
|
method: "POST",
|
|
23
|
-
url: `${
|
|
23
|
+
url: `${apiBase}/scripts/${name}/run?wetRun=${wetRun}`,
|
|
24
24
|
}),
|
|
25
25
|
}),
|
|
26
26
|
}),
|
|
27
27
|
overrideExisting: true,
|
|
28
28
|
});
|
|
29
|
-
}, [api,
|
|
29
|
+
}, [api, apiBase]);
|
|
30
30
|
// noExplicitAny: RTK Query generates hook names dynamically; not statically expressible
|
|
31
31
|
// biome-ignore lint/suspicious/noExplicitAny: dynamic hook lookup on RTK Query enhanced API
|
|
32
32
|
const enhanced = enhancedApi;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"dependencies": {
|
|
3
|
-
"@terreno/ui": "0.
|
|
3
|
+
"@terreno/ui": "0.17.0",
|
|
4
4
|
"expo-router": "~6.0.19",
|
|
5
5
|
"jspdf": "^4.2.0",
|
|
6
6
|
"lodash": "^4.17.23",
|
|
@@ -51,5 +51,5 @@
|
|
|
51
51
|
"test:coverage": "bun run ../scripts/check-coverage.ts"
|
|
52
52
|
},
|
|
53
53
|
"types": "dist/index.d.ts",
|
|
54
|
-
"version": "0.
|
|
54
|
+
"version": "0.17.0"
|
|
55
55
|
}
|
|
@@ -22,6 +22,8 @@ export const AdminFieldRenderer: React.FC<AdminFieldRendererProps> = ({
|
|
|
22
22
|
onChange,
|
|
23
23
|
errorText,
|
|
24
24
|
baseUrl,
|
|
25
|
+
apiBase,
|
|
26
|
+
routeBase,
|
|
25
27
|
api,
|
|
26
28
|
modelConfigs,
|
|
27
29
|
parentFormState,
|
|
@@ -31,6 +33,7 @@ export const AdminFieldRenderer: React.FC<AdminFieldRendererProps> = ({
|
|
|
31
33
|
return (
|
|
32
34
|
<AdminNestedArrayField
|
|
33
35
|
api={api}
|
|
36
|
+
apiBase={apiBase}
|
|
34
37
|
baseUrl={baseUrl}
|
|
35
38
|
errorText={errorText}
|
|
36
39
|
helperText={fieldConfig.description}
|
|
@@ -39,6 +42,7 @@ export const AdminFieldRenderer: React.FC<AdminFieldRendererProps> = ({
|
|
|
39
42
|
onChange={onChange}
|
|
40
43
|
parentFormState={parentFormState}
|
|
41
44
|
refRenderers={refRenderers}
|
|
45
|
+
routeBase={routeBase}
|
|
42
46
|
title={startCase(fieldKey)}
|
|
43
47
|
value={Array.isArray(value) ? (value as Record<string, AdminFieldValue>[]) : []}
|
|
44
48
|
/>
|
|
@@ -48,6 +52,7 @@ export const AdminFieldRenderer: React.FC<AdminFieldRendererProps> = ({
|
|
|
48
52
|
return (
|
|
49
53
|
<AdminFieldRendererCore
|
|
50
54
|
api={api}
|
|
55
|
+
apiBase={apiBase}
|
|
51
56
|
baseUrl={baseUrl}
|
|
52
57
|
errorText={errorText}
|
|
53
58
|
fieldConfig={fieldConfig}
|
|
@@ -56,6 +61,7 @@ export const AdminFieldRenderer: React.FC<AdminFieldRendererProps> = ({
|
|
|
56
61
|
onChange={onChange}
|
|
57
62
|
parentFormState={parentFormState}
|
|
58
63
|
refRenderers={refRenderers}
|
|
64
|
+
routeBase={routeBase}
|
|
59
65
|
value={value}
|
|
60
66
|
/>
|
|
61
67
|
);
|
|
@@ -98,6 +98,8 @@ export const AdminFieldRendererCore: React.FC<AdminFieldRendererCoreProps> = ({
|
|
|
98
98
|
onChange,
|
|
99
99
|
errorText,
|
|
100
100
|
baseUrl,
|
|
101
|
+
apiBase,
|
|
102
|
+
routeBase,
|
|
101
103
|
api,
|
|
102
104
|
modelConfigs,
|
|
103
105
|
parentFormState,
|
|
@@ -199,11 +201,13 @@ export const AdminFieldRendererCore: React.FC<AdminFieldRendererCoreProps> = ({
|
|
|
199
201
|
return (
|
|
200
202
|
<CustomRenderer
|
|
201
203
|
api={api}
|
|
204
|
+
apiBase={apiBase}
|
|
202
205
|
baseUrl={baseUrl}
|
|
203
206
|
errorText={errorText}
|
|
204
207
|
helperText={helperText}
|
|
205
208
|
onChange={onChange}
|
|
206
209
|
refModelName={fieldConfig.ref}
|
|
210
|
+
routeBase={routeBase}
|
|
207
211
|
routePath={refModel?.routePath ?? ""}
|
|
208
212
|
title={label}
|
|
209
213
|
value={typeof value === "string" ? value : ""}
|
|
@@ -214,11 +218,13 @@ export const AdminFieldRendererCore: React.FC<AdminFieldRendererCoreProps> = ({
|
|
|
214
218
|
return (
|
|
215
219
|
<AdminRefField
|
|
216
220
|
api={api}
|
|
221
|
+
apiBase={apiBase}
|
|
217
222
|
baseUrl={baseUrl}
|
|
218
223
|
errorText={errorText}
|
|
219
224
|
helperText={helperText}
|
|
220
225
|
onChange={onChange}
|
|
221
226
|
refModelName={fieldConfig.ref}
|
|
227
|
+
routeBase={routeBase}
|
|
222
228
|
routePath={refModel.routePath}
|
|
223
229
|
title={label}
|
|
224
230
|
value={typeof value === "string" ? value : ""}
|
|
@@ -352,6 +358,7 @@ export const AdminFieldRendererCore: React.FC<AdminFieldRendererCoreProps> = ({
|
|
|
352
358
|
return (
|
|
353
359
|
<AdminPrimitiveArrayField
|
|
354
360
|
api={api}
|
|
361
|
+
apiBase={apiBase}
|
|
355
362
|
baseUrl={baseUrl}
|
|
356
363
|
errorText={errorText}
|
|
357
364
|
helperText={helperText}
|
|
@@ -361,6 +368,7 @@ export const AdminFieldRendererCore: React.FC<AdminFieldRendererCoreProps> = ({
|
|
|
361
368
|
modelConfigs={modelConfigs}
|
|
362
369
|
onChange={onChange}
|
|
363
370
|
refRenderers={refRenderers}
|
|
371
|
+
routeBase={routeBase}
|
|
364
372
|
title={label}
|
|
365
373
|
value={Array.isArray(value) ? value : []}
|
|
366
374
|
/>
|
package/src/AdminModelForm.tsx
CHANGED
|
@@ -9,12 +9,17 @@ import type {
|
|
|
9
9
|
AdminModelConfig,
|
|
10
10
|
RefRendererMap,
|
|
11
11
|
} from "./types";
|
|
12
|
-
import {SYSTEM_FIELDS} from "./types";
|
|
12
|
+
import {resolveAdminBases, SYSTEM_FIELDS} from "./types";
|
|
13
13
|
import {useAdminApi} from "./useAdminApi";
|
|
14
14
|
import {useAdminConfig} from "./useAdminConfig";
|
|
15
15
|
|
|
16
16
|
interface AdminModelFormProps {
|
|
17
|
-
|
|
17
|
+
/** @deprecated Use `apiBase`/`routeBase`. Kept as a backward-compatible alias. */
|
|
18
|
+
baseUrl?: string;
|
|
19
|
+
/** Base path where admin API requests are sent. Falls back to `baseUrl`. */
|
|
20
|
+
apiBase?: string;
|
|
21
|
+
/** Base path used for in-app navigation. Falls back to `baseUrl`. */
|
|
22
|
+
routeBase?: string;
|
|
18
23
|
api: AdminApi;
|
|
19
24
|
modelName: string;
|
|
20
25
|
mode: "create" | "edit";
|
|
@@ -162,6 +167,8 @@ const EmptyFields: React.FC = () => <Text color="secondaryDark">No editable fiel
|
|
|
162
167
|
*/
|
|
163
168
|
export const AdminModelForm: React.FC<AdminModelFormProps> = ({
|
|
164
169
|
baseUrl,
|
|
170
|
+
apiBase,
|
|
171
|
+
routeBase,
|
|
165
172
|
api,
|
|
166
173
|
modelName,
|
|
167
174
|
mode,
|
|
@@ -171,7 +178,12 @@ export const AdminModelForm: React.FC<AdminModelFormProps> = ({
|
|
|
171
178
|
onSaveSuccess,
|
|
172
179
|
refRenderers,
|
|
173
180
|
}) => {
|
|
174
|
-
const {
|
|
181
|
+
const {apiBase: resolvedApiBase, routeBase: resolvedRouteBase} = resolveAdminBases({
|
|
182
|
+
apiBase,
|
|
183
|
+
baseUrl,
|
|
184
|
+
routeBase,
|
|
185
|
+
});
|
|
186
|
+
const {config, isLoading: isConfigLoading} = useAdminConfig(api, resolvedApiBase);
|
|
175
187
|
const [formState, setFormState] = useState<Record<string, AdminFieldValue>>({});
|
|
176
188
|
const [errors, setErrors] = useState<Record<string, string>>({});
|
|
177
189
|
const [isInitialized, setIsInitialized] = useState(false);
|
|
@@ -358,7 +370,7 @@ export const AdminModelForm: React.FC<AdminModelFormProps> = ({
|
|
|
358
370
|
{editableFields.map(([fieldKey, fieldConfig]) => (
|
|
359
371
|
<AdminFieldRenderer
|
|
360
372
|
api={api}
|
|
361
|
-
|
|
373
|
+
apiBase={resolvedApiBase}
|
|
362
374
|
errorText={errors[fieldKey]}
|
|
363
375
|
fieldConfig={fieldConfig}
|
|
364
376
|
fieldKey={fieldKey}
|
|
@@ -367,6 +379,7 @@ export const AdminModelForm: React.FC<AdminModelFormProps> = ({
|
|
|
367
379
|
onChange={(value: AdminFieldValue) => handleFieldChange(fieldKey, value)}
|
|
368
380
|
parentFormState={formState}
|
|
369
381
|
refRenderers={refRenderers}
|
|
382
|
+
routeBase={resolvedRouteBase}
|
|
370
383
|
value={formState[fieldKey]}
|
|
371
384
|
/>
|
|
372
385
|
))}
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
import {beforeEach, describe, expect, it, mock} from "bun:test";
|
|
4
4
|
import {renderWithTheme} from "@terreno/ui/src/test-utils";
|
|
5
5
|
import React from "react";
|
|
6
|
+
import {act, fireEvent} from "../../ui/node_modules/@testing-library/react-native";
|
|
6
7
|
import type {AdminApi, AdminConfigResponse} from "./types";
|
|
7
8
|
|
|
8
9
|
const routerPush = mock(() => {});
|
|
@@ -15,12 +16,18 @@ const mockConfigState: {data: AdminConfigResponse | null; error: unknown; isLoad
|
|
|
15
16
|
error: null,
|
|
16
17
|
isLoading: false,
|
|
17
18
|
};
|
|
19
|
+
// Records the apiBase passed to useAdminConfig so tests can assert that data
|
|
20
|
+
// fetching uses the resolved API base (not the route base).
|
|
21
|
+
const configApiBaseCalls: string[] = [];
|
|
18
22
|
mock.module("./useAdminConfig", () => ({
|
|
19
|
-
useAdminConfig: () =>
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
23
|
+
useAdminConfig: (_api: unknown, apiBase: string) => {
|
|
24
|
+
configApiBaseCalls.push(apiBase);
|
|
25
|
+
return {
|
|
26
|
+
config: mockConfigState.data,
|
|
27
|
+
error: mockConfigState.error,
|
|
28
|
+
isLoading: mockConfigState.isLoading,
|
|
29
|
+
};
|
|
30
|
+
},
|
|
24
31
|
}));
|
|
25
32
|
|
|
26
33
|
import {AdminModelList} from "./AdminModelList";
|
|
@@ -46,6 +53,7 @@ const baseConfig = {
|
|
|
46
53
|
describe("AdminModelList", () => {
|
|
47
54
|
beforeEach(() => {
|
|
48
55
|
routerPush.mockClear();
|
|
56
|
+
configApiBaseCalls.length = 0;
|
|
49
57
|
mockConfigState.data = null;
|
|
50
58
|
mockConfigState.error = null;
|
|
51
59
|
mockConfigState.isLoading = false;
|
|
@@ -95,4 +103,32 @@ describe("AdminModelList", () => {
|
|
|
95
103
|
expect(getByText("Models")).toBeDefined();
|
|
96
104
|
expect(getByTestId("admin-model-card-User")).toBeDefined();
|
|
97
105
|
});
|
|
106
|
+
|
|
107
|
+
it("fetches config from apiBase but navigates using routeBase when split", async () => {
|
|
108
|
+
mockConfigState.data = {...baseConfig, customScreens: [], scripts: []};
|
|
109
|
+
const {getByLabelText} = renderWithTheme(
|
|
110
|
+
<AdminModelList api={{} as unknown as AdminApi} apiBase="/admin" routeBase="/console" />
|
|
111
|
+
);
|
|
112
|
+
// Data fetching must use the API base.
|
|
113
|
+
expect(configApiBaseCalls).toContain("/admin");
|
|
114
|
+
// Card press must navigate using the route base, not the API base.
|
|
115
|
+
await act(async () => {
|
|
116
|
+
fireEvent.press(getByLabelText("User"));
|
|
117
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
118
|
+
});
|
|
119
|
+
expect(routerPush).toHaveBeenCalledWith("/console/User");
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("uses baseUrl for both fetching and navigation (backward compat)", async () => {
|
|
123
|
+
mockConfigState.data = {...baseConfig, customScreens: [], scripts: []};
|
|
124
|
+
const {getByLabelText} = renderWithTheme(
|
|
125
|
+
<AdminModelList api={{} as unknown as AdminApi} baseUrl="/admin" />
|
|
126
|
+
);
|
|
127
|
+
expect(configApiBaseCalls).toContain("/admin");
|
|
128
|
+
await act(async () => {
|
|
129
|
+
fireEvent.press(getByLabelText("User"));
|
|
130
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
131
|
+
});
|
|
132
|
+
expect(routerPush).toHaveBeenCalledWith("/admin/User");
|
|
133
|
+
});
|
|
98
134
|
});
|
package/src/AdminModelList.tsx
CHANGED
|
@@ -2,11 +2,21 @@ import {Box, Card, Heading, Page, Spinner, Text} from "@terreno/ui";
|
|
|
2
2
|
import type {Href} from "expo-router";
|
|
3
3
|
import {router} from "expo-router";
|
|
4
4
|
import React, {useCallback} from "react";
|
|
5
|
-
import
|
|
5
|
+
import {
|
|
6
|
+
type AdminApi,
|
|
7
|
+
type AdminCustomScreen,
|
|
8
|
+
type AdminModelConfig,
|
|
9
|
+
resolveAdminBases,
|
|
10
|
+
} from "./types";
|
|
6
11
|
import {useAdminConfig} from "./useAdminConfig";
|
|
7
12
|
|
|
8
13
|
interface AdminModelListProps {
|
|
9
|
-
|
|
14
|
+
/** @deprecated Use `apiBase`/`routeBase`. Kept as a backward-compatible alias. */
|
|
15
|
+
baseUrl?: string;
|
|
16
|
+
/** Base path where admin API requests are sent. Falls back to `baseUrl`. */
|
|
17
|
+
apiBase?: string;
|
|
18
|
+
/** Base path used for in-app navigation. Falls back to `baseUrl`. */
|
|
19
|
+
routeBase?: string;
|
|
10
20
|
api: AdminApi;
|
|
11
21
|
/** Path to navigate to for the configuration screen. When provided, a Configuration card is shown. */
|
|
12
22
|
configurationPath?: string;
|
|
@@ -118,17 +128,24 @@ const CustomScreenCard: React.FC<{screen: AdminCustomScreen; onPress: () => void
|
|
|
118
128
|
*/
|
|
119
129
|
export const AdminModelList: React.FC<AdminModelListProps> = ({
|
|
120
130
|
baseUrl,
|
|
131
|
+
apiBase,
|
|
132
|
+
routeBase,
|
|
121
133
|
api,
|
|
122
134
|
configurationPath,
|
|
123
135
|
customScreens: propCustomScreens,
|
|
124
136
|
}) => {
|
|
125
|
-
const {
|
|
137
|
+
const {apiBase: resolvedApiBase, routeBase: resolvedRouteBase} = resolveAdminBases({
|
|
138
|
+
apiBase,
|
|
139
|
+
baseUrl,
|
|
140
|
+
routeBase,
|
|
141
|
+
});
|
|
142
|
+
const {config, isLoading, error} = useAdminConfig(api, resolvedApiBase);
|
|
126
143
|
|
|
127
144
|
const handlePress = useCallback(
|
|
128
145
|
(modelName: string) => {
|
|
129
|
-
router.push(`${
|
|
146
|
+
router.push(`${resolvedRouteBase}/${modelName}` as Href);
|
|
130
147
|
},
|
|
131
|
-
[
|
|
148
|
+
[resolvedRouteBase]
|
|
132
149
|
);
|
|
133
150
|
|
|
134
151
|
if (isLoading) {
|
|
@@ -18,12 +18,18 @@ const configState: {config: AdminConfigResponse | null; isLoading: boolean} = {
|
|
|
18
18
|
config: null,
|
|
19
19
|
isLoading: false,
|
|
20
20
|
};
|
|
21
|
+
// Records the apiBase passed to useAdminConfig so tests can assert that data
|
|
22
|
+
// fetching uses the resolved API base (not the route base).
|
|
23
|
+
const configApiBaseCalls: string[] = [];
|
|
21
24
|
mock.module("./useAdminConfig", () => ({
|
|
22
|
-
useAdminConfig: () =>
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
useAdminConfig: (_api: unknown, apiBase: string) => {
|
|
26
|
+
configApiBaseCalls.push(apiBase);
|
|
27
|
+
return {
|
|
28
|
+
config: configState.config,
|
|
29
|
+
error: null,
|
|
30
|
+
isLoading: configState.isLoading,
|
|
31
|
+
};
|
|
32
|
+
},
|
|
27
33
|
}));
|
|
28
34
|
|
|
29
35
|
const listState: {data: unknown; isLoading: boolean} = {
|
|
@@ -74,6 +80,7 @@ describe("AdminModelTable", () => {
|
|
|
74
80
|
routerPush.mockClear();
|
|
75
81
|
setOptions.mockClear();
|
|
76
82
|
deleteFn.mockClear();
|
|
83
|
+
configApiBaseCalls.length = 0;
|
|
77
84
|
configState.config = null;
|
|
78
85
|
configState.isLoading = false;
|
|
79
86
|
listState.data = {data: [], total: 0};
|
|
@@ -356,4 +363,50 @@ describe("AdminModelTable", () => {
|
|
|
356
363
|
});
|
|
357
364
|
expect(toJSON()).toBeDefined();
|
|
358
365
|
});
|
|
366
|
+
|
|
367
|
+
it("fetches config from apiBase but builds row href + create nav from routeBase when split", async () => {
|
|
368
|
+
configState.config = fullConfig;
|
|
369
|
+
listState.data = {data: [{_id: "u1", email: "a@b.com"}], total: 1};
|
|
370
|
+
let headerRight: React.ReactElement | null = null;
|
|
371
|
+
setOptions.mockImplementation((opts: Record<string, unknown>) => {
|
|
372
|
+
if (opts?.headerRight) {
|
|
373
|
+
headerRight = (opts.headerRight as () => React.ReactElement)();
|
|
374
|
+
}
|
|
375
|
+
});
|
|
376
|
+
const {UNSAFE_root} = renderWithTheme(
|
|
377
|
+
<AdminModelTable
|
|
378
|
+
api={{} as unknown as AdminApi}
|
|
379
|
+
apiBase="/admin"
|
|
380
|
+
modelName="User"
|
|
381
|
+
routeBase="/console"
|
|
382
|
+
/>
|
|
383
|
+
);
|
|
384
|
+
// Data fetching must use the API base.
|
|
385
|
+
expect(configApiBaseCalls).toContain("/admin");
|
|
386
|
+
// The first cell's link href must use the route base.
|
|
387
|
+
const tables = UNSAFE_root.findAll((n: ReactTestInstance) => Array.isArray(n.props?.data));
|
|
388
|
+
expect(tables.length).toBeGreaterThan(0);
|
|
389
|
+
const rows = (tables[0] as ReactTestInstance).props.data as {value: {href?: string}}[][];
|
|
390
|
+
expect(rows[0][0].value.href).toBe("/console/User/u1");
|
|
391
|
+
// The create button must navigate using the route base.
|
|
392
|
+
expect(headerRight).not.toBeNull();
|
|
393
|
+
const header = renderWithTheme(headerRight as unknown as React.ReactElement);
|
|
394
|
+
await act(async () => {
|
|
395
|
+
fireEvent.press(header.getByTestId("admin-create-button"));
|
|
396
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
397
|
+
});
|
|
398
|
+
expect(routerPush).toHaveBeenCalledWith("/console/User/create");
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
it("uses baseUrl for both fetching and navigation (backward compat)", () => {
|
|
402
|
+
configState.config = fullConfig;
|
|
403
|
+
listState.data = {data: [{_id: "u1", email: "a@b.com"}], total: 1};
|
|
404
|
+
const {UNSAFE_root} = renderWithTheme(
|
|
405
|
+
<AdminModelTable api={{} as unknown as AdminApi} baseUrl="/admin" modelName="User" />
|
|
406
|
+
);
|
|
407
|
+
expect(configApiBaseCalls).toContain("/admin");
|
|
408
|
+
const tables = UNSAFE_root.findAll((n: ReactTestInstance) => Array.isArray(n.props?.data));
|
|
409
|
+
const rows = (tables[0] as ReactTestInstance).props.data as {value: {href?: string}}[][];
|
|
410
|
+
expect(rows[0][0].value.href).toBe("/admin/User/u1");
|
|
411
|
+
});
|
|
359
412
|
});
|
package/src/AdminModelTable.tsx
CHANGED
|
@@ -17,12 +17,23 @@ import type {Href} from "expo-router";
|
|
|
17
17
|
import {router, useNavigation} from "expo-router";
|
|
18
18
|
import startCase from "lodash/startCase";
|
|
19
19
|
import React, {useCallback, useEffect, useMemo, useState} from "react";
|
|
20
|
-
import
|
|
20
|
+
import {
|
|
21
|
+
type AdminApi,
|
|
22
|
+
type AdminFieldConfig,
|
|
23
|
+
type AdminFieldValue,
|
|
24
|
+
type AdminModelConfig,
|
|
25
|
+
resolveAdminBases,
|
|
26
|
+
} from "./types";
|
|
21
27
|
import {useAdminApi} from "./useAdminApi";
|
|
22
28
|
import {useAdminConfig} from "./useAdminConfig";
|
|
23
29
|
|
|
24
30
|
interface AdminModelTableProps {
|
|
25
|
-
|
|
31
|
+
/** @deprecated Use `apiBase`/`routeBase`. Kept as a backward-compatible alias. */
|
|
32
|
+
baseUrl?: string;
|
|
33
|
+
/** Base path where admin API requests are sent. Falls back to `baseUrl`. */
|
|
34
|
+
apiBase?: string;
|
|
35
|
+
/** Base path used for in-app navigation. Falls back to `baseUrl`. */
|
|
36
|
+
routeBase?: string;
|
|
26
37
|
api: AdminApi;
|
|
27
38
|
modelName: string;
|
|
28
39
|
columns?: string[];
|
|
@@ -196,12 +207,19 @@ const EmptyContent: React.FC = () => (
|
|
|
196
207
|
*/
|
|
197
208
|
export const AdminModelTable: React.FC<AdminModelTableProps> = ({
|
|
198
209
|
baseUrl,
|
|
210
|
+
apiBase,
|
|
211
|
+
routeBase,
|
|
199
212
|
api,
|
|
200
213
|
modelName,
|
|
201
214
|
columns: columnsProp,
|
|
202
215
|
columnWidths,
|
|
203
216
|
}) => {
|
|
204
|
-
const {
|
|
217
|
+
const {apiBase: resolvedApiBase, routeBase: resolvedRouteBase} = resolveAdminBases({
|
|
218
|
+
apiBase,
|
|
219
|
+
baseUrl,
|
|
220
|
+
routeBase,
|
|
221
|
+
});
|
|
222
|
+
const {config, isLoading: isConfigLoading} = useAdminConfig(api, resolvedApiBase);
|
|
205
223
|
const [page, setPage] = useState(1);
|
|
206
224
|
const [sortColumn, setSortColumn] = useState<ColumnSortInterface | undefined>();
|
|
207
225
|
const navigation = useNavigation();
|
|
@@ -219,7 +237,7 @@ export const AdminModelTable: React.FC<AdminModelTableProps> = ({
|
|
|
219
237
|
headerRight: () => (
|
|
220
238
|
<Box alignItems="center" justifyContent="center" marginRight={3}>
|
|
221
239
|
<Button
|
|
222
|
-
onClick={() => router.push(`${
|
|
240
|
+
onClick={() => router.push(`${resolvedRouteBase}/${modelName}/create` as Href)}
|
|
223
241
|
testID="admin-create-button"
|
|
224
242
|
text="Create"
|
|
225
243
|
variant="primary"
|
|
@@ -228,7 +246,7 @@ export const AdminModelTable: React.FC<AdminModelTableProps> = ({
|
|
|
228
246
|
),
|
|
229
247
|
title: modelConfig.displayName,
|
|
230
248
|
});
|
|
231
|
-
}, [navigation, modelConfig,
|
|
249
|
+
}, [navigation, modelConfig, resolvedRouteBase, modelName]);
|
|
232
250
|
|
|
233
251
|
const displayFields = useMemo(
|
|
234
252
|
() => columnsProp ?? modelConfig?.listFields ?? [],
|
|
@@ -312,7 +330,7 @@ export const AdminModelTable: React.FC<AdminModelTableProps> = ({
|
|
|
312
330
|
if (isFirst) {
|
|
313
331
|
return {
|
|
314
332
|
value: {
|
|
315
|
-
href: `${
|
|
333
|
+
href: `${resolvedRouteBase}/${modelName}/${item._id}`,
|
|
316
334
|
text: formatted,
|
|
317
335
|
},
|
|
318
336
|
};
|
|
@@ -323,7 +341,9 @@ export const AdminModelTable: React.FC<AdminModelTableProps> = ({
|
|
|
323
341
|
};
|
|
324
342
|
});
|
|
325
343
|
|
|
326
|
-
const actionsCell = {
|
|
344
|
+
const actionsCell = {
|
|
345
|
+
value: {baseUrl: resolvedRouteBase, id: item._id, modelName, onDelete: handleDelete},
|
|
346
|
+
};
|
|
327
347
|
return [...fieldCells, actionsCell];
|
|
328
348
|
});
|
|
329
349
|
|
|
@@ -17,7 +17,12 @@ interface AdminNestedArrayFieldProps {
|
|
|
17
17
|
value: Record<string, AdminFieldValue>[];
|
|
18
18
|
onChange: (value: Record<string, AdminFieldValue>[]) => void;
|
|
19
19
|
api: AdminApi;
|
|
20
|
-
|
|
20
|
+
/** @deprecated Use `apiBase`/`routeBase`. Kept as a backward-compatible alias. */
|
|
21
|
+
baseUrl?: string;
|
|
22
|
+
/** Base path where admin API requests are sent. Falls back to `baseUrl`. */
|
|
23
|
+
apiBase?: string;
|
|
24
|
+
/** Base path used for in-app navigation. Falls back to `baseUrl`. */
|
|
25
|
+
routeBase?: string;
|
|
21
26
|
modelConfigs?: Array<{name: string; routePath: string}>;
|
|
22
27
|
/** Parent document form state, used to derive dynamic options for sub-fields */
|
|
23
28
|
parentFormState?: Record<string, AdminFieldValue>;
|
|
@@ -34,6 +39,8 @@ interface AdminNestedArrayFieldProps {
|
|
|
34
39
|
const renderNestedSubField = ({
|
|
35
40
|
api,
|
|
36
41
|
baseUrl,
|
|
42
|
+
apiBase,
|
|
43
|
+
routeBase,
|
|
37
44
|
fieldKey,
|
|
38
45
|
fieldConfig,
|
|
39
46
|
fieldValue,
|
|
@@ -44,7 +51,9 @@ const renderNestedSubField = ({
|
|
|
44
51
|
refRenderers,
|
|
45
52
|
}: {
|
|
46
53
|
api: AdminApi;
|
|
47
|
-
baseUrl
|
|
54
|
+
baseUrl?: string;
|
|
55
|
+
apiBase?: string;
|
|
56
|
+
routeBase?: string;
|
|
48
57
|
fieldKey: string;
|
|
49
58
|
fieldConfig: AdminFieldConfig;
|
|
50
59
|
fieldValue: AdminFieldValue;
|
|
@@ -58,6 +67,7 @@ const renderNestedSubField = ({
|
|
|
58
67
|
return (
|
|
59
68
|
<AdminNestedArrayField
|
|
60
69
|
api={api}
|
|
70
|
+
apiBase={apiBase}
|
|
61
71
|
baseUrl={baseUrl}
|
|
62
72
|
helperText={fieldConfig.description}
|
|
63
73
|
items={fieldConfig.items}
|
|
@@ -66,6 +76,7 @@ const renderNestedSubField = ({
|
|
|
66
76
|
onChange={(val) => onSubFieldChange(index, fieldKey, val)}
|
|
67
77
|
parentFormState={parentFormState}
|
|
68
78
|
refRenderers={refRenderers}
|
|
79
|
+
routeBase={routeBase}
|
|
69
80
|
title={startCase(fieldKey)}
|
|
70
81
|
value={Array.isArray(fieldValue) ? (fieldValue as Record<string, AdminFieldValue>[]) : []}
|
|
71
82
|
/>
|
|
@@ -75,6 +86,7 @@ const renderNestedSubField = ({
|
|
|
75
86
|
return (
|
|
76
87
|
<AdminFieldRendererCore
|
|
77
88
|
api={api}
|
|
89
|
+
apiBase={apiBase}
|
|
78
90
|
baseUrl={baseUrl}
|
|
79
91
|
fieldConfig={fieldConfig}
|
|
80
92
|
fieldKey={fieldKey}
|
|
@@ -83,6 +95,7 @@ const renderNestedSubField = ({
|
|
|
83
95
|
onChange={(val) => onSubFieldChange(index, fieldKey, val)}
|
|
84
96
|
parentFormState={parentFormState}
|
|
85
97
|
refRenderers={refRenderers}
|
|
98
|
+
routeBase={routeBase}
|
|
86
99
|
value={fieldValue}
|
|
87
100
|
/>
|
|
88
101
|
);
|
|
@@ -122,6 +135,8 @@ export const AdminNestedArrayField: React.FC<AdminNestedArrayFieldProps> = ({
|
|
|
122
135
|
onChange,
|
|
123
136
|
api,
|
|
124
137
|
baseUrl,
|
|
138
|
+
apiBase,
|
|
139
|
+
routeBase,
|
|
125
140
|
modelConfigs,
|
|
126
141
|
parentFormState,
|
|
127
142
|
refRenderers,
|
|
@@ -235,6 +250,7 @@ export const AdminNestedArrayField: React.FC<AdminNestedArrayFieldProps> = ({
|
|
|
235
250
|
{subFieldEntries.map(([fieldKey, fieldConfig]) =>
|
|
236
251
|
renderNestedSubField({
|
|
237
252
|
api,
|
|
253
|
+
apiBase,
|
|
238
254
|
baseUrl,
|
|
239
255
|
fieldConfig,
|
|
240
256
|
fieldKey,
|
|
@@ -244,6 +260,7 @@ export const AdminNestedArrayField: React.FC<AdminNestedArrayFieldProps> = ({
|
|
|
244
260
|
onSubFieldChange: handleSubFieldChange,
|
|
245
261
|
parentFormState: subFieldParentFormState,
|
|
246
262
|
refRenderers,
|
|
263
|
+
routeBase,
|
|
247
264
|
})
|
|
248
265
|
)}
|
|
249
266
|
</Box>
|
|
@@ -255,7 +272,9 @@ export const AdminNestedArrayField: React.FC<AdminNestedArrayFieldProps> = ({
|
|
|
255
272
|
idToIndexMap,
|
|
256
273
|
subFieldEntries,
|
|
257
274
|
api,
|
|
275
|
+
apiBase,
|
|
258
276
|
baseUrl,
|
|
277
|
+
routeBase,
|
|
259
278
|
modelConfigs,
|
|
260
279
|
parentFormState,
|
|
261
280
|
refRenderers,
|