From 22a9786168d8c6c3837c0e6cb3e9289bd89906c6 Mon Sep 17 00:00:00 2001 From: Sebastian Kippe Date: Thu, 23 May 2019 09:46:09 +0200 Subject: [PATCH] Add contribution basics --- app/components/add-contribution/component.js | 72 ++++++++++++++++++++ app/components/add-contribution/template.hbs | 58 ++++++++++++++++ app/controllers/contributions/new.js | 28 ++++++++ app/router.js | 3 + app/services/kredits.js | 11 +++ app/templates/contributions/new.hbs | 10 +++ package-lock.json | 51 ++++++++++++++ package.json | 1 + 8 files changed, 234 insertions(+) create mode 100644 app/components/add-contribution/component.js create mode 100644 app/components/add-contribution/template.hbs create mode 100644 app/controllers/contributions/new.js create mode 100644 app/templates/contributions/new.hbs 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 @@ +
+

+ +

+

+ +

+

+ {{ember-flatpickr + allowInput=false + altFormat="F j, Y" + altInput=true + altInputClass="date-alt" + date=date + dateFormat="Y-m-d" + defaultDate=defaultDate + maxDate=defaultDate + onChange=(action (mut date)) + }} +

+

+ {{input type="text" + placeholder="500" + value=amount + class=(if isValidAmount "valid" "")}} +

+

+ {{input type="text" + placeholder="Description" + value=description + class=(if isValidDescription "valid" "")}} +

+

+ {{input type="text" + placeholder="URL (optional)" + value=url + class=(if isValidUrl "valid" "")}} +

+

+ {{input type="submit" + disabled=inProgress + value=(if inProgress "Processing" "Save")}} + {{#link-to "index"}}Back{{/link-to}} +

+
+ 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 @@ +
+
+

Submit a contribution

+
+ +
+ {{add-contribution contributors=minedContributors save=(action "save")}} +
+
+ diff --git a/package-lock.json b/package-lock.json index cc8d49b..bb54c5d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6515,6 +6515,38 @@ "semver": "^5.3.0" } }, + "ember-diff-attrs": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/ember-diff-attrs/-/ember-diff-attrs-0.2.2.tgz", + "integrity": "sha512-dziQ8G8QVRMqSFMg2l9E+Te19kcwk7+Aad7Q8lOci2b3EAiU7s0IFB3Z8rRed0JRJ3e6mPJyRmNbyUuNoyCM8g==", + "dev": true, + "requires": { + "ember-cli-babel": "^6.16.0" + }, + "dependencies": { + "ember-cli-babel": { + "version": "6.18.0", + "resolved": "https://registry.npmjs.org/ember-cli-babel/-/ember-cli-babel-6.18.0.tgz", + "integrity": "sha512-7ceC8joNYxY2wES16iIBlbPSxwKDBhYwC8drU3ZEvuPDMwVv1KzxCNu1fvxyFEBWhwaRNTUxSCsEVoTd9nosGA==", + "dev": true, + "requires": { + "amd-name-resolver": "1.2.0", + "babel-plugin-debug-macros": "^0.2.0-beta.6", + "babel-plugin-ember-modules-api-polyfill": "^2.6.0", + "babel-plugin-transform-es2015-modules-amd": "^6.24.0", + "babel-polyfill": "^6.26.0", + "babel-preset-env": "^1.7.0", + "broccoli-babel-transpiler": "^6.5.0", + "broccoli-debug": "^0.6.4", + "broccoli-funnel": "^2.0.0", + "broccoli-source": "^1.1.0", + "clone": "^2.0.0", + "ember-cli-version-checker": "^2.1.2", + "semver": "^5.5.0" + } + } + } + }, "ember-export-application-global": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ember-export-application-global/-/ember-export-application-global-2.0.0.tgz", @@ -6547,6 +6579,19 @@ } } }, + "ember-flatpickr": { + "version": "2.14.0", + "resolved": "https://registry.npmjs.org/ember-flatpickr/-/ember-flatpickr-2.14.0.tgz", + "integrity": "sha512-YId4mRiVVYm74TWVaoKeWK0Ma8QY46dloZura9gkG3n+HADTTCerRvqZv5l6J7XaLzTC7bwXkf+4wOz3sYXmUQ==", + "dev": true, + "requires": { + "ember-cli-babel": "^7.1.2", + "ember-cli-node-assets": "^0.2.2", + "ember-diff-attrs": "^0.2.2", + "fastboot-transform": "^0.1.3", + "flatpickr": "4.5.2" + } + }, "ember-load-initializers": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/ember-load-initializers/-/ember-load-initializers-1.1.0.tgz", @@ -8392,6 +8437,12 @@ "integrity": "sha1-Hxik2TgVLUlZZfnJWNkjqy3WabQ=", "dev": true }, + "flatpickr": { + "version": "4.5.2", + "resolved": "https://registry.npmjs.org/flatpickr/-/flatpickr-4.5.2.tgz", + "integrity": "sha512-jDy4QYGpmiy7+Qk8QvKJ4spjDdxcx9cxMydmq1x427HkKWBw0qizLYeYM2F6tMcvvqGjU5VpJS55j4LnsaBblA==", + "dev": true + }, "follow-redirects": { "version": "1.7.0", "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.7.0.tgz", diff --git a/package.json b/package.json index bf43f95..6486fc2 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ "ember-cli-uglify": "^2.1.0", "ember-cli-update": "^0.21.2", "ember-export-application-global": "^2.0.0", + "ember-flatpickr": "^2.14.0", "ember-load-initializers": "^1.1.0", "ember-macro-helpers": "0.17.0", "ember-maybe-import-regenerator": "^0.1.6",