Browse Source

feat: add a description for time gifts (and refactor errors)

kb/add-usage-guide
Trent Larson 2 years ago
parent
commit
f6a7677bdc
  1. 76
      src/views/ContactsView.vue

76
src/views/ContactsView.vue

@ -49,10 +49,6 @@
My Contacts My Contacts
</h1> </h1>
<div>
{{ errorMessage }}
</div>
<!-- New Contact --> <!-- New Contact -->
<div class="mb-4 flex"> <div class="mb-4 flex">
<input <input
@ -78,6 +74,13 @@
placeholder="1" placeholder="1"
v-model="hourInput" v-model="hourInput"
/> />
<br />
<input
class="border border rounded border-slate-400 w-48"
type="text"
placeholder="Description"
v-model="hourDescriptionInput"
/>
</div> </div>
</div> </div>
@ -144,8 +147,9 @@ export interface GiveVerifiableCredential {
"@context": string; "@context": string;
"@type": string; "@type": string;
agent: { identifier: string }; agent: { identifier: string };
recipient: { identifier: string }; description?: string;
object: { amountOfThisGood: number; unitCode: string }; object: { amountOfThisGood: number; unitCode: string };
recipient: { identifier: string };
} }
@Options({ @Options({
@ -158,9 +162,9 @@ export default class ContactsView extends Vue {
givenByMeTotals: Record<string, number> = {}; givenByMeTotals: Record<string, number> = {};
// { "did:...": amount } entry for each contact // { "did:...": amount } entry for each contact
givenToMeTotals: Record<string, number> = {}; givenToMeTotals: Record<string, number> = {};
hourDescriptionInput = "";
hourInput = "0"; hourInput = "0";
identity: IIdentifier | null = null; identity: IIdentifier | null = null;
errorMessage = "";
showGiveTotals = false; showGiveTotals = false;
// 'created' hook runs when the Vue instance is first created // 'created' hook runs when the Vue instance is first created
@ -231,7 +235,9 @@ export default class ContactsView extends Vue {
this.givenByMeTotals = contactTotals; this.givenByMeTotals = contactTotals;
} }
} catch (error) { } catch (error) {
this.errorMessage = "" + error; this.alertTitle = "Error from Server";
this.alertMessage = error as string;
this.isAlertVisible = true;
} }
// load all the time I have received // load all the time I have received
@ -259,7 +265,9 @@ export default class ContactsView extends Vue {
this.givenToMeTotals = contactTotals; this.givenToMeTotals = contactTotals;
} }
} catch (error) { } catch (error) {
this.errorMessage = "" + error; this.alertTitle = "Error from Server";
this.alertMessage = error as string;
this.isAlertVisible = true;
} }
} }
@ -275,20 +283,25 @@ export default class ContactsView extends Vue {
} }
async onClickAddGive(fromDid: string, toDid: string): Promise<void> { async onClickAddGive(fromDid: string, toDid: string): Promise<void> {
if (!this.hourInput) { if (!this.isNumeric(this.hourInput)) {
this.errorMessage = "Giving 0 hours does nothing."; this.alertTitle = "Input Error";
} else if (!this.isNumeric(this.hourInput)) { this.alertMessage =
this.errorMessage =
"This is not a valid number of hours: " + this.hourInput; "This is not a valid number of hours: " + this.hourInput;
this.isAlertVisible = true;
} else if (!parseFloat(this.hourInput)) {
this.alertTitle = "Input Error";
this.alertMessage = "Giving 0 hours does nothing.";
this.isAlertVisible = true;
} else if (!this.identity) { } else if (!this.identity) {
this.errorMessage = "No identity is available."; this.alertTitle = "Status Error";
this.alertMessage = "No identity is available.";
this.isAlertVisible = true;
} else { } else {
this.errorMessage = "";
let toFrom; let toFrom;
if (fromDid == this.identity?.did) { if (fromDid == this.identity?.did) {
toFrom = "to " + this.nameForDid(this.contacts, toDid); toFrom = "from you to " + this.nameForDid(this.contacts, toDid);
} else { } else {
toFrom = "from " + this.nameForDid(this.contacts, fromDid); toFrom = "from " + this.nameForDid(this.contacts, fromDid) + " to you";
} }
if ( if (
confirm( confirm(
@ -303,7 +316,8 @@ export default class ContactsView extends Vue {
this.identity, this.identity,
fromDid, fromDid,
toDid, toDid,
parseFloat(this.hourInput) parseFloat(this.hourInput),
this.hourDescriptionInput
); );
} }
} }
@ -313,16 +327,20 @@ export default class ContactsView extends Vue {
identity: IIdentifier, identity: IIdentifier,
fromDid: string, fromDid: string,
toDid: string, toDid: string,
amount: number amount: number,
description: string
): Promise<void> { ): Promise<void> {
// Make a claim // Make a claim
const vcClaim: GiveVerifiableCredential = { const vcClaim: GiveVerifiableCredential = {
"@context": "https://schema.org", "@context": "https://schema.org",
"@type": "GiveAction", "@type": "GiveAction",
agent: { identifier: fromDid }, agent: { identifier: fromDid },
recipient: { identifier: toDid },
object: { amountOfThisGood: amount, unitCode: "HUR" }, object: { amountOfThisGood: amount, unitCode: "HUR" },
recipient: { identifier: toDid },
}; };
if (description) {
vcClaim.description = description;
}
// Make a payload for the claim // Make a payload for the claim
const vcPayload = { const vcPayload = {
vc: { vc: {
@ -359,18 +377,17 @@ export default class ContactsView extends Vue {
const resp = await this.axios.post(url, payload, { headers }); const resp = await this.axios.post(url, payload, { headers });
//console.log("Got resp data:", resp.data); //console.log("Got resp data:", resp.data);
if (resp.data?.success?.handleId) { if (resp.data?.success?.handleId) {
this.errorMessage = "";
this.alertTitle = ""; this.alertTitle = "";
this.alertMessage = ""; this.alertMessage = "";
if (fromDid === identity.did) { if (fromDid === identity.did) {
this.givenByMeTotals[toDid] = this.givenByMeTotals[toDid] + amount; this.givenByMeTotals[toDid] = this.givenByMeTotals[toDid] + amount;
// do this to update the UI // do this to update the UI (is there a better way?)
// eslint-disable-next-line no-self-assign // eslint-disable-next-line no-self-assign
this.givenByMeTotals = this.givenByMeTotals; this.givenByMeTotals = this.givenByMeTotals;
} else { } else {
this.givenToMeTotals[fromDid] = this.givenToMeTotals[fromDid] =
this.givenToMeTotals[fromDid] + amount; this.givenToMeTotals[fromDid] + amount;
// do this to update the UI // do this to update the UI (is there a better way?)
// eslint-disable-next-line no-self-assign // eslint-disable-next-line no-self-assign
this.givenToMeTotals = this.givenToMeTotals; this.givenToMeTotals = this.givenToMeTotals;
} }
@ -379,21 +396,18 @@ export default class ContactsView extends Vue {
let userMessage = "There was an error. See logs for more info."; let userMessage = "There was an error. See logs for more info.";
const serverError = error as AxiosError; const serverError = error as AxiosError;
if (serverError) { if (serverError) {
this.isAlertVisible = true;
if (serverError.message) { if (serverError.message) {
this.alertTitle = "User Message"; userMessage = serverError.message; // Info for the user
userMessage = serverError.message; // This is info for the user.
this.alertMessage = userMessage;
} else { } else {
this.alertTitle = "Server Message"; userMessage = JSON.stringify(serverError.toJSON());
this.alertMessage = JSON.stringify(serverError.toJSON());
} }
} else { } else {
this.alertTitle = "Claim Error"; userMessage = error as string;
this.alertMessage = error as string;
} }
// Now set that error for the user to see. // Now set that error for the user to see.
this.errorMessage = userMessage; this.alertTitle = "Error with Server";
this.alertMessage = userMessage;
this.isAlertVisible = true;
} }
} }
} }

Loading…
Cancel
Save