Refactor add-contributor component to DDAU

This commit is contained in:
2018-04-06 14:04:44 +02:00
committed by Michael Bumann
parent fa7db66f4e
commit 8f841522a3
7 changed files with 101 additions and 95 deletions
+44 -58
View File
@@ -1,81 +1,67 @@
import Ember from 'ember';
import Contributor from 'kredits-web/models/contributor';
import Component from 'ember-component';
import computed, { and } from 'ember-computed';
import injectService from 'ember-service/inject';
import isPresent from 'kredits-web/utils/cps/is-present';
const {
Component,
isPresent,
inject: {
service
},
computed
} = Ember;
export default Component.extend({
kredits: injectService(),
kredits: service(),
// Default attributes used by reset
attributes: {
address: null,
name: null,
kind: 'person',
url: null,
github_username: null,
github_uid: null,
wiki_username: null,
profileHash: null,
isCore: false,
},
newContributor: null,
inProgress: false,
didInsertElement() {
this._super(...arguments);
this.reset();
},
isValidAddress: function() {
isValidAddress: computed('kredits.ethProvider', 'address', function() {
// TODO: add proper address validation
return this.get('newContributor.address') !== '';
}.property('kredits.ethProvider', 'newContributor.address'),
isValidName: function() {
return isPresent(this.get('newContributor.name'));
}.property('newContributor.name'),
isValidURL: function() {
return isPresent(this.get('newContributor.url'));
}.property('newContributor.url'),
isValidGithubUID: function() {
return isPresent(this.get('newContributor.github_uid'));
}.property('newContributor.github_uid'),
isValidGithubUsername: function() {
return isPresent(this.get('newContributor.github_username'));
}.property('newContributor.github_username'),
isValidWikiUsername: function() {
return isPresent(this.get('newContributor.wiki_username'));
}.property('newContributor.wiki_username'),
isValid: computed.and(
return this.get('address') !== '';
}),
isValidName: isPresent('name'),
isValidURL: isPresent('url'),
isValidGithubUID: isPresent('github_uid'),
isValidGithubUsername: isPresent('github_username'),
isValidWikiUsername: isPresent('wiki_username'),
isValid: and(
'isValidAddress',
'isValidName',
'isValidGithubUID'
),
reset: function() {
this.setProperties({
newContributor: Contributor.create({ kind: 'person' }),
inProgress: false
});
this.setProperties(this.get('attributes'));
},
actions: {
save() {
if (!this.get('contractInteractionEnabled')) {
alert('Only core team members can add new contributors. Please ask someone to set you up.');
submit() {
if (!this.get('isValid')) {
alert('Invalid data. Please review and try again.');
return;
}
if (this.get('isValid')) {
this.set('inProgress', true);
let attributes = Object.keys(this.get('attributes'));
let contributor = this.getProperties(attributes);
let saved = this.save(contributor);
this.get('kredits').addContributor(this.get('newContributor')).then(contributor => {
this.reset();
this.get('contributors').pushObject(contributor);
window.scroll(0,0);
});
} else {
alert('Invalid data. Please review and try again.');
}
// The promise handles inProgress
this.set('inProgress', saved);
saved.then(() => {
this.reset();
window.scroll(0,0);
});
}
}
});
+17 -12
View File
@@ -1,6 +1,9 @@
<form {{action "save" on="submit"}}>
<form {{action "submit" on="submit"}}>
<p>
{{input type="checkbox" name="is-core" id="is-core" checked=newContributor.isCore}}
{{input name="is-core"
type="checkbox"
id="is-core"
checked=isCore}}
<label for="is-core" class="checkbox">
Core team member (can add contributors)
</label>
@@ -9,51 +12,53 @@
{{input name="address"
type="text"
placeholder="0xF18E631Ea191aE4ebE70046Fcb01a436554421BA4"
value=newContributor.address
value=address
class=(if isValidAddress 'valid' '')}}
</p>
<p>
<select required onchange={{action (mut newContributor.kind) value="target.value"}}>
<option value="person" selected={{eq newContributor.kind "person"}}>Person</option>
<option value="organization" selected={{eq newContributor.kind "organization"}}>Organization</option>
<select required onchange={{action (mut kind) value="target.value"}}>
<option value="person" selected={{eq kind "person"}}>Person</option>
<option value="organization" selected={{eq kind "organization"}}>Organization</option>
</select>
</p>
<p>
{{input name="name"
type="text"
placeholder="Name"
value=newContributor.name
value=name
class=(if isValidName 'valid' '')}}
</p>
<p>
{{input name="url"
type="text"
placeholder="URL"
value=newContributor.url
value=url
class=(if isValidURL 'valid' '')}}
</p>
<p>
{{input name="github_uid"
type="text"
placeholder="GitHub UID (123)"
value=newContributor.github_uid
value=github_uid
class=(if isValidGithubUID 'valid' '')}}
</p>
<p>
{{input name="github_username"
type="text"
placeholder="GitHub username"
value=newContributor.github_username
value=github_username
class=(if isValidGithubUsername 'valid' '')}}
</p>
<p>
{{input name="wiki_username"
type="text"
placeholder="Wiki Username"
value=newContributor.wiki_username
value=wiki_username
class=(if isValidWikiUsername 'valid' '')}}
</p>
<p class="actions">
{{input type="submit" value=(if inProgress 'Processing' 'Save') disabled=inProgress}}
{{input type="submit"
disabled=(is-pending inProgress)
value=(if (is-pending inProgress) 'Processing' 'Save')}}
</p>
</form>
+8 -3
View File
@@ -116,13 +116,18 @@ export default Ember.Controller.extend({
actions: {
confirmProposal(proposalId) {
this.get('kredits').vote(proposalId).then(transaction => {
window.confirm('Vote submitted to Ethereum blockhain: '+transaction.hash);
});
},
save(contributor) {
return this.get('kredits').addContributor(contributor)
.then((contributor) => {
this.get('model.contributors').pushObject(contributor);
return contributor;
});
}
}
});
+6 -3
View File
@@ -65,8 +65,11 @@
</header>
<div class="content">
{{add-contributor contributors=model.contributors
newContributor=model.newContributor
contractInteractionEnabled=contractInteractionEnabled}}
{{!--
TODO:
only show the form if currentUser.isCore else show:
Only core team members can add new contributors. Please ask someone to set you up.
--}}
{{add-contributor contributors=model.contributors save=(action 'save')}}
</div>
</section>
+8
View File
@@ -0,0 +1,8 @@
import computed from 'ember-computed';
import { isPresent } from 'ember-utils';
export default function(key) {
return computed(key, function() {
return isPresent(this.get(key));
});
}
+17 -19
View File
@@ -3144,6 +3144,11 @@
"safe-buffer": "5.1.1"
},
"dependencies": {
"core-util-is": {
"version": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz",
"integrity": "sha512-3lqz5YjWTYnW6dlDa5TLaTCcShfar1e40rmcJVwCBJC6mWlFuj0eCHIElmG1g5kyuJ/GD+8Wn4FFCcz4gJPfaQ==",
"dev": true
},
"process-nextick-args": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.0.tgz",
@@ -3163,13 +3168,6 @@
"safe-buffer": "5.1.1",
"string_decoder": "1.0.3",
"util-deprecate": "1.0.2"
},
"dependencies": {
"core-util-is": {
"version": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz",
"integrity": "sha512-3lqz5YjWTYnW6dlDa5TLaTCcShfar1e40rmcJVwCBJC6mWlFuj0eCHIElmG1g5kyuJ/GD+8Wn4FFCcz4gJPfaQ==",
"dev": true
}
}
},
"string_decoder": {
@@ -3462,6 +3460,11 @@
"source-map": "https://registry.npmjs.org/source-map/-/source-map-0.5.6.tgz"
},
"dependencies": {
"convert-source-map": {
"version": "https://registry.npmjs.org/convert-source-map/-/convert-source-map-1.3.0.tgz",
"integrity": "sha512-8iaBspZWViJD+h8epOgGyXGWH2wSDLzQ8w9qDs/L7W67IfnEeXKu/Q1cPeWpIrRJc9Mvzbn6hKxnL3goD5ncwQ==",
"dev": true
},
"minimatch": {
"version": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.3.tgz",
"integrity": "sha512-NyXjqu1IwcqH6nv5vmMtaG3iw7kdV3g6MwlUBZkc3Vn5b5AMIWYKfptvzipoyFfhlfOgBQ9zoTxQMravF1QTnw==",
@@ -5668,11 +5671,6 @@
"integrity": "sha1-PXz0Rk22RG6mRL9LOVB/mFEAjo4=",
"dev": true
},
"convert-source-map": {
"version": "https://registry.npmjs.org/convert-source-map/-/convert-source-map-1.3.0.tgz",
"integrity": "sha512-8iaBspZWViJD+h8epOgGyXGWH2wSDLzQ8w9qDs/L7W67IfnEeXKu/Q1cPeWpIrRJc9Mvzbn6hKxnL3goD5ncwQ==",
"dev": true
},
"copy-dereference": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/copy-dereference/-/copy-dereference-1.0.0.tgz",
@@ -22277,7 +22275,7 @@
"ember-cli-babel": {
"version": "6.12.0",
"resolved": "https://registry.npmjs.org/ember-cli-babel/-/ember-cli-babel-6.12.0.tgz",
"integrity": "sha512-LMwZ3Xf3Q3jQUXaJtLLJsbbhRZRNv/iea64lZ8OgqZp1fh66CSXfmqV3L9QSuYQKPDNqFiu2v6IpOT08C6GU6w==",
"integrity": "sha1-Otzb4SeNofzQuQOPE2DLSsXUQUw=",
"dev": true,
"requires": {
"amd-name-resolver": "0.0.7",
@@ -23378,7 +23376,7 @@
"ember-weakmap": {
"version": "3.1.1",
"resolved": "https://registry.npmjs.org/ember-weakmap/-/ember-weakmap-3.1.1.tgz",
"integrity": "sha512-rfW3A1m3NFsHd/NuHyBkssU0Qf0zGcJmASGfhjZc7fIQeBZvSLIFYZTej+W+YBLPtED9h/SVW63DHTRY5PUR4Q==",
"integrity": "sha1-KubgCAtbgM8NEI93Utxp6pYD29c=",
"dev": true,
"requires": {
"browserslist": "2.11.3",
@@ -23389,7 +23387,7 @@
"debug": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz",
"integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==",
"integrity": "sha1-W7WgZyYotkFJVmuhaBnmFRjGcmE=",
"dev": true,
"requires": {
"ms": "2.0.0"
@@ -23398,7 +23396,7 @@
"ember-cli-babel": {
"version": "6.12.0",
"resolved": "https://registry.npmjs.org/ember-cli-babel/-/ember-cli-babel-6.12.0.tgz",
"integrity": "sha512-LMwZ3Xf3Q3jQUXaJtLLJsbbhRZRNv/iea64lZ8OgqZp1fh66CSXfmqV3L9QSuYQKPDNqFiu2v6IpOT08C6GU6w==",
"integrity": "sha1-Otzb4SeNofzQuQOPE2DLSsXUQUw=",
"dev": true,
"requires": {
"amd-name-resolver": "0.0.7",
@@ -25695,7 +25693,7 @@
}
},
"kredits-contracts": {
"version": "github:67P/truffle-kredits#4d2ba2a4bf1bb3ceb1ae9c768abfc73a9cf5ab00",
"version": "github:67P/truffle-kredits#bdd99d58cf601d9fadd81c7f0cbd61e57cf552b5",
"dev": true
},
"lcid": {
@@ -28245,7 +28243,7 @@
},
"slash": {
"version": "https://registry.npmjs.org/slash/-/slash-1.0.0.tgz",
"integrity": "sha512-3TYDR7xWt4dIqV2JauJr+EJeW356RXijHeUlO+8djJ+uBXPn8/2dpzBc8yQhh583sVvc9CvFAeQVgijsH+PNNg==",
"integrity": "sha1-xB8vbDn8FtHNF61LXYlhFK5HDVU=",
"dev": true
},
"snapdragon": {
@@ -29020,7 +29018,7 @@
},
"trim-right": {
"version": "https://registry.npmjs.org/trim-right/-/trim-right-1.0.1.tgz",
"integrity": "sha512-WZGXGstmCWgeevgTL54hrCuw1dyMQIzWy7ZfqRJfSmJZBwklI15egmQytFP6bPidmw3M8d5yEowl1niq4vmqZw==",
"integrity": "sha1-yy4SAwZ+DI3h9hQJS5/kVwTqYAM=",
"dev": true
},
"true-case-path": {
+1
View File
@@ -53,6 +53,7 @@
"ember-load-initializers": "^0.5.1",
"ember-macro-helpers": "0.17.0",
"ember-parachute": "0.1.0",
"ember-promise-helpers": "1.0.6",
"ember-resolver": "^2.0.3",
"ember-truth-helpers": "1.3.0",
"ethers": "^3.0.8",