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

FIO-8330 fixed saving draft if saveDraft and skipDraftRestore are true #5593

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
17 changes: 12 additions & 5 deletions src/Webform.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _ from 'lodash';
import moment from 'moment';
import { compareVersions } from 'compare-versions';
import { Component } from '@formio/core';

Check warning on line 4 in src/Webform.js

View workflow job for this annotation

GitHub Actions / setup

'Component' is defined but never used
import EventEmitter from './EventEmitter';
import i18nDefaults from './i18n';
import { Formio } from './Formio';
Expand Down Expand Up @@ -303,12 +303,19 @@
this.language = this.i18next.language;

// See if we need to restore the draft from a user.
if (this.options.saveDraft && !this.options.skipDraftRestore) {
if (this.options.saveDraft) {
this.formReady.then(()=> {
const user = Formio.getUser();
// Only restore a draft if the submission isn't explicitly set.
if (user && !this.submissionSet) {
this.restoreDraft(user._id);
if (!this.options.skipDraftRestore) {
const user = Formio.getUser();
// Only restore a draft if the submission isn't explicitly set.
if (user && !this.submissionSet) {
this.restoreDraft(user._id);
}
}
else {
// Enable drafts
this.draftEnabled = true;
this.savingDraft = false;
}
});
}
Expand Down
35 changes: 35 additions & 0 deletions src/Webform.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4601,6 +4601,7 @@ describe('Webform tests', function() {
const originalMakeRequest = Formio.makeRequest;
let saveDraftCalls = 0;
let restoreDraftCalls = 0;
let state = null;
const scenario = {
restoreDraftError: false,
saveDraftError: false,
Expand All @@ -4624,6 +4625,11 @@ describe('Webform tests', function() {
? Promise.reject('Save Draft Error')
: Promise.resolve(fastCloneDeep(data));
}
if (type === 'submission' && method === 'post') {
state = data.state;
saveDraftCalls = ++saveDraftCalls;
return Promise.resolve(fastCloneDeep(data));
}
if (type === 'form' && method === 'get') {
return Promise.resolve(fastCloneDeep({
_id: '65cdd69efb1b9683c216fa1d',
Expand Down Expand Up @@ -4707,6 +4713,7 @@ describe('Webform tests', function() {
afterEach(() => {
saveDraftCalls = 0;
restoreDraftCalls = 0;
state = null;
scenario.restoreDraftError = false;
scenario.saveDraftError = false;
});
Expand Down Expand Up @@ -4810,6 +4817,34 @@ describe('Webform tests', function() {
}, 200);
}).catch((err) => done(err));
});

it('Should save the draft after changing the data if skipDraftRestore is set as true', function(done) {
const formElement = document.createElement('div');
Formio.createForm(
formElement,
'http://localhost:3000/zarbzxibjafpcjb/testdrafterrors',
{
saveDraft: true,
skipDraftRestore: true
}
).then((form) => {
setTimeout(() => {
assert.equal(restoreDraftCalls, 0, 'Should not restore Draft');
assert.equal(saveDraftCalls, 0);
assert.equal(_.isUndefined(form.submission.state), true);
const tfInput = form.getComponent('textField').refs.input[0];
tfInput.value = 'test';
const inputEvent = new Event('input');
tfInput.dispatchEvent(inputEvent);
setTimeout(() => {
assert.equal(restoreDraftCalls, 0);
assert.equal(saveDraftCalls, 1, 'Should save Draft');
assert.equal(state, 'draft');
done();
}, 300);
},200);
}).catch((err) => done(err));
});
});

for (const formTest of FormTests) {
Expand Down
Loading