From cc7c47cbab9892c66fff1dc42fc82a207377b1e0 Mon Sep 17 00:00:00 2001 From: Manuel Wiedenmann Date: Sun, 8 Apr 2018 02:00:46 +0200 Subject: [PATCH] Refactor proposal form --- app/components/add-proposal/component.js | 95 +++++++++++------------- app/components/add-proposal/template.hbs | 28 +++---- app/templates/proposals/new.hbs | 2 +- 3 files changed, 60 insertions(+), 65 deletions(-) diff --git a/app/components/add-proposal/component.js b/app/components/add-proposal/component.js index 7b7541f..e605fd6 100644 --- a/app/components/add-proposal/component.js +++ b/app/components/add-proposal/component.js @@ -1,67 +1,60 @@ -import Ember from 'ember'; - -const { - Component, - isPresent, - inject: { - service - }, - computed -} = Ember; +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'; 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, - contributors: null, - inProgress: false, + didInsertElement() { + this._super(...arguments); + this.reset(); + }, - isValidRecipient: computed('proposal.recipientAddress', function() { - // TODO: add proper address validation - return this.get('proposal.recipientAddress') !== ''; + contributors: [], + + 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() { - return parseInt(this.get('proposal.amount'), 10) > 0; - }), - - 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'), + reset: function() { + this.setProperties(this.get('attributes')); + }, actions: { - save() { - if (! this.get('isValid')) { + submit() { + if (!this.get('isValid')) { 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 - // contribution object (which is to be stored in IPFS as well) - let contributor = this.get('contributors').findBy('address', proposal.get('recipientAddress')); - proposal.set('recipientProfile', contributor.get('ipfsHash')); + let attributes = Object.keys(this.get('attributes')); + let proposal = this.getProperties(attributes); + let saved = this.save(proposal); - this.get('kredits').addProposal(proposal) - .then(() => { - this.attrs.onSave(); - }).catch((error) => { - Ember.Logger.error('[add-proposal] error creating the proposal', error); - alert('Something went wrong.'); - }).finally(() => { - this.set('inProgress', false); - }); + // The promise handles inProgress + this.set('inProgress', saved); + + saved.then(() => { + this.reset(); + window.scroll(0,0); + window.alert('Contributor added.'); + }); } } - }); diff --git a/app/components/add-proposal/template.hbs b/app/components/add-proposal/template.hbs index f04db3b..c7bb975 100644 --- a/app/components/add-proposal/template.hbs +++ b/app/components/add-proposal/template.hbs @@ -1,41 +1,43 @@ -
+

- {{#each contributors as |contributor|}} - + {{/each}}

- + + + + +

{{input type="text" placeholder="100" - value=proposal.amount + value=amount class=(if isValidAmount 'valid' '')}}

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

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

- {{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}}

diff --git a/app/templates/proposals/new.hbs b/app/templates/proposals/new.hbs index 820d989..24a9fe9 100644 --- a/app/templates/proposals/new.hbs +++ b/app/templates/proposals/new.hbs @@ -4,7 +4,7 @@
- {{add-proposal proposal=model contributors=contributors onSave=(action 'onSave')}} + {{add-proposal contributors=minedContributors save=(action 'save')}}