Skip to content

Commit

Permalink
FIO-7395: reimplement logic
Browse files Browse the repository at this point in the history
  • Loading branch information
roma-formio committed Mar 15, 2024
1 parent c8ef5ac commit 19b6b4b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
31 changes: 16 additions & 15 deletions src/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,27 +165,28 @@ export default class Form extends Element {
* Check Subdirectories path and provide correct options
*
* @param {string} url - The the URL of the form json.
* @return {*}
* @param {form} object - The form json.
* @return {object} The initial options with base and project.
*/
getFormInitOptions(url) {
getFormInitOptions(url, form) {
const options = {};
const urlParts = Formio.getUrlParts(url);
if (!urlParts) {
const index = url.indexOf(form?.path);
// form url doesn't include form path
if (index === -1) {
return options;
}
const baseUrl = `${urlParts[1]}${urlParts[2]}`;
// Subdirectories path must be '/projectId/formId'
const path = urlParts[3]?.split('?')[0]?.split('/');
if (path?.length !== 3) {
const projectUrl = url.substring(0, index - 1);
const urlParts = Formio.getUrlParts(projectUrl);
// project url doesn't include subdirectories path
if (!urlParts || urlParts.filter(part => !!part).length < 4) {
return options;
}
path.shift();
const [projectId, formId] = path;
// Detect Subdirectories path type when baseUrl wasn't set for this url
if (baseUrl !== Formio.baseUrl && projectId && formId) {
const baseUrl = `${urlParts[1]}${urlParts[2]}`;
// Skip if baseUrl has already been set
if (baseUrl !== Formio.baseUrl) {
return {
base: baseUrl,
project: `${baseUrl}/${projectId}`,
project: projectUrl,
};
}

Expand All @@ -196,8 +197,7 @@ export default class Form extends Element {
let result;
formParam = formParam || this.form;
if (typeof formParam === 'string') {
const options = this.getFormInitOptions(formParam);
const formio = new Formio(formParam, options);
const formio = new Formio(formParam);
let error;
this.loading = true;
result = this.getSubmission(formio, this.options)
Expand All @@ -217,6 +217,7 @@ export default class Form extends Element {
}
this.loading = false;
this.instance = this.instance || this.create(form.display);
const options = this.getFormInitOptions(formParam, form);
this.instance.setUrl(formParam, options);
this.instance.nosubmit = false;
this._form = this.instance.form = form;
Expand Down
8 changes: 5 additions & 3 deletions src/Formio.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2228,14 +2228,14 @@ describe('Formio.js Tests', () => {
name: 'Should return correct options for form url with Subdirectories path',
test() {
let form = new Formio.Form();
let options = form.getFormInitOptions('http://localhost:3000/fakeproject/fakeform');
let options = form.getFormInitOptions('http://localhost:3000/fakeproject/fakeform', { path: 'fakeform' });
assert.deepEqual(options, {
base: 'http://localhost:3000',
project: 'http://localhost:3000/fakeproject',
});

form = new Formio.Form();
options = form.getFormInitOptions(`${Formio.baseUrl}/fakeproject/fakeform`);
options = form.getFormInitOptions(`${Formio.baseUrl}/fakeproject/fakeform`, { path: 'fakeform' });
assert.deepEqual(options, {});
}
},
Expand All @@ -2258,7 +2258,9 @@ describe('Formio.js Tests', () => {
headers: {
'Content-Type': 'application/json',
},
body: {}
body: {
path: 'fakeform',
}
};
}
};
Expand Down
8 changes: 7 additions & 1 deletion src/components/form/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,13 @@ export default class FormComponent extends Component {
if (shouldLoadSubmissionById) {
const formId = submission.form || this.formObj.form || this.component.form;
const submissionUrl = `${this.subForm.formio.formsUrl}/${formId}/submission/${submission._id}`;
this.subForm.setUrl(submissionUrl, this.options);
const options = this.root.formio?.base && this.root.formio?.projectUrl
? {
base: this.root.formio.base,
project: this.root.formio.projectUrl,
}
: {};
this.subForm.setUrl(submissionUrl, { ...this.options, ...options });
this.subForm.loadSubmission().catch((err) => {
console.error(`Unable to load subform submission ${submission._id}:`, err);
});
Expand Down

0 comments on commit 19b6b4b

Please sign in to comment.