diff --git a/app/components/add-contribution/component.js b/app/components/add-contribution/component.js new file mode 100644 index 0000000..205e8f1 --- /dev/null +++ b/app/components/add-contribution/component.js @@ -0,0 +1,72 @@ +import Component from '@ember/component'; +import { computed } from '@ember/object'; +import { and, notEmpty } from '@ember/object/computed'; + +export default Component.extend({ + + attributes: null, + contributors: Object.freeze([]), + + isValidContributor: notEmpty('contributorId'), + isValidKind: notEmpty('kind'), + isValidAmount: computed('amount', function() { + return parseInt(this.amount, 10) > 0; + }), + isValidDescription: notEmpty('description'), + isValidUrl: notEmpty('url'), + isValid: and('isValidContributor', + 'isValidKind', + 'isValidAmount', + 'isValidDescription'), + + init () { + this._super(...arguments); + this.set('defaultDate', new Date()); + + // Default attributes used by reset + this.set('attributes', { + contributorId: null, + kind: null, + date: [new Date()], + amount: null, + description: null, + url: null, + }); + + this.reset(); + }, + + reset () { + this.setProperties(this.attributes); + }, + + actions: { + + submit () { + if (!this.isValid) { + alert('Invalid data. Please review and try again.'); + return; + } + + const attributes = this.getProperties(Object.keys(this.attributes)); + const [ date/* , time */ ] = attributes.date[0].toISOString().split('T'); + attributes.date = date; + + this.set('inProgress', true); + + this.save(attributes) + .then(contribution => { + console.debug('contribution', contribution); + this.reset(); + window.scroll(0,0); + }, err => { + console.warn(err); + window.alert('Fail'); + }) + .finally(() => this.set('inProgress', false)); + + } + + } + +}); diff --git a/app/components/add-contribution/template.hbs b/app/components/add-contribution/template.hbs new file mode 100644 index 0000000..948e9db --- /dev/null +++ b/app/components/add-contribution/template.hbs @@ -0,0 +1,58 @@ +
+ diff --git a/app/controllers/contributions/new.js b/app/controllers/contributions/new.js new file mode 100644 index 0000000..9baa5d3 --- /dev/null +++ b/app/controllers/contributions/new.js @@ -0,0 +1,28 @@ +import Controller from '@ember/controller'; +import { alias, filterBy, sort } from '@ember/object/computed'; +import { inject as service } from '@ember/service'; + +export default Controller.extend({ + + kredits: service(), + + contributors: alias('kredits.contributors'), + minedContributors: filterBy('contributors', 'id'), + sortedContributors: sort('minedContributors', Object.freeze(['name:asc'])), + + actions: { + + save (contribution) { + const contributor = this.contributors.findBy('id', contribution.contributorId); + contribution.contributorIpfsHash = contributor.ipfsHash; + + return this.kredits.addContribution(contribution) + .then(contribution => { + this.transitionToRoute('index'); + return contribution; + }); + } + + } + +}); diff --git a/app/router.js b/app/router.js index 53708e0..7a49d13 100644 --- a/app/router.js +++ b/app/router.js @@ -10,6 +10,9 @@ Router.map(function() { this.route('proposals', function() { this.route('new'); }); + this.route('contributions', function() { + this.route('new'); + }); }); export default Router; diff --git a/app/services/kredits.js b/app/services/kredits.js index 2eee5c0..c5362cc 100644 --- a/app/services/kredits.js +++ b/app/services/kredits.js @@ -203,6 +203,17 @@ export default Service.extend({ }); }, + addContribution(attributes) { + console.debug('[kredits] add contribution', attributes); + + return this.kredits.Contribution.addContribution(attributes) + .then(data => { + console.debug('[kredits] add contribution response', data); + attributes.contributor = this.contributors.findBy('id', attributes.contributorId); + return Contribution.create(attributes); + }); + }, + addProposal(attributes) { console.debug('[kredits] add proposal', attributes); diff --git a/app/templates/contributions/new.hbs b/app/templates/contributions/new.hbs new file mode 100644 index 0000000..4c375f3 --- /dev/null +++ b/app/templates/contributions/new.hbs @@ -0,0 +1,10 @@ +