From c7b6f9e3e7c9c7ddbb92430b3816b1f206cdf87a Mon Sep 17 00:00:00 2001 From: Sebastian Kippe Date: Thu, 1 Oct 2020 12:48:01 +0200 Subject: [PATCH] Submit reimbursements --- app/components/add-expense-item/component.js | 28 ++++++++------- app/components/add-reimbursement/component.js | 34 +++++++++++++++---- app/components/add-reimbursement/template.hbs | 11 ++++-- app/models/reimbursement.js | 10 ++---- app/services/kredits.js | 17 ++++++++++ 5 files changed, 71 insertions(+), 29 deletions(-) diff --git a/app/components/add-expense-item/component.js b/app/components/add-expense-item/component.js index 8d478ea..2486b4d 100644 --- a/app/components/add-expense-item/component.js +++ b/app/components/add-expense-item/component.js @@ -3,6 +3,7 @@ import { tracked } from '@glimmer/tracking'; import { action } from '@ember/object'; import moment from 'moment'; import isValidAmount from 'kredits-web/utils/is-valid-amount'; +import { isPresent } from '@ember/utils'; export default class AddExpenseItemComponent extends Component { // @tracked newExpense = Expense.create(); @@ -69,20 +70,21 @@ export default class AddExpenseItemComponent extends Component { const [ date ] = dateInput.toISOString().split('T'); const isValid = this.validateForm(); + if (!isValid) return false; - if (isValid) { - const expense = { - amount: parseFloat(this.amount), - currency: this.currency, - date: date, - title: this.title, - description: this.description, - url: this.url, - tags: this.tags.split(',').map(t => t.trim()) - } - this.args.addExpenseItem(expense); - } else { - return false; + const expense = { + amount: parseFloat(this.amount), + currency: this.currency, + date: date, + title: this.title, + description: isPresent(this.description) ? this.description : undefined, + url: isPresent(this.url) ? this.url : undefined, } + + if (isPresent(this.tags)) { + expense.tags = this.tags.split(',').map(t => t.trim()); + } + + this.args.addExpenseItem(expense); } } diff --git a/app/components/add-reimbursement/component.js b/app/components/add-reimbursement/component.js index eae5e05..2d28c26 100644 --- a/app/components/add-reimbursement/component.js +++ b/app/components/add-reimbursement/component.js @@ -5,16 +5,17 @@ import { inject as service } from '@ember/service'; import { action } from '@ember/object'; import { A } from '@ember/array'; import { scheduleOnce } from '@ember/runloop'; -import Reimbursement from 'kredits-web/models/reimbursement'; import isValidAmount from 'kredits-web/utils/is-valid-amount'; +import config from 'kredits-web/config/environment'; export default class AddReimbursementComponent extends Component { + @service router; @service kredits; @service exchangeRates; @alias('kredits.contributorsSorted') contributors; - @tracked recipientId = null; + @tracked contributorId = null; @tracked title = ''; @tracked total = '0'; @tracked expenses = A([]); @@ -106,9 +107,30 @@ export default class AddReimbursementComponent extends Component { @action submit (e) { e.preventDefault(); - // TODO - // amount = parseFloat(this.total) - // token = "WBTC" (or token address) - // title = "Expenses covered by contributor.name" + const contributor = this.contributors.findBy('id', this.contributorId); + + const attributes = { + amount: parseInt(parseFloat(this.total) * 100000000), // convert to sats + token: config.tokens['WBTC'], + contributorId: parseInt(this.contributorId), + title: `Expenses covered by ${contributor.name}`, + description: this.description, + url: this.url, + expenses: JSON.parse(JSON.stringify((this.expenses))) + } + + this.inProgress = true; + + this.kredits.addReimbursement(attributes) + .then((/* reimbursement */) => { + this.router.transitionTo('budget'); + }) + .catch(e => { + console.error('Could not add reimbursement:', e); + window.alert('Something went wrong. Please check the browser console.') + }) + .finally(() => { + this.inProgress = false; + }); } } diff --git a/app/components/add-reimbursement/template.hbs b/app/components/add-reimbursement/template.hbs index ab623e4..314460a 100644 --- a/app/components/add-reimbursement/template.hbs +++ b/app/components/add-reimbursement/template.hbs @@ -2,7 +2,7 @@