Browse Source

fix: type-checking

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

110
src/views/ContactsView.vue

@ -114,6 +114,16 @@
</li>
</ul>
</section>
<div v-bind:class="computedAlertClassNames()">
<button
class="close-button bg-slate-200 w-8 leading-loose rounded-full absolute top-2 right-2"
@click="onClickClose()"
>
<fa icon="xmark"></fa>
</button>
<h4 class="font-bold pr-5">{{ alertTitle }}</h4>
<p>{{ alertMessage }}</p>
</div>
</template>
<script lang="ts">
@ -126,7 +136,7 @@ import { AppString } from "@/constants/app";
import { accessToken, SimpleSigner } from "@/libs/crypto";
import { IIdentifier } from "@veramo/core";
import { db } from "../db";
import { Contact } from "../db/tables/contacts.ts";
import { Contact } from "../db/tables/contacts";
export interface GiveVerifiableCredential {
"@context": string;
@ -143,11 +153,11 @@ export default class ContactsView extends Vue {
contacts: Array<Contact> = [];
contactInput = "";
// { "did:...": amount } entry for each contact
givenByMeTotals = {};
givenByMeTotals: Record<string, number> = {};
// { "did:...": amount } entry for each contact
givenToMeTotals = {};
hourInput = 0;
identity: IIdentifier = null;
givenToMeTotals: Record<string, number> = {};
hourInput = "0";
identity: IIdentifier | null = null;
errorMessage = "";
showGiveTotals = false;
@ -165,7 +175,7 @@ export default class ContactsView extends Vue {
}
}
async onClickNewContact(): void {
async onClickNewContact(): Promise<void> {
let did = this.contactInput;
let name, publicKeyBase64;
const commaPos1 = this.contactInput.indexOf(",");
@ -184,6 +194,13 @@ export default class ContactsView extends Vue {
}
async loadGives() {
if (!this.identity) {
console.error(
"Attempted to load Give records with no identity available."
);
return;
}
const endorserApiServer = AppString.DEFAULT_ENDORSER_API_SERVER;
// load all the time I have given
@ -191,7 +208,7 @@ export default class ContactsView extends Vue {
const url =
endorserApiServer +
"/api/v2/report/gives?agentId=" +
encodeURIComponent(this.identity.did);
encodeURIComponent(this.identity?.did);
const token = await accessToken(this.identity);
const headers = {
"Content-Type": "application/json",
@ -200,11 +217,12 @@ export default class ContactsView extends Vue {
const resp = await this.axios.get(url, { headers });
//console.log("Server response", resp.status, resp.data);
if (resp.status === 200) {
const contactTotals = {};
const contactTotals: Record<string, number> = {};
for (const give of resp.data.data) {
if (give.unit == "HUR") {
const prevAmount = contactTotals[give.recipientDid] || 0;
contactTotals[give.recipientDid] = prevAmount + give.amount;
const recipDid: string = give.recipientDid;
const prevAmount = contactTotals[recipDid] || 0;
contactTotals[recipDid] = prevAmount + give.amount;
}
}
//console.log("Done retrieving gives", contactTotals);
@ -228,7 +246,7 @@ export default class ContactsView extends Vue {
const resp = await this.axios.get(url, { headers });
//console.log("Server response", resp.status, resp.data);
if (resp.status === 200) {
const contactTotals = {};
const contactTotals: Record<string, number> = {};
for (const give of resp.data.data) {
if (give.unit == "HUR") {
const prevAmount = contactTotals[give.agentDid] || 0;
@ -244,27 +262,31 @@ export default class ContactsView extends Vue {
}
// from https://stackoverflow.com/a/175787/845494
private isNumeric(str): boolean {
return !isNaN(str) && !isNaN(parseFloat(str));
//
private isNumeric(str: string): boolean {
return !isNaN(+str);
}
private contactForDid(contacts: Array<Contact>, did: string): Contact {
return R.find((con) => con.did == did, contacts);
private nameForDid(contacts: Array<Contact>, did: string): string {
const contact = R.find((con) => con.did == did, contacts);
return contact?.name || "this unnamed user";
}
async onClickAddGive(fromDid: string, toDid: string): void {
async onClickAddGive(fromDid: string, toDid: string): Promise<void> {
if (!this.hourInput) {
this.errorMessage = "Giving 0 hours does nothing.";
} else if (!this.isNumeric(this.hourInput)) {
this.errorMessage =
"This is not a valid number of hours: " + this.hourInput;
} else if (!this.identity) {
this.errorMessage = "No identity is available.";
} else {
this.errorMessage = "";
let toFrom;
if (fromDid == this.identity.did) {
toFrom = "to " + this.contactForDid(this.contacts, toDid).name;
if (fromDid == this.identity?.did) {
toFrom = "to " + this.nameForDid(this.contacts, toDid);
} else {
toFrom = "from " + this.contactForDid(this.contacts, fromDid).name;
toFrom = "from " + this.nameForDid(this.contacts, fromDid);
}
if (
confirm(
@ -275,16 +297,22 @@ export default class ContactsView extends Vue {
"?"
)
) {
this.createAndSubmitGive(fromDid, toDid, parseFloat(this.hourInput));
this.createAndSubmitGive(
this.identity,
fromDid,
toDid,
parseFloat(this.hourInput)
);
}
}
}
private async createAndSubmitGive(
identity: IIdentifier,
fromDid: string,
toDid: string,
amount: number
): void {
): Promise<void> {
// Make a claim
const vcClaim: GiveVerifiableCredential = {
"@context": "https://schema.org",
@ -302,15 +330,15 @@ export default class ContactsView extends Vue {
},
};
// Create a signature using private key of identity
if (this.identity.keys[0].privateKeyHex !== null) {
if (identity.keys[0].privateKeyHex !== null) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const privateKeyHex: string = this.identity.keys[0].privateKeyHex!;
const privateKeyHex: string = identity.keys[0].privateKeyHex!;
const signer = await SimpleSigner(privateKeyHex);
const alg = undefined;
// Create a JWT for the request
const vcJwt: string = await didJwt.createJWT(vcPayload, {
alg: alg,
issuer: this.identity.did,
issuer: identity.did,
signer: signer,
});
@ -319,7 +347,7 @@ export default class ContactsView extends Vue {
const payload = JSON.stringify({ jwtEncoded: vcJwt });
const endorserApiServer = AppString.DEFAULT_ENDORSER_API_SERVER;
const url = endorserApiServer + "/api/v2/claim";
const token = await accessToken(this.identity);
const token = await accessToken(identity);
const headers = {
"Content-Type": "application/json",
Authorization: "Bearer " + token,
@ -327,12 +355,12 @@ export default class ContactsView extends Vue {
try {
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) {
this.errorMessage = "";
this.alertTitle = "";
this.alertMessage = "";
if (fromDid === this.identity.did) {
if (fromDid === identity.did) {
this.givenByMeTotals[toDid] = this.givenByMeTotals[toDid] + amount;
// do this to update the UI
// eslint-disable-next-line no-self-assign
@ -359,7 +387,6 @@ export default class ContactsView extends Vue {
this.alertMessage = JSON.stringify(serverError.toJSON());
}
} else {
console.log("Here's the full error trying to save the claim:", error);
this.alertTitle = "Claim Error";
this.alertMessage = error as string;
}
@ -368,5 +395,32 @@ export default class ContactsView extends Vue {
}
}
}
alertTitle = "";
alertMessage = "";
isAlertVisible = false;
public onClickClose() {
this.isAlertVisible = false;
this.alertTitle = "";
this.alertMessage = "";
}
public computedAlertClassNames() {
return {
hidden: !this.isAlertVisible,
"dismissable-alert": true,
"bg-slate-100": true,
"p-5": true,
rounded: true,
"drop-shadow-lg": true,
absolute: true,
"top-3": true,
"inset-x-3": true,
"transition-transform": true,
"ease-in": true,
"duration-300": true,
};
}
}
</script>

Loading…
Cancel
Save