11import { RJSFSchema } from "@rjsf/utils" ;
22import { Col , Container , Row } from "react-bootstrap" ;
33import Intro from "./components/Intro" ;
4- import formSchemas from "./assets/forms.json" ;
54import WizardForm from "./components/WizardForm" ;
65import validator from "@rjsf/validator-ajv8" ;
7- import { useState } from "react" ;
6+ import { useEffect , useState } from "react" ;
7+ import { useTranslation } from "react-i18next" ;
88
99export default function App ( ) {
10- const [ forms ] = useState < RJSFSchema [ ] > (
11- formSchemas as unknown as RJSFSchema [ ]
12- ) ;
10+ const { i18n } = useTranslation ( ) ;
11+ const [ forms , setForms ] = useState < { [ key : string ] : RJSFSchema [ ] } > ( {
12+ nl : [ ] ,
13+ en : [ ] ,
14+ } ) ;
1315
1416 // Not yet used but very likely to be used in the future.
1517 const [ , setFormData ] = useState ( { } ) ;
1618 const [ activeForm , setActiveForm ] = useState < RJSFSchema | null > ( null ) ;
1719 let formsMenu = [ { id : 0 , title : "No Form template found." } ] ;
1820
19- if ( forms . length > 0 ) {
20- formsMenu = ( forms as RJSFSchema [ ] ) . map ( ( item , index ) => {
21- return { id : index , title : item . title ?? "No form title" } ;
21+ if ( forms [ i18n . language ] && forms [ i18n . language ] . length > 0 ) {
22+ formsMenu = ( forms [ i18n . language ] as RJSFSchema [ ] ) . map ( ( item , index ) => {
23+ return { id : index , title : item . JSONSchema . title ?? "No form title" } ;
2224 } ) ;
2325 }
2426
@@ -27,17 +29,47 @@ export default function App() {
2729 setActiveForm ( null ) ;
2830 } ;
2931
30- const { uiSchema, ...activeFormProps } = activeForm ?? { } ;
32+ const onFormActivate = ( index : number ) => {
33+ setActiveForm ( forms [ i18n . language ] [ index ] ) ;
34+ } ;
35+
36+ useEffect ( ( ) => {
37+ // Use Vite's import.meta.glob to get a map of file paths in nested directories
38+ const jsonFiles = import . meta. glob ( "/src/schemas/*/*.json" ) ;
39+
40+ const loadJsonFiles = async ( ) => {
41+ const forms = { } as { [ key : string ] : RJSFSchema [ ] } ;
42+
43+ const dataEntries = await Promise . all (
44+ Object . entries ( jsonFiles ) . map ( async ( [ path , importFile ] ) => {
45+ const module = await importFile ( ) ;
46+ return [ path , module . default ] ; // Return file path and content
47+ } )
48+ ) ;
49+
50+ // Convert array of entries to an array per language
51+ for ( const [ path , data ] of dataEntries ) {
52+ const language : string = path . split ( "/" ) [ 3 ] ?? "nl" ;
53+ const dataObject = forms [ language ] ?? [ ] ;
54+ dataObject . push ( data ) ;
55+ forms [ language ] = dataObject ;
56+ }
57+
58+ setForms ( forms ) ;
59+ } ;
60+
61+ loadJsonFiles ( ) ;
62+ } , [ ] ) ;
3163
3264 return (
3365 < Container className = "vh-100" >
3466 < Row className = "justify-content-center align-items-center h-100" >
3567 < Col xs = { 12 } className = "" >
3668 { activeForm ? (
3769 < WizardForm
38- id = { activeFormProps . id }
39- schema = { activeFormProps }
40- uiSchema = { uiSchema }
70+ id = { activeForm . JSONSchema . title }
71+ schema = { activeForm . JSONSchema }
72+ uiSchema = { activeForm . uiSchema }
4173 formData = { { } }
4274 onSubmit = { onFormSubmit }
4375 onCancel = { onFormSubmit }
@@ -46,7 +78,7 @@ export default function App() {
4678 ) : (
4779 < Intro
4880 forms = { formsMenu }
49- onStart = { ( id : number ) => setActiveForm ( forms [ id ] ) }
81+ onStart = { ( id : number ) => onFormActivate ( id ) }
5082 />
5183 ) }
5284 </ Col >
0 commit comments