@airoom/nextmin-react 0.1.6 → 0.1.8
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/components/RefMultiSelect.d.ts +10 -2
- package/dist/components/RefMultiSelect.js +78 -45
- package/dist/components/RefSingleSelect.js +11 -11
- package/dist/components/SchemaForm.d.ts +7 -1
- package/dist/components/SchemaForm.js +9 -9
- package/dist/lib/api.js +22 -15
- package/dist/nextmin.css +1 -1
- package/dist/views/CreateEditPage.js +15 -1
- package/package.json +1 -1
|
@@ -7,6 +7,17 @@ import { api } from '../lib/api';
|
|
|
7
7
|
import { SchemaForm } from '../components/SchemaForm';
|
|
8
8
|
import Link from 'next/link';
|
|
9
9
|
import { Button, Divider } from '@heroui/react';
|
|
10
|
+
function extractFieldErrors(err) {
|
|
11
|
+
// Try common shapes: axios error -> err.response.data.fields
|
|
12
|
+
const data = err?.response?.data ?? err?.data ?? err;
|
|
13
|
+
const fields = Array.isArray(data?.fields) ? data.fields : [];
|
|
14
|
+
return fields
|
|
15
|
+
.filter((f) => f && typeof f === 'object')
|
|
16
|
+
.map((f) => ({
|
|
17
|
+
field: String(f.field ?? ''),
|
|
18
|
+
message: String(f.message ?? 'Invalid value'),
|
|
19
|
+
}));
|
|
20
|
+
}
|
|
10
21
|
export function CreateEditPage({ model, id }) {
|
|
11
22
|
const router = useRouter();
|
|
12
23
|
const { items } = useSelector((s) => s.schemas);
|
|
@@ -14,6 +25,7 @@ export function CreateEditPage({ model, id }) {
|
|
|
14
25
|
const [initialValues, setInitialValues] = useState({});
|
|
15
26
|
const [busy, setBusy] = useState(false);
|
|
16
27
|
const [error, setError] = useState();
|
|
28
|
+
const [fieldErrors, setFieldErrors] = useState([]);
|
|
17
29
|
useEffect(() => {
|
|
18
30
|
let cancelled = false;
|
|
19
31
|
if (!id) {
|
|
@@ -41,6 +53,7 @@ export function CreateEditPage({ model, id }) {
|
|
|
41
53
|
const handleSubmit = async (values) => {
|
|
42
54
|
setBusy(true);
|
|
43
55
|
setError(undefined);
|
|
56
|
+
setFieldErrors([]);
|
|
44
57
|
try {
|
|
45
58
|
if (id) {
|
|
46
59
|
await api.update(model, id, values);
|
|
@@ -52,6 +65,7 @@ export function CreateEditPage({ model, id }) {
|
|
|
52
65
|
router.refresh();
|
|
53
66
|
}
|
|
54
67
|
catch (e) {
|
|
68
|
+
setFieldErrors(e.fields ?? []);
|
|
55
69
|
setError(e?.message || 'Save failed');
|
|
56
70
|
}
|
|
57
71
|
finally {
|
|
@@ -60,5 +74,5 @@ export function CreateEditPage({ model, id }) {
|
|
|
60
74
|
};
|
|
61
75
|
if (!schema)
|
|
62
76
|
return _jsx("div", { className: "text-danger text-sm", children: "Schema not found" });
|
|
63
|
-
return (_jsxs("div", { className: "grid gap-4 px-4", children: [_jsxs("div", { className: "flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between", children: [_jsx("h2", { className: "m-0 text-xl font-semibold", children: id ? `Edit ${schema.modelName}` : `Create ${schema.modelName}` }), _jsx("div", { className: "flex items-center gap-3", children: _jsx(Button, { as: Link, href: `/admin/${model}`, size: "sm", variant: "flat", color: "danger", isDisabled: busy, children: "Cancel" }) })] }), _jsx(Divider, { className: "my-3" }), error && _jsx("div", { className: "text-danger text-sm", children: error }), _jsx(SchemaForm, { model: model, schemaOverride: schema, initialValues: initialValues, submitLabel: id ? 'Save' : 'Create', busy: busy, onSubmit: handleSubmit })] }));
|
|
77
|
+
return (_jsxs("div", { className: "grid gap-4 px-4", children: [_jsxs("div", { className: "flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between", children: [_jsx("h2", { className: "m-0 text-xl font-semibold", children: id ? `Edit ${schema.modelName}` : `Create ${schema.modelName}` }), _jsx("div", { className: "flex items-center gap-3", children: _jsx(Button, { as: Link, href: `/admin/${model}`, size: "sm", variant: "flat", color: "danger", isDisabled: busy, children: "Cancel" }) })] }), _jsx(Divider, { className: "my-3" }), error && _jsx("div", { className: "text-danger text-sm", children: error }), fieldErrors.length > 0 && (_jsx("div", { className: "rounded-md border border-danger-200 bg-danger-50 px-3 py-2 text-warning-800 text-sm", children: _jsx("ul", { className: "list-disc pl-5", children: fieldErrors.map((fe, idx) => (_jsxs("li", { children: [_jsx("span", { className: "font-medium", children: fe.field }), ": ", fe.message] }, `${fe.field}-${idx}`))) }) })), _jsx(SchemaForm, { model: model, schemaOverride: schema, initialValues: initialValues, submitLabel: id ? 'Save' : 'Create', busy: busy, onSubmit: handleSubmit, fieldErrors: fieldErrors })] }));
|
|
64
78
|
}
|