Add contribution basics

This commit is contained in:
2019-05-23 09:46:09 +02:00
parent 18be60da9b
commit 22a9786168
8 changed files with 234 additions and 0 deletions
@@ -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));
}
}
});
@@ -0,0 +1,58 @@
<form {{action "submit" on="submit"}}>
<p>
<select required onchange={{action (mut contributorId) value="target.value"}}>
<option value="" selected disabled hidden>Contributor</option>
{{#each contributors as |contributor|}}
<option value={{contributor.id}} selected={{eq contributorId contributor.id}}>{{contributor.github_username}}</option>
{{/each}}
</select>
</p>
<p>
<select required onchange={{action (mut kind) value="target.value"}}>
<option value="" selected disabled hidden>Kind</option>
<option value="community" selected={{eq kind "community"}}>Community</option>
<option value="design" selected={{eq kind "design"}}>Design</option>
<option value="dev" selected={{eq kind "dev"}}>Development</option>
<option value="docs" selected={{eq kind "docs"}}>Documentation</option>
<option value="ops" selected={{eq kind "ops"}}>IT Operations</option>
</select>
</p>
<p>
{{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))
}}
</p>
<p>
{{input type="text"
placeholder="500"
value=amount
class=(if isValidAmount "valid" "")}}
</p>
<p>
{{input type="text"
placeholder="Description"
value=description
class=(if isValidDescription "valid" "")}}
</p>
<p>
{{input type="text"
placeholder="URL (optional)"
value=url
class=(if isValidUrl "valid" "")}}
</p>
<p class="actions">
{{input type="submit"
disabled=inProgress
value=(if inProgress "Processing" "Save")}}
{{#link-to "index"}}Back{{/link-to}}
</p>
</form>
+28
View File
@@ -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;
});
}
}
});
+3
View File
@@ -10,6 +10,9 @@ Router.map(function() {
this.route('proposals', function() { this.route('proposals', function() {
this.route('new'); this.route('new');
}); });
this.route('contributions', function() {
this.route('new');
});
}); });
export default Router; export default Router;
+11
View File
@@ -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) { addProposal(attributes) {
console.debug('[kredits] add proposal', attributes); console.debug('[kredits] add proposal', attributes);
+10
View File
@@ -0,0 +1,10 @@
<section id="add-contribution">
<header>
<h2>Submit a contribution</h2>
</header>
<div class="content">
{{add-contribution contributors=minedContributors save=(action "save")}}
</div>
</section>
+51
View File
@@ -6515,6 +6515,38 @@
"semver": "^5.3.0" "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": { "ember-export-application-global": {
"version": "2.0.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/ember-export-application-global/-/ember-export-application-global-2.0.0.tgz", "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": { "ember-load-initializers": {
"version": "1.1.0", "version": "1.1.0",
"resolved": "https://registry.npmjs.org/ember-load-initializers/-/ember-load-initializers-1.1.0.tgz", "resolved": "https://registry.npmjs.org/ember-load-initializers/-/ember-load-initializers-1.1.0.tgz",
@@ -8392,6 +8437,12 @@
"integrity": "sha1-Hxik2TgVLUlZZfnJWNkjqy3WabQ=", "integrity": "sha1-Hxik2TgVLUlZZfnJWNkjqy3WabQ=",
"dev": true "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": { "follow-redirects": {
"version": "1.7.0", "version": "1.7.0",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.7.0.tgz", "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.7.0.tgz",
+1
View File
@@ -45,6 +45,7 @@
"ember-cli-uglify": "^2.1.0", "ember-cli-uglify": "^2.1.0",
"ember-cli-update": "^0.21.2", "ember-cli-update": "^0.21.2",
"ember-export-application-global": "^2.0.0", "ember-export-application-global": "^2.0.0",
"ember-flatpickr": "^2.14.0",
"ember-load-initializers": "^1.1.0", "ember-load-initializers": "^1.1.0",
"ember-macro-helpers": "0.17.0", "ember-macro-helpers": "0.17.0",
"ember-maybe-import-regenerator": "^0.1.6", "ember-maybe-import-regenerator": "^0.1.6",