From 8004d8436cea2dafb457e64c6866ce978f367648 Mon Sep 17 00:00:00 2001 From: John Abrahams Date: Wed, 26 Jun 2019 17:37:49 -0400 Subject: [PATCH] First cut at saving between metadata forms --- app/components/workflow-metadata.js | 24 ++++++++++++++--- .../components/workflow-metadata-test.js | 26 +++++++++++++++++-- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/app/components/workflow-metadata.js b/app/components/workflow-metadata.js index da3e7efcf..a6531dde9 100644 --- a/app/components/workflow-metadata.js +++ b/app/components/workflow-metadata.js @@ -128,11 +128,13 @@ export default Component.extend({ swal({ type: 'error', title: 'Form Validation Error', - text: this.validationErrorMsg(schemaService.getErrors()) + html: this.validationErrorMsg(schemaService.getErrors()) }); return; } + this.saveMetadata(); + if (step >= this.get('schemas').length - 1) { this.finalizeMetadata(metadata); this.sendAction('next'); @@ -207,8 +209,24 @@ export default Component.extend({ agent_information: this.getBrowserInfo() }); - const finalMetadata = this.get('metadata'); - this.set('submission.metadata', JSON.stringify(finalMetadata)); + this.saveMetadata(); + }, + + saveMetadata() { + let shouldPersist = true; + const md = this.get('metadata'); + try { + const subMD = JSON.parse(this.get('submission.metadata')); + + shouldPersist = !_.isEqual(md, subMD); + } catch (e) { + shouldPersist = true; + } + // console.log(`Saving metadata. Should persist? ${shouldPersist}`); + this.set('submission.metadata', JSON.stringify(this.get('metadata'))); + if (shouldPersist) { + this.get('submission').save(); + } }, /** diff --git a/tests/integration/components/workflow-metadata-test.js b/tests/integration/components/workflow-metadata-test.js index 93f6d079b..7fb115cd5 100644 --- a/tests/integration/components/workflow-metadata-test.js +++ b/tests/integration/components/workflow-metadata-test.js @@ -2,7 +2,7 @@ import { module, test } from 'qunit'; import { setupRenderingTest } from 'ember-qunit'; import hbs from 'htmlbars-inline-precompile'; import { run } from '@ember/runloop'; -import { click, render } from '@ember/test-helpers'; +import { click, render, fillIn } from '@ember/test-helpers'; module('Integration | Component | workflow-metadata', (hooks) => { setupRenderingTest(hooks); @@ -17,7 +17,8 @@ module('Integration | Component | workflow-metadata', (hooks) => { }); const submission = Ember.Object.create({ repositories, - publication + publication, + save: () => Promise.resolve() }); this.set('submission', submission); @@ -363,4 +364,25 @@ module('Integration | Component | workflow-metadata', (hooks) => { assert.notOk(metadata.badMoo, 'metadata.badMoo property should not be found on the metadata object'); }); }); + + test('Should persist changes on Next Form', async function (assert) { + assert.expect(1); // The assertion in save() should run once + + const submission = Ember.Object.create({ + publication: this.get('publication'), + repositories: this.get('repositories'), + save: () => { + assert.ok(true); + return Promise.resolve(); + } + }); + this.set('submission', submission); + + run(async () => { + await render(hbs`{{workflow-metadata submission=submission publication=publication}}`); + + await fillIn('input[name="journal-NLMTA-ID"]', 'MOO ID'); + await click('button[data-key="Next"]'); + }); + }); });