Skip to content

Commit

Permalink
fix: create did button restrictions
Browse files Browse the repository at this point in the history
Signed-off-by: bhavanakarwade <bhavana.karwade@ayanworks.com>
  • Loading branch information
bhavanakarwade committed Jun 21, 2024
1 parent 82cefd4 commit 67babdf
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 38 deletions.
30 changes: 10 additions & 20 deletions src/components/organization/configuration-settings/DidList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useEffect, useState } from "react"
import { Button } from "flowbite-react"
import CopyDid from '../../../commonComponents/CopyDid'
import CreateDidPopup from "./CreateDid"
import { getDids, getOrganizationById, updatePrimaryDid } from "../../../api/organization"
import { getDids, updatePrimaryDid } from "../../../api/organization"
import { getFromLocalStorage } from "../../../api/Auth"
import { apiStatusCodes, storageKeys } from "../../../config/CommonConstant"
import type { AxiosResponse } from "axios"
Expand All @@ -15,8 +15,7 @@ const DIDList = () => {
const [showPopup, setShowPopup] = useState(false);
const [erroMsg, setErrMsg] = useState<string | null>(null);
const [successMsg, setSuccessMsg] = useState<string | null>(null);
const [roleName, setRoleName] = useState<string | null>(null);

const [userRoles, setUserRoles] = useState<string[]>([]);
const setPrimaryDid = async (id: string, did: string) => {
try {
const orgId = await getFromLocalStorage(storageKeys.ORG_ID);
Expand All @@ -36,22 +35,6 @@ const DIDList = () => {
}
}

const fetchOrganizationDetails = async () => {
const orgId = await getFromLocalStorage(storageKeys.ORG_ID);
const response = await getOrganizationById(orgId);
const { data } = response as AxiosResponse;
if (data?.statusCode === apiStatusCodes.API_STATUS_SUCCESS) {

const ownerRole = data?.data?.userOrgRoles.find(role => role?.orgRole.name === "owner");

const ownerRoleName = ownerRole ? ownerRole.orgRole.name : null;
setRoleName(ownerRoleName);

} else {
console.error('Error in fetching organization:::');
}
};

const getData = async () => {
try {
const orgId = await getFromLocalStorage(storageKeys.ORG_ID);
Expand All @@ -70,9 +53,15 @@ const DIDList = () => {
}
}

const getUserOrgRoles = async () => {
const orgRoles = await getFromLocalStorage(storageKeys.ORG_ROLES);
const roles = orgRoles.split(',');
setUserRoles(roles);
}

useEffect(() => {
getData();
fetchOrganizationDetails();
getUserOrgRoles();
}, [])

return (
Expand All @@ -90,6 +79,7 @@ const DIDList = () => {
<h3 className="text-lg font-bold dark:text-white">DID Details</h3>
<Button
onClick={() => setShowPopup(true)}
disabled= {userRoles.includes(Roles.MEMBER) || userRoles.includes(Roles.ISSUER) || userRoles.includes(Roles.VERIFIER)}
className={`hover:bg-primary-800 dark:hover:text-white dark:hover:bg-primary-700 hover:!bg-primary-800 text-base font-medium text-center text-white bg-primary-700 rounded-lg focus:ring-4 focus:ring-primary-300 sm:w-auto dark:bg-primary-600 dark:focus:ring-primary-800`}
>
Create DID
Expand Down
43 changes: 25 additions & 18 deletions src/components/organization/walletCommonComponents/SharedAgent.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Button, Label, Checkbox } from "flowbite-react";
import { Field, Form, Formik } from "formik";
import { Field, Form, Formik, type FormikHelpers } from "formik";
import { useState, useEffect, type ChangeEvent } from "react";
import { getLedgerConfig, getLedgers } from "../../../api/Agent";
import { apiStatusCodes } from "../../../config/CommonConstant";
import * as yup from 'yup';
import type { AxiosResponse } from 'axios';
import CopyDid from '../../../commonComponents/CopyDid';
import { DidMethod } from '../../../common/enums';
import { DidMethod, Ledgers, Network } from '../../../common/enums';
import SetDomainValueInput from './SetDomainValueInput';
import SetPrivateKeyValueInput from './SetPrivateKeyValue';
import type { ISharedAgentForm, IValuesShared } from "./interfaces";
Expand Down Expand Up @@ -58,37 +58,37 @@ const SharedAgentForm = ({
if (data?.statusCode === apiStatusCodes.API_STATUS_SUCCESS) {
const ledgerConfigData: ILedgerConfigData = {
indy: {
'did:indy': {}
[`${DidMethod.INDY}`]: {}
},
polygon: {
'did:polygon': {}
[`${DidMethod.POLYGON}`]: {}
},
noLedger: {}
};

data.data.forEach(({ name, details }: ILedgerItem) => {
const lowerName = name.toLowerCase();

if (lowerName === 'indy' && details) {
if (lowerName === Ledgers.INDY && details) {
for (const [key, subDetails] of Object.entries(details)) {
if (typeof subDetails === 'object' && subDetails !== null) {
for (const [subKey, value] of Object.entries(subDetails)) {
const formattedKey = `${key}:${subKey}`.replace('did:indy:', '');
ledgerConfigData.indy['did:indy'][formattedKey] = value;
const formattedKey = `${key}:${subKey}`.replace(`${DidMethod.INDY}:`, '');
ledgerConfigData.indy[`${DidMethod.INDY}`][formattedKey] = value;
}
}
}
} else if (lowerName === 'polygon' && details) {
} else if (lowerName === Ledgers.POLYGON && details) {
for (const [key, value] of Object.entries(details)) {
if (typeof value === 'object' && value !== null) {
for (const [subKey, subValue] of Object.entries(value)) {
ledgerConfigData.polygon['did:polygon'][subKey] = subValue;
ledgerConfigData.polygon[`${DidMethod.POLYGON}`][subKey] = subValue;
}
} else if (typeof value === 'string') {
ledgerConfigData.polygon['did:polygon'][key] = value;
ledgerConfigData.polygon[`${DidMethod.POLYGON}`][key] = value;
}
}
} else if (lowerName === 'noledger' && details) {
} else if (lowerName === Ledgers.NO_LEDGER.toLowerCase() && details) {
for (const [key, value] of Object.entries(details)) {
ledgerConfigData.noLedger[key] = value as string;
}
Expand Down Expand Up @@ -207,8 +207,13 @@ const SharedAgentForm = ({
return null;
}

return Object.keys(networks).map((network) => (
<div key={network} className="mt-2">
let filteredNetworks = Object.keys(networks);
if (selectedMethod === DidMethod.POLYGON) {
filteredNetworks = filteredNetworks.filter(network => network === Network.TESTNET);
}

return filteredNetworks.map((network) => (
<div key={network} className="mt-2">
<input
type="radio"
id={network}
Expand Down Expand Up @@ -269,8 +274,9 @@ const SharedAgentForm = ({
keyType: ''
}}
validationSchema={yup.object().shape(validations)}
onSubmit={(values: IValuesShared) => {

onSubmit={(values: IValuesShared,
actions: FormikHelpers<IValuesShared>
) => {
if (!values.privatekey) {
values.privatekey = privateKeyValue;
}
Expand All @@ -279,6 +285,7 @@ const SharedAgentForm = ({
values,
domainValue,
);
actions.resetForm();
}}
>
{(formikHandlers) => (
Expand Down Expand Up @@ -388,7 +395,7 @@ const SharedAgentForm = ({
)}
</div>

{selectedLedger !== 'noLedger' && (
{selectedLedger !== Ledgers.NO_LEDGER && (
<div className="mb-3 relative">
<label
htmlFor="network"
Expand All @@ -408,7 +415,7 @@ const SharedAgentForm = ({
</div>
)}

{selectedLedger !== 'noLedger' && (
{selectedLedger !== Ledgers.NO_LEDGER && (

<div className="mb-3 relative">
<label
Expand Down Expand Up @@ -461,7 +468,7 @@ const SharedAgentForm = ({
</div>

<div className="grid grid-cols-1 md:grid-cols-2 bg-[#F4F4F4] dark:bg-gray-700 mt-4 pl-4 pr-2 md:pr-0">
{selectedMethod === 'did:polygon' && (
{selectedMethod === DidMethod.POLYGON && (
<><div className="grid-col-1">
<SetPrivateKeyValueInput setPrivateKeyValue={setPrivateKeyValue}
privateKeyValue={privateKeyValue} formikHandlers={formikHandlers} />
Expand Down

0 comments on commit 67babdf

Please sign in to comment.