Skip to content

Commit

Permalink
FIO-8433 fixed restore draft for nested forms
Browse files Browse the repository at this point in the history
  • Loading branch information
HannaKurban committed Jun 7, 2024
1 parent 2f14fbf commit 43f7313
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/Webform.js
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,8 @@ export default class Webform extends NestedDataComponent {
formio.loadSubmissions({
params: {
state: 'draft',
owner: userId
owner: userId,
sort: '-created'
}
}).then(submissions => {
if (submissions.length > 0 && !this.options.skipDraftRestore) {
Expand Down
5 changes: 3 additions & 2 deletions src/components/form/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -706,10 +706,11 @@ export default class FormComponent extends Component {
&& submission._id
&& this.subForm.formio
&& _.isEmpty(submission.data);
const shouldLoadDraftById = this.options.saveDraft && _.isEmpty(submission.data) && _.get(this.subForm, 'submission._id');

if (shouldLoadSubmissionById) {
if (shouldLoadSubmissionById || shouldLoadDraftById) {
const formId = submission.form || this.formObj.form || this.component.form;
const submissionUrl = `${this.subForm.formio.formsUrl}/${formId}/submission/${submission._id}`;
const submissionUrl = `${this.subForm.formio.formsUrl}/${formId}/submission/${submission._id || this.subForm.submission._id}`;
const options = this.root.formio?.base && this.root.formio?.projectUrl
? {
base: this.root.formio.base,
Expand Down
39 changes: 37 additions & 2 deletions src/components/form/Form.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,11 @@ describe('SaveDraft functionality for Nested Form', () => {
let state = null;
let subFormState = null;

const restoredDraftNestedData = { nested: 'test Nested' };

const restoredDraftData = {
parent: 'test Parent',
form: { nested: 'test Nested' },
form: { data: restoredDraftNestedData },
submit: false,
};

Expand All @@ -307,6 +309,19 @@ describe('SaveDraft functionality for Nested Form', () => {
return Promise.resolve(fastCloneDeep(data));
}

if (type === 'submission' && method === 'get' && _.endsWith(url, '660e75e4e8c776f1225142aa')) {
return Promise.resolve(
fastCloneDeep({
_id: '660e75e4e8c776f1225142aa',
form: '63e4deda12b88c4f05c125cf',
owner: '63ceaccebe0090345b109da7',
data: restoredDraftNestedData,
project: '65b0ccbaf019a907ac01a869',
state: 'draft',
}),
);
}

if (type === 'form' && method === 'get') {
return Promise.resolve(fastCloneDeep(_.endsWith(url, 'parent') ? comp7 : comp8));
}
Expand All @@ -332,7 +347,7 @@ describe('SaveDraft functionality for Nested Form', () => {
_id: '660e75e4e8c776f1225142aa',
form: '63e4deda12b88c4f05c125cf',
owner: '63ceaccebe0090345b109da7',
data: restoredDraftData.form,
data: restoredDraftNestedData,
project: '65b0ccbaf019a907ac01a869',
state: 'draft',
}),
Expand Down Expand Up @@ -379,6 +394,26 @@ describe('SaveDraft functionality for Nested Form', () => {
}).catch((err) => done(err));
});

it('Should restore draft for nested Form', function(done) {
const formElement = document.createElement('div');
Formio.createForm(
formElement,
'http://localhost:3000/idwqwhclwioyqbw/testdraftparent',
{
saveDraft: true
}
).then((form) => {
setTimeout(() => {
assert.equal(restoreDraftCalls, 2);
assert.equal(saveDraftCalls, 0);
assert.equal(form.submission.state, 'draft');
assert.deepEqual(form.data, restoredDraftData);
assert.deepEqual(_.get(form.submission.data, 'form.data'), restoredDraftNestedData);
done();
}, 200);
}).catch((err) => done(err));
});

it('Should not restore draft for Nested Form if skipDraftRestore is set as true', function(done) {
const formElement = document.createElement('div');
Formio.createForm(
Expand Down

0 comments on commit 43f7313

Please sign in to comment.