diff --git a/app/controllers/project/donate.js b/app/controllers/project/donate.js index 8f862a80e..dd97726a1 100644 --- a/app/controllers/project/donate.js +++ b/app/controllers/project/donate.js @@ -1,5 +1,6 @@ import Ember from 'ember'; import FriendlyError from 'code-corps-ember/utils/friendly-error'; +import { isValidationError } from 'code-corps-ember/utils/error-utils'; const { Controller, @@ -14,16 +15,6 @@ const CUSTOMER_CREATION_ERROR = 'There was a problem in connecting your account const CARD_CREATION_ERROR = 'There was a problem in using your payment information. Please try again.'; const SUBSCRIPTION_CREATION_ERROR = 'There was a problem in setting up your monthly donation. Please try again.'; -function isValidationError(payload) { - if (!payload.isAdapterError) { - return false; - } - - let errors = payload.errors || []; - - return errors.some((e) => e.id == 'VALIDATION_ERROR'); -} - export default Controller.extend({ amount: null, isAddingCard: false, diff --git a/app/controllers/project/settings/donations.js b/app/controllers/project/settings/donations.js index 4bcdf3afa..bd1ef9840 100644 --- a/app/controllers/project/settings/donations.js +++ b/app/controllers/project/settings/donations.js @@ -1,4 +1,6 @@ import Ember from 'ember'; +import FriendlyError from 'code-corps-ember/utils/friendly-error'; +import { isValidationError } from 'code-corps-ember/utils/error-utils'; const { Controller, @@ -7,6 +9,8 @@ const { set } = Ember; +const PROBLEM_SAVING_DONATION_GOAL = 'There was a problem saving your donation goal. Please try again.'; + export default Controller.extend({ projectDonationGoals: service(), store: service(), @@ -20,8 +24,8 @@ export default Controller.extend({ */ activateDonations(project) { get(this, 'store') - .createRecord('stripe-connect-plan', { project }) - .save(); + .createRecord('stripe-connect-plan', { project }) + .save(); }, /** @@ -74,7 +78,7 @@ export default Controller.extend({ /** * Action which commits changes to a donation goal. * - * Triggers when user clicks the save button while edditing or + * Triggers when user clicks the save button while editing or * adding a new donation goal. * * @method saveDonationGoal @@ -83,16 +87,19 @@ export default Controller.extend({ */ saveDonationGoal(donationGoal, properties) { donationGoal.setProperties(properties); - console.log('set'); - donationGoal.save().then((donationGoal) => { - let projectDonationGoals = get(this, 'projectDonationGoals'); - let project = get(this, 'project'); - project.reload().then((project) => { - projectDonationGoals.reload(project).then(() => { - this.send('cancelDonationGoal', donationGoal); - }); - }); - }); + donationGoal.save() + .then((donationGoal) => this._onDoneSaving(donationGoal)) + .catch((response) => this._onFailedSaving(response)); + } + }, + + _onDoneSaving(donationGoal) { + set(donationGoal, 'isEditing', false); + }, + + _onFailedSaving(response) { + if (!isValidationError(response)) { + set(this, 'error', new FriendlyError(PROBLEM_SAVING_DONATION_GOAL)); } } }); diff --git a/app/styles/components/donation-goal-edit.scss b/app/styles/components/donation-goal-edit.scss index ae2117417..25ee688c9 100644 --- a/app/styles/components/donation-goal-edit.scss +++ b/app/styles/components/donation-goal-edit.scss @@ -21,5 +21,10 @@ border-width: 0 1px; border-radius: 0; } + + } + + .error { + clear: left; } } diff --git a/app/templates/components/donation-goal-edit.hbs b/app/templates/components/donation-goal-edit.hbs index 4571031d2..b37ec7888 100644 --- a/app/templates/components/donation-goal-edit.hbs +++ b/app/templates/components/donation-goal-edit.hbs @@ -3,6 +3,9 @@ {{input class="amount" name="amount" type="number" placeholder="Custom amount" value=amount}} per month +{{#each donationGoal.errors.amount as |error|}} +
{{error.message}}
+{{/each}}{{error.message}}
+{{/each}} +