Just as with Cook the Books, there's a class that wraps the Wasm interaction code and provides a clean interface. The contents of Transaction.js are very similar to the contents of /src/store/WasmTransactions.js from Cook the Books. Most of the changes accommodate for the categoryId being present in a transaction record and a single amount field (no more raw and cooked amounts). Additional functionality was added to interact with the database. For example, here's a function that edits an existing transaction, both in the database and the linked list from the Wasm instance:
getValidAmount(transaction) {
const { amount, type } = transaction;
return type === 'Withdrawal' ? -Math.abs(amount) : amount;
}
edit(transactionId, contents) {
const updatedTransaction = this.db.get('transactions')
.find({ id: transactionId })
.assign(contents)
.write();
const { categoryId, ...transaction } = updatedTransaction;
const amount = this.getValidAmount(transaction);
this.wasmInstance._editTransaction(transactionId, categoryId, amount);
return updatedTransaction;
}
The edit() function updates the database record that corresponds to the transactionId argument with the values in the contents argument. this.db is the database instance that was created in the load-assets.js file. Since the categoryId field is available on the updatedTransaction record, we can pass it directly to this.wasmInstance._editTransaction(). It gets passed into the constructor when a new instance of Transaction is created.