Browse Source

Fix a bug in HomeView and clean up recordGive method

project-gives
Matthew Raymer 1 year ago
parent
commit
dc23ba1375
  1. 75
      src/views/HomeView.vue

75
src/views/HomeView.vue

@ -98,6 +98,29 @@ export default class HomeView extends Vue {
alertTitle = ""; alertTitle = "";
alertMessage = ""; 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() { async created() {
try { try {
await accountsDB.open(); await accountsDB.open();
@ -254,24 +277,26 @@ export default class HomeView extends Vue {
* @param description may be an empty string * @param description may be an empty string
* @param hours may be 0 * @param hours may be 0
*/ */
recordGive(giverDid, description, hours) { public async recordGive(giverDid, description, hours) {
if (!this.activeDid) { if (!this.activeDid) {
this.alertTitle = "Error"; this.setAlert(
this.alertMessage = "Error",
"You must select an identity before you can record a give."; "You must select an identity before you can record a give.",
);
return; return;
} }
if (!description && !hours) { if (!description && !hours) {
this.alertTitle = "Error"; this.setAlert(
this.alertMessage = "Error",
"You must enter a description or some number of hours."; "You must enter a description or some number of hours.",
);
return; return;
} }
try {
const identity = await this.getIdentity(this.activeDid); const identity = await this.getIdentity(this.activeDid);
const result = await createAndSubmitGive(
createAndSubmitGive(
this.axios, this.axios,
this.apiServer, this.apiServer,
identity, identity,
@ -279,26 +304,30 @@ export default class HomeView extends Vue {
this.activeDid, this.activeDid,
description, description,
hours, hours,
) );
.then((result) => {
if (isGiveCreationError(result)) { if (isGiveCreationError(result)) {
const errorMessage = getGiveCreationErrorMessage(result); const errorMessage = getGiveCreationErrorMessage(result);
console.log("Error with give result:", result); console.log("Error with give result:", result);
this.alertTitle = "Error"; this.setAlert(
this.alertMessage = "Error",
errorMessage || "There was an error recording the give."; errorMessage || "There was an error recording the give.",
);
} else { } else {
this.alertTitle = "Success"; this.setAlert("Success", "That gift was recorded.");
this.alertMessage = "That gift was recorded.";
} }
}) } catch (error) {
.catch((error) => {
console.log("Error with give caught:", error); console.log("Error with give caught:", error);
this.alertTitle = "Error"; this.setAlert(
this.alertMessage = "Error",
getGiveErrorMessage(error) || getGiveErrorMessage(error) || "There was an error recording the give.",
"There was an error recording the give."; );
}); }
}
private setAlert(title, message) {
this.alertTitle = title;
this.alertMessage = message;
} }
// Helper functions for readability // Helper functions for readability

Loading…
Cancel
Save