Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert data from GraphQL to fir with jssdk model #137

Merged
merged 1 commit into from
Aug 26, 2024
Merged
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
31 changes: 23 additions & 8 deletions src/@episerver/forms-sdk/src/form-loader/formLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,12 @@ export class FormLoader<T extends FormContainer> {
}),
})
.then(async (response: Response) => {
if(response.ok){
if (response.ok) {
let json = await response.json();
let formStr = json.data.FormContainerBlock.items[0]?.FormRenderTemplate;
if(formStr){
resolve(JSON.parse(formStr) as T);
}
else {
reject(response);
}
let formStr = json.data.FormContainer.items[0];
console.log(formStr)
let convertedFormStr = this.convertFirstLetterToLowerCase(formStr) as T
resolve(convertedFormStr)
}
else {
reject(response);
Expand All @@ -103,4 +100,22 @@ export class FormLoader<T extends FormContainer> {
});
});
}

/**
* Function to convert the first letter of object keys to lowercase
* @param data Data in json format
* **/
private convertFirstLetterToLowerCase(data: any): any {
const isObject = typeof data === 'object'
if (data && isObject && !Array.isArray(data)) {
return Object.keys(data).reduce((accumulator, key) => {
const normalizedKey = key.charAt(0).toLowerCase() + key.slice(1);
accumulator[normalizedKey] = isObject ? this.convertFirstLetterToLowerCase(data[key]) : data[key];
return accumulator;
}, {} as any);
} else if (Array.isArray(data)) {
return data.map(item => this.convertFirstLetterToLowerCase(item));
}
return data;
}
}
Loading