From e64d964202e6b9930dda0712837682a71ad0d1db Mon Sep 17 00:00:00 2001 From: jadrake75 Date: Wed, 8 Jun 2022 16:25:16 -0400 Subject: [PATCH] Provide the ability to create Save and New on edit When editing a stamp if you click Save and New it will create a new instance of a stamp based on the stamp you are saving (or editing). The values of the stamp will be set from the preferences (if defined) and initialized. Fixes #172 --- README.md | 3 +- .../elements/stamps/stamp-editor.html | 2 +- src/resources/elements/stamps/stamp-editor.js | 64 ++++++------------- src/util/common-models.js | 12 ++++ src/views/stamps/stamp-list.js | 14 +--- test/unit/util/common-models.spec.js | 24 +++++++ 6 files changed, 60 insertions(+), 59 deletions(-) diff --git a/README.md b/README.md index f979154..71a2abb 100644 --- a/README.md +++ b/README.md @@ -90,7 +90,7 @@ Webdriver for NodeJS is used for the integration tests. This project has been mo ## Test Statistics -The following is a list of test statistics for the project for date +The following is a list of test statistics for the project by date and commit | Date | Commit | Number of Tests | Code Coverage | | --- |-----------------------------------------------------------------------------------------------------------|-----------------|---------------| @@ -98,6 +98,7 @@ The following is a list of test statistics for the project for date | 2022-01-15 | [081fe3f](https://github.com/stamp-web/stamp-web-aurelia/commit/081fe3f31d5962c10777f4017e2c7a5dbe26e12e) | 86 | 12.20% | | 2022-01-15 | [38899a3](https://github.com/stamp-web/stamp-web-aurelia/commit/38899a32d69cd5c62ade7341a83708d4a8e1e726) | 94 | 13.29% | | 2022-01-25 | [c17f067](https://github.com/stamp-web/stamp-web-aurelia/commit/c17f06784332adff83e0a2594a705de26285d30a) | 98 | 18.35% +| 2022-06-08 | [a798ac3](https://github.com/stamp-web/stamp-web-aurelia/commit/a798ac36ac61a06258729173d8fa5cacf6a0ff24) | 102 | 18.41% ## Optimizing for Browsers diff --git a/src/resources/elements/stamps/stamp-editor.html b/src/resources/elements/stamps/stamp-editor.html index 83f2056..4d2df6d 100644 --- a/src/resources/elements/stamps/stamp-editor.html +++ b/src/resources/elements/stamps/stamp-editor.html @@ -16,7 +16,7 @@
- +
diff --git a/src/resources/elements/stamps/stamp-editor.js b/src/resources/elements/stamps/stamp-editor.js index 8d9b9d4..4b77fa2 100644 --- a/src/resources/elements/stamps/stamp-editor.js +++ b/src/resources/elements/stamps/stamp-editor.js @@ -330,22 +330,21 @@ export class StampEditorComponent extends EventManaged { } save(keepOpen) { - let self = this; - if (self.preprocess()) { + if (this.preprocess()) { // patch for https://github.com/aurelia/validatejs/issues/68 - _.each(self.duplicateModel.catalogueNumbers, cn => { + _.each(this.duplicateModel.catalogueNumbers, cn => { if (cn.__validationReporter__) { delete cn.__validationReporter__; } }); - self.stampService.save(self.duplicateModel).then(stamp => { - if( self.duplicateModel.id <= 0 ) { - self.eventBus.publish(EventNames.stampCount, { stamp: self.duplicateModel, increment: true }); - self.cacheSessionValues(self.duplicateModel); + this.stampService.save(this.duplicateModel).then(stamp => { + if( this.duplicateModel.id <= 0 ) { + this.eventBus.publish(EventNames.stampCount, { stamp: this.duplicateModel, increment: true }); + this.cacheSessionValues(this.duplicateModel); } - self.eventBus.publish(EventNames.stampSaved, { stamp: stamp, remainOpen: keepOpen }); + this.eventBus.publish(EventNames.stampSaved, { stamp: stamp, remainOpen: keepOpen }); if( keepOpen) { - self._resetModel(); + this._resetModel(this.duplicateModel); } }).catch(err => { logger.error(err); @@ -379,34 +378,9 @@ export class StampEditorComponent extends EventManaged { return true; } - _resetModel() { + _resetModel(theModel) { this._resetValidity(); - this.duplicateModel.id = 0; - this.duplicateModel.rate = ""; - this.duplicateModel.description = ""; - this._resetCatalogueNumber(this.activeCatalogueNumber); - this._resetOwnership(this.ownership); - this.model = this.duplicateModel; - } - - _resetCatalogueNumber(cn) { - cn.id = 0; - cn.number = ""; - cn.unknown = false; - cn.nospace = false; - cn.value = 0; - } - - _resetOwnership(owner) { - if( owner ) { - owner.notes = undefined; - owner.cert = false; - owner.img = undefined; - owner.certImg = undefined; - owner.pricePaid = 0.0; - owner.defects = 0; - owner.deception = 0; - } + this.model = StampHelper.createEmptyStamp(theModel.wantList); } _resetValidity() { @@ -434,24 +408,24 @@ export class StampEditorComponent extends EventManaged { @computedFrom('duplicateModel') get ownership() { let self = this; - if (!self.duplicateModel) { + if (!this.duplicateModel) { return undefined; } - let owners = self.duplicateModel.stampOwnerships; + let owners = this.duplicateModel.stampOwnerships; let owner; - if( self.duplicateModel.wantList === false ) { + if( this.duplicateModel.wantList === false ) { let configureOwnership = () => { - self.duplicateModel.stampOwnerships = []; + this.duplicateModel.stampOwnerships = []; owner = createOwnership(); - if( owner && self.cachedValues.purchased ) { - owner.purchased = self.cachedValues.purchased; + if( owner && this.cachedValues.purchased ) { + owner.purchased = this.cachedValues.purchased; } - self.duplicateModel.stampOwnerships.push(owner); + this.duplicateModel.stampOwnerships.push(owner); }; if (!owners) { configureOwnership(); - } else if (self.duplicateModel.stampOwnerships.length > 0) { - owner = _.first(self.duplicateModel.stampOwnerships); + } else if (this.duplicateModel.stampOwnerships.length > 0) { + owner = _.first(this.duplicateModel.stampOwnerships); } else { configureOwnership(); diff --git a/src/util/common-models.js b/src/util/common-models.js index 9c412e9..5e43c29 100644 --- a/src/util/common-models.js +++ b/src/util/common-models.js @@ -294,6 +294,18 @@ export var StampHelper = function() { } } return path; + }, + + createEmptyStamp: (wantList) => { + return { + id: 0, + rate: '', + description: '', + wantList: wantList, + countryRef: -1, + catalogueNumbers: [], + stampOwnerships: [] + }; } }; }(); diff --git a/src/views/stamps/stamp-list.js b/src/views/stamps/stamp-list.js index f87d1b6..0824175 100644 --- a/src/views/stamps/stamp-list.js +++ b/src/views/stamps/stamp-list.js @@ -25,7 +25,7 @@ import {SessionContext} from '../../services/session-context'; import {EventNames, StorageKeys, EventManaged} from '../../events/event-managed'; import {KeyCodes} from '../../events/key-codes'; import {LocationHelper} from '../../util/location-helper'; -import {StampFilter, ConditionFilter, Condition, CurrencyCode} from '../../util/common-models'; +import {StampFilter, ConditionFilter, Condition, CurrencyCode, StampHelper} from '../../util/common-models'; import {PredicateUtilities} from '../../util/object-utilities'; import {asCurrencyValueConverter} from '../../resources/value-converters/as-currency-formatted'; @@ -37,16 +37,6 @@ import _ from 'lodash'; const logger = LogManager.getLogger('stamp-list'); -function createStamp(wantList) { - return { - id: 0, - wantList: wantList, - countryRef: -1, - catalogueNumbers: [], - stampOwnerships: [] - }; -} - @inject(Element, EventAggregator, Router, Stamps, Countries, Preferences, asCurrencyValueConverter, I18N, DialogService, PdfGenerator, ReportHelper) export class StampList extends EventManaged { @@ -320,7 +310,7 @@ export class StampList extends EventManaged { showEditor(action) { if (action === 'create-stamp' || action === 'create-wantList') { - this.editingStamp = createStamp((action === 'create-wantList')); + this.editingStamp = StampHelper.createEmptyStamp((action === 'create-wantList')); this.panelContents = "stamp-editor"; } else if (action === 'search-panel') { this.panelContents = action; diff --git a/test/unit/util/common-models.spec.js b/test/unit/util/common-models.spec.js index db38dab..8078114 100644 --- a/test/unit/util/common-models.spec.js +++ b/test/unit/util/common-models.spec.js @@ -49,6 +49,30 @@ describe('EnumeratedTypeHelper test suite', () => { describe('StampHelper test suite', () => { + describe('createEmptyStamp test', () => { + it('as wantlist', () => { + let stamp = StampHelper.createEmptyStamp(true); + expect(stamp.rate).toBe(''); + expect(stamp.description).toBe(''); + expect(stamp.id).toBe(0); + expect(stamp.wantList).toBe(true); + expect(stamp.countryRef).toBe(-1); + expect(stamp.catalogueNumbers).toEqual([]); + expect(stamp.stampOwnerships).toEqual([]); + }); + + it('as stamp', () => { + let stamp = StampHelper.createEmptyStamp(false); + expect(stamp.rate).toBe(''); + expect(stamp.description).toBe(''); + expect(stamp.id).toBe(0); + expect(stamp.wantList).toBe(false); + expect(stamp.countryRef).toBe(-1); + expect(stamp.catalogueNumbers).toEqual([]); + expect(stamp.stampOwnerships).toEqual([]); + }); + }); + describe('calculateImagePath test', () => { let countryServiceSpy;