MetadataFormRenderer.tsx
2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Metadata-driven form renderer.
//
// Given a form definition slug, fetches the JSON Schema + UI Schema
// from the metadata API and renders a fully functional @rjsf form
// using the VibeErp theme and custom ERP widgets.
import { useEffect, useState } from 'react'
import Form from '@rjsf/core'
import type { IChangeEvent } from '@rjsf/core'
import type { RJSFSchema, UiSchema } from '@rjsf/utils'
import validator from '@rjsf/validator-ajv8'
import { apiFetch } from '@/api/client'
import type { FormDefinition } from '@/types/api'
import { vibeWidgets } from '@/components/form-widgets'
import { vibeTemplates } from '@/components/form-widgets/vibeErpTheme'
import { Loading } from '@/components/Loading'
import { ErrorBox } from '@/components/ErrorBox'
interface MetadataFormRendererProps {
slug: string
initialValues?: Record<string, unknown>
onSubmit?: (values: Record<string, unknown>) => void
readOnly?: boolean
}
export function MetadataFormRenderer({
slug,
initialValues,
onSubmit,
readOnly = false,
}: MetadataFormRendererProps) {
const [def, setDef] = useState<FormDefinition | null>(null)
const [loading, setLoading] = useState(true)
const [error, setError] = useState<Error | null>(null)
const [formData, setFormData] = useState<Record<string, unknown>>(
initialValues ?? {},
)
useEffect(() => {
setLoading(true)
setError(null)
apiFetch<FormDefinition>(`/api/v1/_meta/metadata/forms/${slug}`)
.then((data) => {
setDef(data)
if (initialValues) setFormData(initialValues)
})
.catch((err: unknown) => {
if (err instanceof Error) {
setError(err)
} else {
setError(new Error(String(err)))
}
})
.finally(() => setLoading(false))
}, [slug, initialValues])
if (loading) return <Loading />
if (error) return <ErrorBox error={error} />
if (!def) return <ErrorBox error={new Error(`Form "${slug}" not found.`)} />
const handleChange = (e: IChangeEvent) => {
setFormData((e.formData as Record<string, unknown>) ?? {})
}
const handleSubmit = (e: IChangeEvent) => {
onSubmit?.((e.formData as Record<string, unknown>) ?? {})
}
return (
<Form
schema={def.jsonSchema as RJSFSchema}
uiSchema={def.uiSchema as UiSchema}
formData={formData}
validator={validator}
widgets={vibeWidgets}
templates={vibeTemplates}
readonly={readOnly}
onChange={handleChange}
onSubmit={handleSubmit}
formContext={{ formData }}
/>
)
}