Automatically select connected user/contributor as recipient

This commit is contained in:
2024-03-20 17:22:58 +01:00
parent 55f7baecf6
commit f5f74ae27f
3 changed files with 24 additions and 6 deletions
@@ -27,6 +27,10 @@ export default class AddReimbursementComponent extends Component {
this.exchangeRates.fetchRates(); this.exchangeRates.fetchRates();
} }
get contributorId () {
return this.recipientId || this.kredits.currentUser?.id;
}
get isValidTotal () { get isValidTotal () {
return isValidAmount(this.total); return isValidAmount(this.total);
} }
@@ -104,7 +108,7 @@ export default class AddReimbursementComponent extends Component {
@action @action
updateContributor (event) { updateContributor (event) {
this.recipientId = event.target.value; this.recipientId = parseInt(event.target.value);
} }
@action @action
@@ -136,12 +140,12 @@ export default class AddReimbursementComponent extends Component {
if (!this.kredits.currentUser) { window.alert('You need to connect your RSK account first.'); return false } if (!this.kredits.currentUser) { window.alert('You need to connect your RSK account first.'); return false }
if (!this.kredits.currentUserIsCore) { window.alert('Only core contributors can submit reimbursements.'); return false } if (!this.kredits.currentUserIsCore) { window.alert('Only core contributors can submit reimbursements.'); return false }
const contributor = this.contributors.findBy('id', parseInt(this.recipientId)); const contributor = this.contributors.findBy('id', this.contributorId);
const attributes = { const attributes = {
amount: parseInt(parseFloat(this.total) * 100000000), // convert to sats amount: parseInt(parseFloat(this.total) * 100000000), // convert to sats
token: config.tokens['BTC'], token: config.tokens['BTC'],
recipientId: parseInt(this.recipientId), recipientId: this.contributorId,
title: `Expenses covered by ${contributor.name}`, title: `Expenses covered by ${contributor.name}`,
description: this.description, description: this.description,
url: this.url, url: this.url,
@@ -2,8 +2,7 @@
<label> <label>
<p class="label">Contributor:</p> <p class="label">Contributor:</p>
<p> <p>
<select required {{on "change" this.updateContributor}}> <select id="contributor" required {{on "change" this.updateContributor}}>
<option value="" selected disabled hidden></option>
{{#each this.contributors as |contributor|}} {{#each this.contributors as |contributor|}}
<option value={{contributor.id}} selected={{eq this.contributorId contributor.id}}>{{contributor.name}}</option> <option value={{contributor.id}} selected={{eq this.contributorId contributor.id}}>{{contributor.name}}</option>
{{/each}} {{/each}}
@@ -2,11 +2,26 @@ import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit'; import { setupRenderingTest } from 'ember-qunit';
import { render } from '@ember/test-helpers'; import { render } from '@ember/test-helpers';
import { hbs } from 'ember-cli-htmlbars'; import { hbs } from 'ember-cli-htmlbars';
import contributors from '../../../fixtures/contributors';
module('Integration | Component | add-reimbursement', function(hooks) { module('Integration | Component | add-reimbursement', function(hooks) {
setupRenderingTest(hooks); setupRenderingTest(hooks);
test('it works', async function(assert) { hooks.beforeEach(function(assert) {
let kredits = this.owner.lookup('service:kredits');
kredits.set('contributors', contributors);
kredits.set('currentUser', contributors.findBy('id', 3));
});
test('Contributor select menu', async function(assert) {
await render(hbs`<AddReimbursement />`);
assert.equal(this.element.querySelectorAll('select#contributor option').length, contributors.length,
'contains correct amount of items');
assert.equal(this.element.querySelector('select#contributor option:checked').value, "3",
'preselects the connected contributor account');
});
await render(hbs`<AddReimbursement />`); await render(hbs`<AddReimbursement />`);
assert.ok(true); assert.ok(true);
}); });