From dc23ba13754ed6d955fdd60a113d267ee053f8ee Mon Sep 17 00:00:00 2001 From: Matthew Raymer Date: Fri, 7 Jul 2023 18:43:16 +0800 Subject: [PATCH] Fix a bug in HomeView and clean up recordGive method --- src/views/HomeView.vue | 101 ++++++++++++++++++++++++++--------------- 1 file changed, 65 insertions(+), 36 deletions(-) diff --git a/src/views/HomeView.vue b/src/views/HomeView.vue index 07c4c42..a289f7c 100644 --- a/src/views/HomeView.vue +++ b/src/views/HomeView.vue @@ -98,6 +98,29 @@ export default class HomeView extends Vue { alertTitle = ""; alertMessage = ""; + public async getIdentity(activeDid) { + await accountsDB.open(); + const accounts = await accountsDB.accounts.toArray(); + const account = R.find((acc) => acc.did === activeDid, accounts); + const identity = JSON.parse(account?.identity || "null"); + + if (!identity) { + throw new Error( + "Attempted to load Give records with no identity available.", + ); + } + return identity; + } + + public async getHeaders(identity) { + const token = await accessToken(identity); + const headers = { + "Content-Type": "application/json", + Authorization: "Bearer " + token, + }; + return headers; + } + async created() { try { await accountsDB.open(); @@ -254,51 +277,57 @@ export default class HomeView extends Vue { * @param description may be an empty string * @param hours may be 0 */ - recordGive(giverDid, description, hours) { + public async recordGive(giverDid, description, hours) { if (!this.activeDid) { - this.alertTitle = "Error"; - this.alertMessage = - "You must select an identity before you can record a give."; + this.setAlert( + "Error", + "You must select an identity before you can record a give.", + ); return; } if (!description && !hours) { - this.alertTitle = "Error"; - this.alertMessage = - "You must enter a description or some number of hours."; + this.setAlert( + "Error", + "You must enter a description or some number of hours.", + ); return; } - const identity = await this.getIdentity(this.activeDid); + try { + const identity = await this.getIdentity(this.activeDid); + const result = await createAndSubmitGive( + this.axios, + this.apiServer, + identity, + giverDid, + this.activeDid, + description, + hours, + ); + + if (isGiveCreationError(result)) { + const errorMessage = getGiveCreationErrorMessage(result); + console.log("Error with give result:", result); + this.setAlert( + "Error", + errorMessage || "There was an error recording the give.", + ); + } else { + this.setAlert("Success", "That gift was recorded."); + } + } catch (error) { + console.log("Error with give caught:", error); + this.setAlert( + "Error", + getGiveErrorMessage(error) || "There was an error recording the give.", + ); + } + } - createAndSubmitGive( - this.axios, - this.apiServer, - identity, - giverDid, - this.activeDid, - description, - hours, - ) - .then((result) => { - if (isGiveCreationError(result)) { - const errorMessage = getGiveCreationErrorMessage(result); - console.log("Error with give result:", result); - this.alertTitle = "Error"; - this.alertMessage = - errorMessage || "There was an error recording the give."; - } else { - this.alertTitle = "Success"; - this.alertMessage = "That gift was recorded."; - } - }) - .catch((error) => { - console.log("Error with give caught:", error); - this.alertTitle = "Error"; - this.alertMessage = - getGiveErrorMessage(error) || - "There was an error recording the give."; - }); + private setAlert(title, message) { + this.alertTitle = title; + this.alertMessage = message; } // Helper functions for readability