Add expenses from file

Allow to upload a JSON file containing a list of expense items
This commit is contained in:
Râu Cao
2022-12-31 15:05:36 +07:00
parent e543708b42
commit 2082b51c5b
3 changed files with 31 additions and 1 deletions
+17 -1
View File
@@ -6,6 +6,7 @@ import { action } from '@ember/object';
import { A } from '@ember/array';
import { scheduleOnce } from '@ember/runloop';
import isValidAmount from 'kredits-web/utils/is-valid-amount';
import readFileContent from 'kredits-web/utils/read-file-content';
import config from 'kredits-web/config/environment';
export default class AddReimbursementComponent extends Component {
@@ -85,7 +86,22 @@ export default class AddReimbursementComponent extends Component {
}
@action
updateContributor(event) {
async addExpensesFromFile (evt) {
const content = await readFileContent(evt.target.files[0]);
const expenses = JSON.parse(content);
if (expenses instanceof Array) {
for (const item of expenses) {
this.addExpenseItem(item);
}
} else {
console.warn("Expenses in file must be a list of items:");
console.debug(content);
}
}
@action
updateContributor (event) {
this.recipientId = event.target.value;
}