import { useEffect, useState } from 'react'; // @ts-ignore import { Button, ButtonSet, Form, Stack, TextInput } from '@carbon/react'; import { useParams } from 'react-router-dom'; import { FormField } from '../interfaces'; import { modifyProcessModelPath, slugifyString } from '../helpers'; import HttpService from '../services/HttpService'; export default function JsonSchemaFormBuilder() { const params = useParams(); const [formTitle, setFormTitle] = useState(''); const [formDescription, setFormDescription] = useState(''); const [formId, setFormId] = useState(''); const [formFields, setFormFields] = useState([]); const [showNewFormField, setShowNewFormField] = useState(false); const [formIdHasBeenUpdatedByUser, setFormIdHasBeenUpdatedByUser] = useState(false); const [formFieldIdHasBeenUpdatedByUser, setFormFieldIdHasBeenUpdatedByUser] = useState(false); const [formFieldId, setFormFieldId] = useState(''); const [formFieldTitle, setFormFieldTitle] = useState(''); const modifiedProcessModelId = modifyProcessModelPath( `${params.process_model_id}` ); useEffect(() => {}, []); const renderFormJson = () => { const formJson = { title: formTitle, description: formDescription, properties: {}, required: [], }; formFields.forEach((formField: FormField) => { (formJson.properties as any)[formField.id] = { type: 'string', title: formField.title, }; }); return JSON.stringify(formJson, null, 2); }; const renderFormUiJson = () => { const uiOrder = formFields.map((formField: FormField) => { return formField.id; }); return JSON.stringify({ 'ui:order': uiOrder }, null, 2); }; const onFormFieldTitleChange = (newFormFieldTitle: string) => { console.log('newFormFieldTitle', newFormFieldTitle); console.log( 'setFormFieldIdHasBeenUpdatedByUser', formFieldIdHasBeenUpdatedByUser ); if (!formFieldIdHasBeenUpdatedByUser) { setFormFieldId(slugifyString(newFormFieldTitle)); } setFormFieldTitle(newFormFieldTitle); }; const onFormTitleChange = (newFormTitle: string) => { if (!formIdHasBeenUpdatedByUser) { setFormId(slugifyString(newFormTitle)); } setFormTitle(newFormTitle); }; const createNewFormField = () => { const newFormField: FormField = { id: formFieldId, title: formFieldTitle, required: false, }; setFormFieldIdHasBeenUpdatedByUser(false); setShowNewFormField(false); setFormFields([...formFields, newFormField]); }; const newFormFieldComponent = () => { if (showNewFormField) { return ( <> { onFormFieldTitleChange(event.srcElement.value); }} /> { setFormFieldIdHasBeenUpdatedByUser(true); setFormFieldId(event.srcElement.value); }} /> ); } return null; }; const formFieldArea = () => { if (formFields.length > 0) { return formFields.map((formField: FormField) => { return

Form Field: {formField.id}

; }); } return null; }; const handleSaveCallback = (result: any) => { console.log('result', result); }; const uploadFile = (file: File) => { const url = `/process-models/${modifiedProcessModelId}/files`; const httpMethod = 'POST'; const formData = new FormData(); formData.append('file', file); formData.append('fileName', file.name); HttpService.makeCallToBackend({ path: url, successCallback: handleSaveCallback, httpMethod, postBody: formData, }); }; const saveFile = () => { const formJsonFileName = `${formId}-schema.json`; const formUiJsonFileName = `${formId}-uischema.json`; uploadFile(new File([renderFormJson()], formJsonFileName)); uploadFile(new File([renderFormUiJson()], formUiJsonFileName)); }; const jsonFormArea = () => { return ( <> { onFormTitleChange(event.srcElement.value); }} /> { setFormIdHasBeenUpdatedByUser(true); setFormId(event.srcElement.value); }} /> { setFormDescription(event.srcElement.value); }} /> {formFieldArea()} {newFormFieldComponent()} ); }; return <>{jsonFormArea()}; }