Skip to content

Commit

Permalink
Merge pull request #137 from episerver/feature/AFORM-4357-change-grap…
Browse files Browse the repository at this point in the history
…hQL-query

Convert data from GraphQL to fir with jssdk model
  • Loading branch information
epi-qang2 authored Aug 26, 2024
2 parents 65b5964 + 555cdf4 commit a3703ba
Showing 1 changed file with 23 additions and 8 deletions.
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;
}
}

0 comments on commit a3703ba

Please sign in to comment.