a9480a09f1
After adding it as a separate field, I figured out that we should use the same picker, because otherwise we'd have to keep the date in sync depending on the time zone. Zones are too messy in that regard, and the date is already normalized to UTC before creating the contribution record. closes #141
71 lines
1.7 KiB
JavaScript
71 lines
1.7 KiB
JavaScript
import Component from '@ember/component';
|
|
import { computed } from '@ember/object';
|
|
import { and, notEmpty } from '@ember/object/computed';
|
|
|
|
export default Component.extend({
|
|
|
|
attributes: null,
|
|
contributors: Object.freeze([]),
|
|
|
|
isValidContributor: notEmpty('contributorId'),
|
|
isValidKind: notEmpty('kind'),
|
|
isValidAmount: computed('amount', function() {
|
|
return parseInt(this.amount, 10) > 0;
|
|
}),
|
|
isValidDescription: notEmpty('description'),
|
|
isValidUrl: notEmpty('url'),
|
|
isValid: and('isValidContributor',
|
|
'isValidKind',
|
|
'isValidAmount',
|
|
'isValidDescription'),
|
|
|
|
init () {
|
|
this._super(...arguments);
|
|
this.set('defaultDate', new Date()); // TODO use beginning of current hour
|
|
|
|
// Default attributes used by reset
|
|
this.set('attributes', {
|
|
contributorId: null,
|
|
kind: null,
|
|
date: this.defaultDate,
|
|
amount: null,
|
|
description: null,
|
|
url: null,
|
|
});
|
|
|
|
this.reset();
|
|
},
|
|
|
|
reset () {
|
|
this.setProperties(this.attributes);
|
|
},
|
|
|
|
actions: {
|
|
|
|
submit () {
|
|
if (!this.isValid) {
|
|
alert('Invalid data. Please review and try again.');
|
|
return;
|
|
}
|
|
|
|
const attributes = this.getProperties(Object.keys(this.attributes));
|
|
const [ date, time ] = attributes.date[0].toISOString().split('T');
|
|
[ attributes.date, attributes.time ] = [ date, time ];
|
|
|
|
this.set('inProgress', true);
|
|
|
|
this.save(attributes)
|
|
.then((/*contribution*/) => {
|
|
this.reset();
|
|
}, err => {
|
|
console.warn(err);
|
|
window.alert('Something went wrong. Check the browser console for details.');
|
|
})
|
|
.finally(() => this.set('inProgress', false));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|