Skip to content
Merged
Show file tree
Hide file tree
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
29 changes: 25 additions & 4 deletions src/form-builder/components/FormBuilderContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,38 @@
}

getFormData() {
return httpInterceptor
.get(`${formBuilderConstants.formUrl}?v=custom:(id,uuid,name,version,published,auditInfo)`)
.then((data) => {
this.setState({ data: this.orderFormByVersion(data.results), loading: false });
let initialForms = [];

Check warning on line 38 in src/form-builder/components/FormBuilderContainer.jsx

View workflow job for this annotation

GitHub Actions / Build & Package

'initialForms' is never reassigned. Use 'const' instead

Check warning on line 38 in src/form-builder/components/FormBuilderContainer.jsx

View workflow job for this annotation

GitHub Actions / Build & Package

'initialForms' is assigned a value but never used

Check warning on line 38 in src/form-builder/components/FormBuilderContainer.jsx

View workflow job for this annotation

GitHub Actions / Build & Package

'initialForms' is never reassigned. Use 'const' instead

Check warning on line 38 in src/form-builder/components/FormBuilderContainer.jsx

View workflow job for this annotation

GitHub Actions / Build & Package

'initialForms' is assigned a value but never used
let forms = [];

Check warning on line 39 in src/form-builder/components/FormBuilderContainer.jsx

View workflow job for this annotation

GitHub Actions / Build & Package

'forms' is never reassigned. Use 'const' instead

Check warning on line 39 in src/form-builder/components/FormBuilderContainer.jsx

View workflow job for this annotation

GitHub Actions / Build & Package

'forms' is never reassigned. Use 'const' instead
const queryParams = '?=';

Check warning on line 40 in src/form-builder/components/FormBuilderContainer.jsx

View workflow job for this annotation

GitHub Actions / Build & Package

'queryParams' is assigned a value but never used

Check warning on line 40 in src/form-builder/components/FormBuilderContainer.jsx

View workflow job for this annotation

GitHub Actions / Build & Package

'queryParams' is assigned a value but never used
const fetchFormsUrl = `${formBuilderConstants.formUrl}?v=custom:(id,uuid,name,version,published,auditInfo)`;

Check warning on line 41 in src/form-builder/components/FormBuilderContainer.jsx

View workflow job for this annotation

GitHub Actions / Build & Package

Line 41 exceeds the maximum line length of 100

Check warning on line 41 in src/form-builder/components/FormBuilderContainer.jsx

View workflow job for this annotation

GitHub Actions / Build & Package

Line 41 exceeds the maximum line length of 100
return httpInterceptor.get(fetchFormsUrl)
.then((initialForms) => {

Check warning on line 43 in src/form-builder/components/FormBuilderContainer.jsx

View workflow job for this annotation

GitHub Actions / Build & Package

'initialForms' is already declared in the upper scope

Check warning on line 43 in src/form-builder/components/FormBuilderContainer.jsx

View workflow job for this annotation

GitHub Actions / Build & Package

'initialForms' is already declared in the upper scope
this.collectAllForms(initialForms, forms);
})
.catch((error) => {
this.showErrors(error);
this.setState({ loading: false });
});
}

collectAllForms(initialForms, forms) {

Check warning on line 52 in src/form-builder/components/FormBuilderContainer.jsx

View workflow job for this annotation

GitHub Actions / Build & Package

collectAllForms should be placed after setMessage

Check warning on line 52 in src/form-builder/components/FormBuilderContainer.jsx

View workflow job for this annotation

GitHub Actions / Build & Package

collectAllForms should be placed after setMessage
forms.push(...initialForms.results);
if (forms.length === formBuilderConstants.dataLimit) {
this.setState({ data: this.orderFormByVersion(forms), loading: false });
return;
}
if (initialForms.links !== undefined && initialForms.links.length > 0 && initialForms.links.find(link => link.rel === 'next') !== undefined) {

Check warning on line 58 in src/form-builder/components/FormBuilderContainer.jsx

View workflow job for this annotation

GitHub Actions / Build & Package

Line 58 exceeds the maximum line length of 100

Check warning on line 58 in src/form-builder/components/FormBuilderContainer.jsx

View workflow job for this annotation

GitHub Actions / Build & Package

Line 58 exceeds the maximum line length of 100
httpInterceptor.get(initialForms.links[0].uri)
.then((privileges) => this.collectAllForms(privileges, forms))
.catch((error) => {
this.showErrors(error);
this.setState({ loading: false });
});
} else {
this.setState({ data: this.orderFormByVersion(forms), loading: false });
}
}

getDefaultLocale() {
httpInterceptor
.get(`${formBuilderConstants.defaultLocaleUrl}`, 'text')
Expand Down
23 changes: 20 additions & 3 deletions src/form-builder/components/FormPrivilegeTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,32 @@ export default class FormPrivilegeTable extends Component {
this.setState({ loading: false });
});
}

fetchPrivileges() {
let initialPrivileges = [];
let privileges = [];
const queryParams = '?=';
const optionsUrl = `${formBuilderConstants.formPrivilegeUrl}${queryParams}`;
httpInterceptor.get(optionsUrl)
.then((initialPrivileges) => {
this.setState({ availablePrivileges: this.arrangePrivileges(initialPrivileges.results), loading: false });
});
.then((initialPrivileges) => {
this.collectAllPrivileges(initialPrivileges, privileges);
});
}

collectAllPrivileges(initialPrivileges, allPrivileges) {
allPrivileges.push(...initialPrivileges.results);
if (allPrivileges.length === formBuilderConstants.dataLimit) {
this.setState({ availablePrivileges: this.arrangePrivileges(allPrivileges), loading: false });
return;
}
if (initialPrivileges.links !== undefined && initialPrivileges.links.length > 0 && initialPrivileges.links.find(link => link.rel === 'next') !== undefined) {
httpInterceptor.get(initialPrivileges.links[0].uri)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if the API has a bug? If the API incorrectly keeps returning a "next" link indefinitely, this function never stops calling itself. Could introducing a limit be a safer option?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can introduce a limit but its like we are not trusting our systems.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's always a good practice to introduce a limit. It's not that we are not trusting our systems but it's defensive programming to avoid rare but critical issues. Logic wise, it's all good.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'll go ahead and add a limit

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arshiyaTW2021 I have added the limit.

.then((privileges) => this.collectAllPrivileges(privileges, allPrivileges));
} else {
this.setState({ availablePrivileges: this.arrangePrivileges(allPrivileges), loading: false });
}
}

fetchFormPrivilegesFromDB() {
let initialPrivilegesFromDB = [];
const queryParams = '?=';
Expand Down
2 changes: 1 addition & 1 deletion src/form-builder/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ export const formBuilderConstants = {
getFormPrivilegesFromUuidUrl: '/openmrs/ws/rest/v1/bahmniie/form/getFormPrivilegesFromUuid',
jsonToPdfConvertionUrl: '/openmrs/ws/rest/v1/bahmniie/form/jsonToPdf',
pdfDownloadUrl: '/openmrs/ws/rest/v1/bahmniie/form/download/',

dataLimit: 9999,
};
Loading