Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ import {FunctionRuntime} from '@aemforms/af-core';
//@ts-ignore
import * as customfunctions from '@aemforms/af-custom-functions';

const getData = async (formId: string) => {
const { search = '' } = window.location;
const dataAPI = process.env.DATA_API;
if (dataAPI) {
try {
const endpoint = `${dataAPI}/${formId}${search}`
const response = await fetch(endpoint);
const json = await response.json();
const { data } = json;
const { data: { afData: { afBoundData = {} } = {} } = {} } = json;
return Object.keys(afBoundData).length > 0 ? afBoundData : (data || json);
} catch (e) {
return null;
}
}
}

const getForm = async () => {
if (process.env.USE_LOCAL_JSON == 'true') {
Expand All @@ -33,7 +49,12 @@ const getForm = async () => {
formAPI = `${formPath}/${SUFFIX}`;
}
const resp = await fetch(formAPI);
return (await resp.json());
const formDef = await resp.json();
const data = await getData(formDef.id);
if (data != null) {
formDef.data = data;
}
return formDef;
}
}

Expand Down Expand Up @@ -83,4 +104,4 @@ const Form = (props: any) => {
return null
}

export default Form
export default Form