Refactor proposal form
This commit is contained in:
@@ -1,67 +1,60 @@
|
|||||||
import Ember from 'ember';
|
import Component from 'ember-component';
|
||||||
|
import computed, { and } from 'ember-computed';
|
||||||
const {
|
import injectService from 'ember-service/inject';
|
||||||
Component,
|
import isPresent from 'kredits-web/utils/cps/is-present';
|
||||||
isPresent,
|
|
||||||
inject: {
|
|
||||||
service
|
|
||||||
},
|
|
||||||
computed
|
|
||||||
} = Ember;
|
|
||||||
|
|
||||||
export default Component.extend({
|
export default Component.extend({
|
||||||
|
kredits: injectService(),
|
||||||
|
|
||||||
kredits: service(),
|
// Default attributes used by reset
|
||||||
|
attributes: {
|
||||||
|
recipientId: null,
|
||||||
|
kind: 'community',
|
||||||
|
amount: null,
|
||||||
|
description: null,
|
||||||
|
url: null,
|
||||||
|
},
|
||||||
|
|
||||||
proposal: null,
|
didInsertElement() {
|
||||||
contributors: null,
|
this._super(...arguments);
|
||||||
inProgress: false,
|
this.reset();
|
||||||
|
},
|
||||||
|
|
||||||
isValidRecipient: computed('proposal.recipientAddress', function() {
|
contributors: [],
|
||||||
// TODO: add proper address validation
|
|
||||||
return this.get('proposal.recipientAddress') !== '';
|
isValidRecipient: isPresent('recipientId'),
|
||||||
|
isValidAmount: computed('amount', function() {
|
||||||
|
return parseInt(this.get('amount'), 10) > 0;
|
||||||
}),
|
}),
|
||||||
|
isValidDescription: isPresent('description'),
|
||||||
|
isValidUrl: isPresent('url'),
|
||||||
|
isValid: and('isValidRecipient',
|
||||||
|
'isValidAmount',
|
||||||
|
'isValidDescription'),
|
||||||
|
|
||||||
isValidAmount: computed('proposal.amount', function() {
|
reset: function() {
|
||||||
return parseInt(this.get('proposal.amount'), 10) > 0;
|
this.setProperties(this.get('attributes'));
|
||||||
}),
|
},
|
||||||
|
|
||||||
isValidUrl: computed('proposal.url', function() {
|
|
||||||
return isPresent(this.get('proposal.url'));
|
|
||||||
}),
|
|
||||||
|
|
||||||
isValidDescription: computed('proposal.description', function() {
|
|
||||||
return isPresent(this.get('proposal.description'));
|
|
||||||
}),
|
|
||||||
|
|
||||||
isValid: computed.and('isValidRecipient',
|
|
||||||
'isValidAmount',
|
|
||||||
'isValidDescription'),
|
|
||||||
|
|
||||||
actions: {
|
actions: {
|
||||||
save() {
|
submit() {
|
||||||
if (! this.get('isValid')) {
|
if (!this.get('isValid')) {
|
||||||
alert('Invalid data. Please review and try again.');
|
alert('Invalid data. Please review and try again.');
|
||||||
return false;
|
return;
|
||||||
}
|
}
|
||||||
this.set('inProgress', true);
|
|
||||||
let proposal = this.get('proposal');
|
|
||||||
|
|
||||||
// Set the recipient's IPFS profile hash so it can be used in the
|
let attributes = Object.keys(this.get('attributes'));
|
||||||
// contribution object (which is to be stored in IPFS as well)
|
let proposal = this.getProperties(attributes);
|
||||||
let contributor = this.get('contributors').findBy('address', proposal.get('recipientAddress'));
|
let saved = this.save(proposal);
|
||||||
proposal.set('recipientProfile', contributor.get('ipfsHash'));
|
|
||||||
|
|
||||||
this.get('kredits').addProposal(proposal)
|
// The promise handles inProgress
|
||||||
.then(() => {
|
this.set('inProgress', saved);
|
||||||
this.attrs.onSave();
|
|
||||||
}).catch((error) => {
|
saved.then(() => {
|
||||||
Ember.Logger.error('[add-proposal] error creating the proposal', error);
|
this.reset();
|
||||||
alert('Something went wrong.');
|
window.scroll(0,0);
|
||||||
}).finally(() => {
|
window.alert('Contributor added.');
|
||||||
this.set('inProgress', false);
|
});
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,41 +1,43 @@
|
|||||||
<form {{action "save" on="submit"}}>
|
<form {{action "submit" on="submit"}}>
|
||||||
<p>
|
<p>
|
||||||
<select required onchange={{action (mut proposal.recipientAddress) value="target.value"}}>
|
<select required onchange={{action (mut recipientId) value="target.value"}}>
|
||||||
<option value="" selected disabled hidden>Contributor</option>
|
<option value="" selected disabled hidden>Contributor</option>
|
||||||
{{#each contributors as |contributor|}}
|
{{#each contributors as |contributor|}}
|
||||||
<option value={{contributor.address}} selected={{eq proposal.recipientAddress contributor.address}}>{{contributor.github_username}}</option>
|
<option value={{contributor.id}} selected={{eq recipientId contributor.id}}>{{contributor.github_username}}</option>
|
||||||
{{/each}}
|
{{/each}}
|
||||||
</select>
|
</select>
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
<select required onchange={{action (mut proposal.kind) value="target.value"}}>
|
<select required onchange={{action (mut kind) value="target.value"}}>
|
||||||
<option value="community" selected={{eq proposal.kind "community"}}>Community</option>
|
<option value="community" selected={{eq kind "community"}}>Community</option>
|
||||||
<option value="design" selected={{eq proposal.kind "design"}}>Design</option>
|
<option value="design" selected={{eq kind "design"}}>Design</option>
|
||||||
<option value="dev" selected={{eq proposal.kind "dev"}}>Development</option>
|
<option value="dev" selected={{eq kind "dev"}}>Development</option>
|
||||||
<option value="docs" selected={{eq proposal.kind "docs"}}>Documentation</option>
|
<option value="docs" selected={{eq kind "docs"}}>Documentation</option>
|
||||||
<option value="ops" selected={{eq proposal.kind "ops"}}>IT Operations</option>
|
<option value="ops" selected={{eq kind "ops"}}>IT Operations</option>
|
||||||
</select>
|
</select>
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
{{input type="text"
|
{{input type="text"
|
||||||
placeholder="100"
|
placeholder="100"
|
||||||
value=proposal.amount
|
value=amount
|
||||||
class=(if isValidAmount 'valid' '')}}
|
class=(if isValidAmount 'valid' '')}}
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
{{input type="text"
|
{{input type="text"
|
||||||
placeholder="Description"
|
placeholder="Description"
|
||||||
value=proposal.description
|
value=description
|
||||||
class=(if isValidDescription 'valid' '')}}
|
class=(if isValidDescription 'valid' '')}}
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
{{input type="text"
|
{{input type="text"
|
||||||
placeholder="URL (optional)"
|
placeholder="URL (optional)"
|
||||||
value=proposal.url
|
value=url
|
||||||
class=(if isValidUrl 'valid' '')}}
|
class=(if isValidUrl 'valid' '')}}
|
||||||
</p>
|
</p>
|
||||||
<p class="actions">
|
<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')}}
|
||||||
{{#link-to 'index'}}Back{{/link-to}}
|
{{#link-to 'index'}}Back{{/link-to}}
|
||||||
</p>
|
</p>
|
||||||
</form>
|
</form>
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
</header>
|
</header>
|
||||||
|
|
||||||
<div class="content">
|
<div class="content">
|
||||||
{{add-proposal proposal=model contributors=contributors onSave=(action 'onSave')}}
|
{{add-proposal contributors=minedContributors save=(action 'save')}}
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user