diff --git a/src/components/Resources/Schema/ViewSchema.tsx b/src/components/Resources/Schema/ViewSchema.tsx index 31ef9ee51..be0add464 100644 --- a/src/components/Resources/Schema/ViewSchema.tsx +++ b/src/components/Resources/Schema/ViewSchema.tsx @@ -1,12 +1,23 @@ import * as yup from 'yup'; -import { Alert, Button, Card, Label, Pagination, Spinner } from 'flowbite-react'; +import { + Alert, + Button, + Card, + Label, + Pagination, + Spinner, +} from 'flowbite-react'; import { Field, Form, Formik } from 'formik'; import { apiStatusCodes, storageKeys } from '../../../config/CommonConstant'; -import { createCredentialDefinition, getCredDeffById, getSchemaById } from '../../../api/Schema'; +import { + createCredentialDefinition, + getCredDeffById, + getSchemaById, +} from '../../../api/Schema'; import { getFromLocalStorage, setToLocalStorage } from '../../../api/Auth'; import { useEffect, useState } from 'react'; -import BackButton from '../../../commonComponents/backbutton' +import BackButton from '../../../commonComponents/backbutton'; import type { AxiosResponse } from 'axios'; import BreadCrumbs from '../../BreadCrumbs'; import CredDeffCard from '../../../commonComponents/CredentialDefinitionCard'; @@ -20,503 +31,621 @@ import { ICheckEcosystem, checkEcosystem, getEcosystemId, getUserRoles } from '. import { createCredDefRequest } from '../../../api/ecosystem'; import EcosystemProfileCard from '../../../commonComponents/EcosystemProfileCard'; import { getLedgersPlatformUrl } from '../../../api/Agent'; +import React from 'react'; interface Values { - tagName: string; - revocable: boolean; + tagName: string; + revocable: boolean; } type SchemaData = { - schema: { - attrNames: string[]; - name: string; - version: string; - issuerId: string; - }; - schemaId: string; - resolutionMetadata: Record; - schemaMetadata: { - didIndyNamespace: string; - indyLedgerSeqNo: number; - }; + schema: { + attrNames: string[]; + name: string; + version: string; + issuerId: string; + }; + schemaId: string; + resolutionMetadata: Record; + schemaMetadata: { + didIndyNamespace: string; + indyLedgerSeqNo: number; + }; }; interface ICredDefCard { - tag: string, - credentialDefinitionId: string - schemaLedgerId: string - revocable: boolean + tag: string; + credentialDefinitionId: string; + schemaLedgerId: string; + revocable: boolean; } - - +const initialPageState = { + pageNumber: 1, + pageSize: 9, + total: 0, +}; const ViewSchemas = () => { - const [schemaDetails, setSchemaDetails] = useState(null); - const [credDeffList, setCredDeffList] = useState([]); - const [loading, setLoading] = useState(true) - const [createloader, setCreateLoader] = useState(false) - const [credDeffloader, setCredDeffloader] = useState(false) - const [success, setSuccess] = useState(null) - const [credDefListErr, setCredDefListErr] = useState(null) - const [schemaDetailErr, setSchemaDetailErr] = useState(null) - const [failure, setFailure] = useState(null) - const [orgId, setOrgId] = useState('') - const [credDefAuto, setCredDefAuto] = useState('') - const [isEcosystemData, setIsEcosystemData] = useState(); - const [ledgerPlatformLoading, setLedgerPlatformLoading] = useState(false) - - - const [userRoles, setUserRoles] = useState([]) - - - const getSchemaDetails = async (SchemaId: string, organizationId: string) => { - try { - setLoading(true); - const SchemaDetails = await getSchemaById(SchemaId, organizationId); - const { data } = SchemaDetails as AxiosResponse; - - if (data?.statusCode === apiStatusCodes.API_STATUS_SUCCESS) { - setSchemaDetails(data?.data); - setCredDefAuto(`${data?.data?.response?.schema?.name} ${nanoid(8)}`); - setLoading(false); - } else { - setLoading(false); - setSchemaDetailErr(SchemaDetails as unknown as string) - } - } catch (error) { - setSchemaDetailErr('Error while fetching schema details') - console.error('Error while fetching schema details:', error); - setLoading(false); - } - }; - - const getCredentialDefinitionList = async (id: string, orgId: string) => { - try { - setCredDeffloader(true); - const credentialDefinitions = await getCredDeffById(id, orgId); - const { data } = credentialDefinitions as AxiosResponse - if (data?.data?.data.length === 0) { - setCredDefListErr('No Data Found'); - } - if (data?.statusCode === apiStatusCodes.API_STATUS_SUCCESS) { - setCredDeffList(data?.data?.data); - setCredDeffloader(false); - } else { - setCredDefListErr(credentialDefinitions as string) - setCredDeffloader(false) - } - } catch (error) { - console.error('Error while fetching credential definition list:', error); - setCredDeffloader(false); - } - }; - - - useEffect(() => { - const fetchData = async () => { - const organizationId = await getFromLocalStorage(storageKeys.ORG_ID); - setOrgId(String(organizationId)); - if (window?.location?.search) { - const str = window?.location?.search; - const schemaId = str.substring(str.indexOf('=') + 1); - await getSchemaDetails(schemaId, String(organizationId)); - await getCredentialDefinitionList(schemaId, String(organizationId)); - } - }; - - fetchData(); - }, []); - - const getUserOrgRoles = async () => { - const roles = await getUserRoles() - setUserRoles(roles) - } - - useEffect(() => { - getUserOrgRoles() - const checkEcosystemData = async () => { - const data: ICheckEcosystem = await checkEcosystem(); - setIsEcosystemData(data) - } - - checkEcosystemData(); - }, []) - - - const submit = async (values: Values) => { - if (isEcosystemData?.isEnabledEcosystem && isEcosystemData?.isEcosystemMember) { - console.log("Submitted for endorsement by ecosystem member") - setCreateLoader(true) - const schemaId = schemaDetails?.schemaId || "" - const requestPayload = { - endorse: true, - tag: values?.tagName, - schemaId, - schemaDetails: { - name: schemaDetails?.schema?.name, - version: schemaDetails?.schema?.version, - attributes: schemaDetails?.schema?.attrNames - } - } - - const ecoId = await getEcosystemId() - - const createCredDeff = await createCredDefRequest(requestPayload, ecoId, orgId); - const { data } = createCredDeff as AxiosResponse - if (data?.statusCode === apiStatusCodes.API_STATUS_CREATED) { - setCreateLoader(false) - setSuccess(data?.message) - } - else { - setFailure(createCredDeff as string) - setCreateLoader(false) - } - getCredentialDefinitionList(schemaId, orgId) - } else { - setCreateLoader(true) - const schemaId = schemaDetails?.schemaId || "" - const CredDeffFieldName: CredDeffFieldNameType = { - tag: values?.tagName, - revocable: values?.revocable, - orgId: orgId, - schemaLedgerId: schemaId - } - - const createCredDeff = await createCredentialDefinition(CredDeffFieldName, orgId); - const { data } = createCredDeff as AxiosResponse - if (data?.statusCode === apiStatusCodes.API_STATUS_CREATED) { - setCreateLoader(false) - setSuccess(data?.message) - } - else { - setFailure(createCredDeff as string) - setCreateLoader(false) - } - getCredentialDefinitionList(schemaId, orgId) - } - } - - const credDefSelectionCallback = async () => { - window.location.href = `${pathRoutes.organizations.Issuance.issue}` - } - - const fetchLedgerPlatformUrl = async (indyNamespace: string) => { - setLedgerPlatformLoading(true) - try { - const response = await getLedgersPlatformUrl(indyNamespace) - const { data } = response as AxiosResponse - setLedgerPlatformLoading(false) - if (data?.statusCode === apiStatusCodes.API_STATUS_SUCCESS) { - if (data.data.networkUrl) { - window.open(data.data.networkUrl) - } - } - } catch (err) { - setLedgerPlatformLoading(false) - } - } - - const formTitle = isEcosystemData?.isEcosystemMember ? "Credential Definition Endorsement" : "Create Credential Definition" - const submitButtonTitle = isEcosystemData?.isEcosystemMember ? { - title: "Request Endorsement", svg: - - - } : { - title: "Create", svg:
- - - -
- } - - return ( -
-
- - - { - isEcosystemData?.isEnabledEcosystem && -
- -
- } - -
-

- Schemas -

- -
-
- -
-
- - {loading ? ( -
- -
- ) : ( -
-
-
- Schema Details -
-
- - {/* */} -
-
-
-
-

- Name: {schemaDetails?.schema?.name} -

-
-
-

- Version: {schemaDetails?.schema?.version} -

-
-

- Schema ID: {schemaDetails?.schemaId} -

-

- Issuer DID: {schemaDetails?.schema?.issuerId} -

-
-
-
    -
  • -
    -
    - Attributes: - {schemaDetails?.schema?.attrNames && schemaDetails?.schema?.attrNames?.length > 0 && - schemaDetails?.schema?.attrNames.map((element: string) => ( - {element} - ))} -
    -
    -
  • -
-
-
- )} -
- { - (userRoles.includes(Roles.OWNER) - || userRoles.includes(Roles.ADMIN)) - - && -
-
- {formTitle} -
-
-
- => { - await submit(values) - formikHandlers.resetForm(); - - }} - > - {(formikHandlers): JSX.Element => ( -
-
-
-
-
- - { - (formikHandlers?.errors?.tagName && formikHandlers?.touched?.tagName) && - {formikHandlers?.errors?.tagName} - } -
-
- -
- {/*
*/} - {/* */} - {/*
+
+
+
+

+ Name: {' '} + {schemaDetails?.schema?.name} +

+
+
+

+ Version: + {schemaDetails?.schema?.version} +

+
+

+ Schema ID: + {schemaDetails?.schemaId} +

+

+ Issuer DID: + {schemaDetails?.schema?.issuerId} +

+
+
+
    +
  • +
    +
    + Attributes: + {schemaDetails?.schema?.attrNames && + schemaDetails?.schema?.attrNames?.length > 0 && + schemaDetails?.schema?.attrNames.map( + (element: string) => ( + + {' '} + {element} + + ), + )} +
    +
    +
  • +
+
+
+ )} +
+ {(userRoles.includes(Roles.OWNER) || + userRoles.includes(Roles.ADMIN)) && ( + +
+
+ {formTitle} +
+
+
+ => { + await submit(values); + formikHandlers.resetForm(); + }} + > + {(formikHandlers): JSX.Element => ( + +
+
+
+
+ + {formikHandlers?.errors?.tagName && + formikHandlers?.touched?.tagName && ( + + {formikHandlers?.errors?.tagName} + + )} +
+
+ +
+ {/*
*/} + {/* */} + {/* */} - {/*
*/} - {createloader &&
-

- - - - - Hold your coffee, this might take a moment... -

-
} -
- { - (success || failure) && -
- { - setSuccess(null) - setFailure(null) - }} - > - -

- {success || failure} -

-
-
-
- } -
-
- -
-
- -
-
- - )} -
-
-
- } -
-
-
- {schemaDetailErr && ( - setSchemaDetailErr(null)}> - -

- {schemaDetailErr} -

-
-
- )} -
-
- Credential Definitions -
- - {loading ? (
- -
) - : credDeffList && credDeffList.length > 0 ? ( -
-
- {credDeffList && credDeffList.length > 0 && - credDeffList.map((element: ICredDefCard, index: number) => ( -
- -
- )) - } -
-
- { - }} - totalPages={0} - /> -
-
) : ( - - } - onClick={() => { }} - />) - } -
- ) -} -export default ViewSchemas + {/* */} + {createloader && ( +
+

+ + + + + Hold your coffee, this might take a moment... +

+
+ )} + + {(success || failure) && ( +
+ { + setSuccess(null); + setFailure(null); + }} + > + +

{success || failure}

+
+
+
+ )} +
+
+ +
+
+ +
+
+ + )} + + + + )} + + +
+ {schemaDetailErr && ( + setSchemaDetailErr(null)}> + +

{schemaDetailErr}

+
+
+ )} +
+
+ Credential Definitions +
+ + {loading ? ( +
+ +
+ ) : credDeffList && credDeffList.length > 0 ? ( +
+
+ {credDeffList && + credDeffList.length > 0 && + credDeffList.map((element: ICredDefCard, index: number) => ( +
+ +
+ ))} +
+ {currentPage.total > 1 && ( +
+ +
+ )} +
+ ) : ( + + + + } + onClick={() => {}} + /> + )} + + ); +}; +export default ViewSchemas;