Skip to content

Commit

Permalink
switch button
Browse files Browse the repository at this point in the history
Signed-off-by: Lucas ONeil <[email protected]>
  • Loading branch information
loneil committed Oct 17, 2023
1 parent 7eae502 commit 567d3df
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,13 @@ import Column from 'primevue/column';
import Accordion from 'primevue/accordion';
import AccordionTab from 'primevue/accordiontab';
import VueJsonPretty from 'vue-json-pretty';
import { useToast } from 'vue-toastification';
// State
import { useConfigStore, useTenantStore } from '@/store';
import { TABLE_OPT } from '@/helpers/constants';
import { storeToRefs } from 'pinia';
// Other Components
import EndorserConnect from './EndorserConnect.vue';
const toast = useToast();
const configStore = useConfigStore();
const tenantStore = useTenantStore();
const { config } = storeToRefs(configStore);
Expand All @@ -104,73 +101,10 @@ const canBecomeIssuer = computed(
tenantConfig.value?.create_public_did?.length
);
const connecttoLedger = async (ledger_id: string) => {
let prevLedgerId: any = undefined;
if (!!writeLedger.value && !!writeLedger.value.ledger_id) {
prevLedgerId = writeLedger.value.ledger_id;
}
try {
await tenantStore.setWriteLedger(ledger_id);
await connectToEndorser();
await registerPublicDid();
} catch (error) {
if (prevLedgerId) {
try {
await tenantStore.setWriteLedger(prevLedgerId);
await connectToEndorser();
} catch (endorserError) {
toast.error(`${endorserError}`);
}
toast.error(
`${error}, reverting to previously set ledger ${prevLedgerId}`
);
} else {
toast.error(`${error}`);
}
}
};
// Connect to endorser
const connectToEndorser = async () => {
try {
await tenantStore.connectToEndorser();
// Give a couple seconds to wait for active. If not done by then
// a message appears to the user saying to refresh themselves
await tenantStore.waitForActiveEndorserConnection();
await tenantStore.getEndorserConnection();
toast.success('Endorser connection request sent');
} catch (error) {
throw Error(`Failure while connecting: ${error}`);
}
};
// Register DID
const registerPublicDid = async () => {
try {
await tenantStore.registerPublicDid();
toast.success('Public DID registration sent');
} catch (error) {
throw Error(`Failure while registering: ${error}`);
}
};
// Details about endorser connection
const showNotActiveWarn = computed(
() => endorserConnection.value && endorserConnection.value.state !== 'active'
);
const isLedgerSet = computed(
() => !!writeLedger.value && !!writeLedger.value.ledger_id
);
// Handler failure logic
const currWriteLedger = computed(() => {
if (!!writeLedger.value && !!writeLedger.value.ledger_id) {
return writeLedger.value.ledger_id;
}
return null;
});
const enableLedgerSwitch = computed(
() => tenantConfig.value.enable_ledger_switch
);
</script>

<style lang="scss" scoped>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
<template>
<div v-if="showEndorserConnect">
<Button
title="Connect to endorser"
icon="pi pi-user-plus"
class="p-button-rounded p-button-icon-only p-button-text"
@click="connectToLedger()"
/>
</div>

<div v-if="showLedgerSwitch">
<Button
label="Switch Ledger"
icon="pi pi-arrow-right-arrow-left"
text
@click="switchLedger($event)"
/>
<!-- <Button
title="Connect to endorser"
icon="pi pi-user-plus"
class="p-button-rounded p-button-icon-only p-button-text"
Expand All @@ -10,7 +25,7 @@
props.ledgerInfo.ledger_id
)
"
/>
/> -->
</div>

<div
Expand Down Expand Up @@ -61,17 +76,14 @@ const { endorserConnection, publicDid, tenantConfig, writeLedger } =
storeToRefs(tenantStore);
// Set the write ledger and then connect to the relevant endorser
const connectToLedger = async (
endorser_alias: string,
ledger_id: string,
switchLeger = false
) => {
const connectToLedger = async (switchLeger = false) => {
// Track the current connected to ledger (or undefined if none)
const prevLedgerId = writeLedger?.value?.ledger_id;
try {
const quickConnect =
config.value.frontend.quickConnectEndorserName === endorser_alias;
await tenantStore.setWriteLedger(ledger_id);
config.value.frontend.quickConnectEndorserName ===
props.ledgerInfo.endorser_alias;
await tenantStore.setWriteLedger(props.ledgerInfo.ledger_id);
await connectToEndorser(quickConnect);
if (quickConnect) {
await registerPublicDid();
Expand Down Expand Up @@ -130,17 +142,39 @@ const showEndorserConnect = computed(() => {
) {
return true;
}
//... you're allowed to Switch ledgers
//... otherwise don't
return false;
});
// Show the ledger switch button when...
const showLedgerSwitch = computed(() => {
// There is an active endorser connection
// and we're looking at the row that's not the current ledger
// and the DID is set (IE the issuer process is complete)
// and the innkeeper has allowed you to swtich ledger
if (
tenantConfig.value.enable_ledger_switch &&
props.ledgerInfo.ledger_id !== currWriteLedger.value
) {
return true;
}
//... otherwise don't
return false;
});
// Switch ledger confirmation
const switchLedger = (event: any) => {
confirm.require({
target: event.currentTarget,
message:
'Switching may have consequences if you have previous issuance. \r\n At this time it will only work if the Endorser switching to is set to auto-accept and auto-endorse.',
header: 'Confirmation',
icon: 'pi pi-exclamation-triangle',
accept: () => {
connectToLedger(true);
},
});
};
// Can delete connection
const hasPublicDid = computed(() => !!publicDid.value && !!publicDid.value.did);
const canDeleteConnection = computed(
Expand Down

0 comments on commit 567d3df

Please sign in to comment.