Skip to content

Commit

Permalink
Changes to show multiple VC
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhishek Singh authored and dmitrizagidulin committed Jul 11, 2024
1 parent c013e57 commit 86aa8d3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
8 changes: 4 additions & 4 deletions lib/credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export async function post({ vp }: CredentialPayload): Promise<StoreCredentialRe
// signature on the VP).

const Credentials = await dbCredentials.open();
const credential = _extractCredential(vp);
const credential = _extractCredential(vp)[0];
const publicId = publicIdFrom(holder, credential);

await Credentials.insert({
Expand Down Expand Up @@ -76,10 +76,10 @@ export function publicIdFrom (holder: string, credential: VerifiableCredential):
return uuidv4();
}

function _extractCredential(vp: VerifiablePresentation): VerifiableCredential {
function _extractCredential(vp: VerifiablePresentation): VerifiableCredential[] {
return Array.isArray(vp.verifiableCredential)
? vp.verifiableCredential[0]
: vp.verifiableCredential;
? vp.verifiableCredential
: [vp.verifiableCredential];
}

/**
Expand Down
42 changes: 28 additions & 14 deletions pages/credentials/[publicCredentialId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,45 @@ import { BottomBar } from 'components/BottomBar/BottomBar';
import { extractCredentialsFrom, VerifiableObject } from 'lib/verifiableObject';
import { LoadingError } from 'components/LoadingError/LoadingError';

interface CredentialVerificationProps {
credential: VerifiableCredential;
}

const CredentialVerification: React.FC<CredentialVerificationProps> = ({
credential,
}) => {
const verificationContext = useVerification(credential);

return (
<div className={styles.verifyContainer}>
<VerificationContext.Provider value={verificationContext}>
<Container>
<CredentialCard credential={credential} />
<VerificationCard />
</Container>
</VerificationContext.Provider>
</div>
);
};

// @see https://nextjs.org/docs/basic-features/data-fetching/client-side#client-side-data-fetching-with-swr
// think this needed to be changed because of ts https://stackoverflow.com/questions/64199630/problem-with-typescript-while-making-request-to-swr
const fetcher = (input: RequestInfo, init: RequestInit, ...args: any[]) => fetch(input, init).then((res) => res.json());

const CredentialPage: NextPage = () => {
// On page load, the credential is undefined, and is loaded and set
// asynchronously from server-side API via `useSWR` hook
const [credential, setCredential] = useState<VerifiableCredential | undefined>(undefined);
const [credentials, setCredentials] = useState<VerifiableCredential[]>([]);
const [isDark, setIsDark] = useState(false);

const credentialContext = useVerification(credential as VerifiableCredential);

const router = useRouter();
const { publicCredentialId } = router.query;

const extract = (data: {vp: VerifiableObject}) => {
if (data !== undefined) {
const vp = data.vp;
const creds = extractCredentialsFrom(vp);
setCredential(creds![0])
setCredentials(creds || []);
}
}

Expand All @@ -46,22 +65,17 @@ const CredentialPage: NextPage = () => {
<BottomBar isDark={isDark}/>
</div>);
}
if (!credential) {
if (credentials.length === 0) {
return <div>Loading...</div>;
}

return (
<div className={styles.container}>
<TopBar hasLogo={true} isDark={isDark} setIsDark={setIsDark} />
<div className={styles.verifyContainer}>
<VerificationContext.Provider value={credentialContext}>
<Container>
<CredentialCard credential={credential} />
<VerificationCard />
</Container>
</VerificationContext.Provider>
</div>
<BottomBar isDark={isDark}/>
{credentials.map((credential, index) => (
<CredentialVerification credential={credential} key={index} />
))}
<BottomBar isDark={isDark} />
</div>
)
}
Expand Down

0 comments on commit 86aa8d3

Please sign in to comment.