diff --git a/app/mirage/config.js b/app/mirage/config.js index 79cee5e..929ea8a 100644 --- a/app/mirage/config.js +++ b/app/mirage/config.js @@ -16,4 +16,9 @@ export function testConfig() { this.post('/species'); this.get('/species/:id'); this.put('/species/:id'); + + this.get('/characteristics'); + this.post('/characteristics'); + this.get('/characteristics/:id'); + this.put('/characteristics/:id'); } diff --git a/app/mirage/factories/characteristics.js b/app/mirage/factories/characteristics.js new file mode 100644 index 0000000..5999c50 --- /dev/null +++ b/app/mirage/factories/characteristics.js @@ -0,0 +1,10 @@ +import Mirage, { faker } from 'ember-cli-mirage'; + +export default Mirage.Factory.extend({ + characteristicName() { return faker.lorem.words().join(' '); }, + characteristicTypeName() { return faker.lorem.words().join(' '); }, + strains: [], + measurements: [], + sortOrder: faker.random.number(), + canEdit: faker.random.boolean(), +}); diff --git a/app/mixins/save-model.js b/app/mixins/save-model.js index 97cc97b..53efda9 100644 --- a/app/mixins/save-model.js +++ b/app/mixins/save-model.js @@ -4,12 +4,13 @@ import ajaxError from '../utils/ajax-error'; const { Mixin } = Ember; export default Mixin.create({ - fallbackRoute: null, + fallbackRouteSave: null, + fallbackRouteCancel: null, actions: { save: function(properties) { const model = this.get('model'); - const fallbackRoute = this.get('fallbackRoute'); + const fallbackRoute = this.get('fallbackRouteSave'); model.setProperties(properties); @@ -30,7 +31,7 @@ export default Mixin.create({ model.get('errors').clear(); model.rollbackAttributes(); - this.transitionToRoute(this.get('fallbackRoute'), model); + this.transitionToRoute(this.get('fallbackRouteCancel'), model); }, }, }); diff --git a/app/mixins/setup-metadata.js b/app/mixins/setup-metadata.js new file mode 100644 index 0000000..183f93f --- /dev/null +++ b/app/mixins/setup-metadata.js @@ -0,0 +1,14 @@ +import Ember from 'ember'; + +const { Mixin, inject: { service }} = Ember; + +export default Mixin.create({ + currentUser: service('session-account'), + metaData: null, + + setupMetaDataOnInit: Ember.on('init', function() { + this.get('currentUser.account').then((user) => { + this.set('metaData', user.get('metaData')); + }); + }), +}); diff --git a/app/pods/protected/characteristics/characteristic-form/component.js b/app/pods/protected/characteristics/characteristic-form/component.js index a53a469..f82a8ca 100644 --- a/app/pods/protected/characteristics/characteristic-form/component.js +++ b/app/pods/protected/characteristics/characteristic-form/component.js @@ -1,13 +1,60 @@ import Ember from 'ember'; +import SetupMetaData from '../../../../mixins/setup-metadata'; + +const { Component } = Ember; + +export default Component.extend(SetupMetaData, { + // Read-only attributes + characteristic: null, + isDirty: false, + + // Actions + "on-save": null, + "on-cancel": null, + "on-update": null, + + // Property mapping + propertiesList: ['characteristicName', 'characteristicTypeName', 'sortOrder'], + characteristicName: null, + characteristicTypeName: null, + sortOrder: null, + + resetOnInit: Ember.on('init', function() { + this.get('propertiesList').forEach((field) => { + const valueInCharacteristic = this.get('characteristic').get(field); + this.set(field, valueInCharacteristic); + }); + }), + + updateField: function(property, value) { + this.set(property, value); + // Manually compare against passed in value + if (this.get('characteristic').get(property) !== value) { + this.set('isDirty', true); + } else { + this.set('isDirty', false); + } + }, -export default Ember.Component.extend({ actions: { save: function() { - this.sendAction('save'); + return this.attrs['on-save'](this.getProperties(this.get('propertiesList'))); }, cancel: function() { - this.sendAction('cancel'); + return this.attrs['on-cancel'](); }, - } + + characteristicNameDidChange: function(value) { + this.updateField('characteristicName', value); + }, + + characteristicTypeNameDidChange: function(value) { + this.updateField('characteristicTypeName', value); + }, + + sortOrderDidChange: function(value) { + this.updateField('sortOrder', value); + }, + }, }); diff --git a/app/pods/protected/characteristics/characteristic-form/template.hbs b/app/pods/protected/characteristics/characteristic-form/template.hbs index b1d68c0..ea89522 100644 --- a/app/pods/protected/characteristics/characteristic-form/template.hbs +++ b/app/pods/protected/characteristics/characteristic-form/template.hbs @@ -1,20 +1,20 @@