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> </li>
</ul> </ul>
</section> </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> </template>
<script lang="ts"> <script lang="ts">
@ -126,7 +136,7 @@ import { AppString } from "@/constants/app";
import { accessToken, SimpleSigner } from "@/libs/crypto"; import { accessToken, SimpleSigner } from "@/libs/crypto";
import { IIdentifier } from "@veramo/core"; import { IIdentifier } from "@veramo/core";
import { db } from "../db"; import { db } from "../db";
import { Contact } from "../db/tables/contacts.ts"; import { Contact } from "../db/tables/contacts";
export interface GiveVerifiableCredential { export interface GiveVerifiableCredential {
"@context": string; "@context": string;
@ -143,11 +153,11 @@ export default class ContactsView extends Vue {
contacts: Array<Contact> = []; contacts: Array<Contact> = [];
contactInput = ""; contactInput = "";
// { "did:...": amount } entry for each contact // { "did:...": amount } entry for each contact
givenByMeTotals = {}; givenByMeTotals: Record<string, number> = {};
// { "did:...": amount } entry for each contact // { "did:...": amount } entry for each contact
givenToMeTotals = {}; givenToMeTotals: Record<string, number> = {};
hourInput = 0; hourInput = "0";
identity: IIdentifier = null; identity: IIdentifier | null = null;
errorMessage = ""; errorMessage = "";
showGiveTotals = false; showGiveTotals = false;
@ -165,7 +175,7 @@ export default class ContactsView extends Vue {
} }
} }
async onClickNewContact(): void { async onClickNewContact(): Promise<void> {
let did = this.contactInput; let did = this.contactInput;
let name, publicKeyBase64; let name, publicKeyBase64;
const commaPos1 = this.contactInput.indexOf(","); const commaPos1 = this.contactInput.indexOf(",");
@ -184,6 +194,13 @@ export default class ContactsView extends Vue {
} }
async loadGives() { async loadGives() {
if (!this.identity) {
console.error(
"Attempted to load Give records with no identity available."
);
return;
}
const endorserApiServer = AppString.DEFAULT_ENDORSER_API_SERVER; const endorserApiServer = AppString.DEFAULT_ENDORSER_API_SERVER;
// load all the time I have given // load all the time I have given
@ -191,7 +208,7 @@ export default class ContactsView extends Vue {
const url = const url =
endorserApiServer + endorserApiServer +
"/api/v2/report/gives?agentId=" + "/api/v2/report/gives?agentId=" +
encodeURIComponent(this.identity.did); encodeURIComponent(this.identity?.did);
const token = await accessToken(this.identity); const token = await accessToken(this.identity);
const headers = { const headers = {
"Content-Type": "application/json", "Content-Type": "application/json",
@ -200,11 +217,12 @@ export default class ContactsView extends Vue {
const resp = await this.axios.get(url, { headers }); const resp = await this.axios.get(url, { headers });
//console.log("Server response", resp.status, resp.data); //console.log("Server response", resp.status, resp.data);
if (resp.status === 200) { if (resp.status === 200) {
const contactTotals = {}; const contactTotals: Record<string, number> = {};
for (const give of resp.data.data) { for (const give of resp.data.data) {
if (give.unit == "HUR") { if (give.unit == "HUR") {
const prevAmount = contactTotals[give.recipientDid] || 0; const recipDid: string = give.recipientDid;
contactTotals[give.recipientDid] = prevAmount + give.amount; const prevAmount = contactTotals[recipDid] || 0;
contactTotals[recipDid] = prevAmount + give.amount;
} }
} }
//console.log("Done retrieving gives", contactTotals); //console.log("Done retrieving gives", contactTotals);
@ -228,7 +246,7 @@ export default class ContactsView extends Vue {
const resp = await this.axios.get(url, { headers }); const resp = await this.axios.get(url, { headers });
//console.log("Server response", resp.status, resp.data); //console.log("Server response", resp.status, resp.data);
if (resp.status === 200) { if (resp.status === 200) {
const contactTotals = {}; const contactTotals: Record<string, number> = {};
for (const give of resp.data.data) { for (const give of resp.data.data) {
if (give.unit == "HUR") { if (give.unit == "HUR") {
const prevAmount = contactTotals[give.agentDid] || 0; const prevAmount = contactTotals[give.agentDid] || 0;
@ -244,27 +262,31 @@ export default class ContactsView extends Vue {
} }
// from https://stackoverflow.com/a/175787/845494 // 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 { private nameForDid(contacts: Array<Contact>, did: string): string {
return R.find((con) => con.did == did, contacts); 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) { if (!this.hourInput) {
this.errorMessage = "Giving 0 hours does nothing."; this.errorMessage = "Giving 0 hours does nothing.";
} else if (!this.isNumeric(this.hourInput)) { } else if (!this.isNumeric(this.hourInput)) {
this.errorMessage = this.errorMessage =
"This is not a valid number of hours: " + this.hourInput; "This is not a valid number of hours: " + this.hourInput;
} else if (!this.identity) {
this.errorMessage = "No identity is available.";
} else { } else {
this.errorMessage = ""; this.errorMessage = "";
let toFrom; let toFrom;
if (fromDid == this.identity.did) { if (fromDid == this.identity?.did) {
toFrom = "to " + this.contactForDid(this.contacts, toDid).name; toFrom = "to " + this.nameForDid(this.contacts, toDid);
} else { } else {
toFrom = "from " + this.contactForDid(this.contacts, fromDid).name; toFrom = "from " + this.nameForDid(this.contacts, fromDid);
} }
if ( if (
confirm( 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( private async createAndSubmitGive(
identity: IIdentifier,
fromDid: string, fromDid: string,
toDid: string, toDid: string,
amount: number amount: number
): void { ): Promise<void> {
// Make a claim // Make a claim
const vcClaim: GiveVerifiableCredential = { const vcClaim: GiveVerifiableCredential = {
"@context": "https://schema.org", "@context": "https://schema.org",
@ -302,15 +330,15 @@ export default class ContactsView extends Vue {
}, },
}; };
// Create a signature using private key of identity // 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 // 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 signer = await SimpleSigner(privateKeyHex);
const alg = undefined; const alg = undefined;
// Create a JWT for the request // Create a JWT for the request
const vcJwt: string = await didJwt.createJWT(vcPayload, { const vcJwt: string = await didJwt.createJWT(vcPayload, {
alg: alg, alg: alg,
issuer: this.identity.did, issuer: identity.did,
signer: signer, signer: signer,
}); });
@ -319,7 +347,7 @@ export default class ContactsView extends Vue {
const payload = JSON.stringify({ jwtEncoded: vcJwt }); const payload = JSON.stringify({ jwtEncoded: vcJwt });
const endorserApiServer = AppString.DEFAULT_ENDORSER_API_SERVER; const endorserApiServer = AppString.DEFAULT_ENDORSER_API_SERVER;
const url = endorserApiServer + "/api/v2/claim"; const url = endorserApiServer + "/api/v2/claim";
const token = await accessToken(this.identity); const token = await accessToken(identity);
const headers = { const headers = {
"Content-Type": "application/json", "Content-Type": "application/json",
Authorization: "Bearer " + token, Authorization: "Bearer " + token,
@ -327,12 +355,12 @@ export default class ContactsView extends Vue {
try { try {
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.errorMessage = "";
this.alertTitle = ""; this.alertTitle = "";
this.alertMessage = ""; this.alertMessage = "";
if (fromDid === this.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
// eslint-disable-next-line no-self-assign // eslint-disable-next-line no-self-assign
@ -359,7 +387,6 @@ export default class ContactsView extends Vue {
this.alertMessage = JSON.stringify(serverError.toJSON()); this.alertMessage = JSON.stringify(serverError.toJSON());
} }
} else { } else {
console.log("Here's the full error trying to save the claim:", error);
this.alertTitle = "Claim Error"; this.alertTitle = "Claim Error";
this.alertMessage = error as string; 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> </script>

Loading…
Cancel
Save