import Component from '@ember/component'; import { computed } from '@ember/object'; import { and, notEmpty } from '@ember/object/computed'; import { inject as injectService } from '@ember/service'; export default Component.extend({ kredits: injectService(), // Default attributes used by reset attributes: { address: null, name: null, kind: 'person', url: null, github_username: null, github_uid: null, wiki_username: null, isCore: false, }, didInsertElement() { this._super(...arguments); this.reset(); }, isValidAddress: computed('kredits.ethProvider', 'address', function() { // TODO: add proper address validation return this.address !== ''; }), isValidName: notEmpty('name'), isValidURL: notEmpty('url'), isValidGithubUID: notEmpty('github_uid'), isValidGithubUsername: notEmpty('github_username'), isValidWikiUsername: notEmpty('wiki_username'), isValid: and( 'isValidAddress', 'isValidName', 'isValidGithubUID' ), reset: function() { this.setProperties(this.attributes); }, actions: { submit() { if (!this.isValid) { alert('Invalid data. Please review and try again.'); return; } let attributes = Object.keys(this.attributes); let contributor = this.getProperties(attributes); let saved = this.save(contributor); // The promise handles inProgress this.set('inProgress', saved); saved.then(() => { this.reset(); window.scroll(0,0); window.alert('Contributor added.'); }); } } });