|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useModalStore } from '@/stores/modalStore'; |
| 4 | +import clsx from 'clsx'; |
| 5 | +import { useRouter } from 'next/navigation'; |
| 6 | +import { CredentialModal } from './CredentialModal'; |
| 7 | + |
| 8 | +export const CredentialsView = ({ |
| 9 | + credentials, |
| 10 | +}: { |
| 11 | + credentials: any[]; |
| 12 | +}) => { |
| 13 | + const { refresh } = useRouter(); |
| 14 | + |
| 15 | + const setIsCredentialModalOpen = useModalStore.use.setIsCredentialModalOpen(); |
| 16 | + const setSelectedCredential = useModalStore.use.setSelectedCredential(); |
| 17 | + |
| 18 | + const handleRevoke = async (id: string) => { |
| 19 | + try { |
| 20 | + const response = await fetch(`/api/revoke-credential/${id}`, { |
| 21 | + method: 'POST', |
| 22 | + headers: { |
| 23 | + 'x-api-key': process.env.API_KEY || '', |
| 24 | + }, |
| 25 | + }); |
| 26 | + |
| 27 | + if (!response.ok) { |
| 28 | + throw new Error('Something went wrong'); |
| 29 | + } |
| 30 | + refresh(); |
| 31 | + } catch (error) { |
| 32 | + console.error(error); |
| 33 | + } |
| 34 | + }; |
| 35 | + |
| 36 | + return ( |
| 37 | + <> |
| 38 | + <div className="w-full max-w-7xl mx-auto h-full flex flex-col p-8"> |
| 39 | + <div className="bg-green-500 text-white py-4 px-6 rounded-t-lg"> |
| 40 | + <h1 className="text-2xl font-bold text-center">Issued Credentials</h1> |
| 41 | + </div> |
| 42 | + <div className="flex-grow overflow-auto bg-white shadow-md rounded-b-lg"> |
| 43 | + <table className="min-w-full"> |
| 44 | + <tbody> |
| 45 | + {credentials.map(({ credential, isRevoked }, index) => ( |
| 46 | + <tr |
| 47 | + key={credential.vc.id} |
| 48 | + className={clsx( |
| 49 | + 'border-b border-gray-100 transition-colors', |
| 50 | + credential.revoked |
| 51 | + ? 'bg-red-50 hover:bg-red-100' |
| 52 | + : index % 2 === 0 |
| 53 | + ? 'bg-white hover:bg-gray-50' |
| 54 | + : 'bg-gray-50 hover:bg-gray-100', |
| 55 | + )} |
| 56 | + > |
| 57 | + <td className="py-4 px-6"> |
| 58 | + <div className="flex flex-col md:flex-row md:items-center gap-2 md:gap-4"> |
| 59 | + <div className="flex-1 min-w-0"> |
| 60 | + <div className="flex flex-col md:flex-row md:items-center gap-2 md:gap-4"> |
| 61 | + <div className="text-xs font-mono truncate max-w-[150px] md:max-w-[250px]"> |
| 62 | + {credential.vc.id} |
| 63 | + </div> |
| 64 | + <span className="inline-block px-2 py-1 text-xs font-medium bg-blue-100 text-blue-800 rounded-md"> |
| 65 | + {credential.vc.type.slice(1).join(', ')} |
| 66 | + </span> |
| 67 | + <span className="text-sm text-gray-600"> |
| 68 | + {new Date(credential.iat * 1000).toLocaleString()} |
| 69 | + </span> |
| 70 | + <span className="text-sm font-medium text-gray-800"> |
| 71 | + {credentialSubject(credential)} |
| 72 | + </span> |
| 73 | + </div> |
| 74 | + </div> |
| 75 | + <div className="flex-shrink-0 mt-2 md:mt-0"> |
| 76 | + <button |
| 77 | + type="button" |
| 78 | + className="px-4 py-2 bg-gray-200 hover:bg-gray-300 text-gray-800 rounded-md transition-colors" |
| 79 | + onClick={() => { |
| 80 | + setSelectedCredential(credential); |
| 81 | + setIsCredentialModalOpen(true); |
| 82 | + }} |
| 83 | + > |
| 84 | + View |
| 85 | + </button>{' '} |
| 86 | + {isRevoked ? ( |
| 87 | + <span className="px-4 py-2 bg-red-200 text-red-800 rounded-md"> |
| 88 | + Revoked |
| 89 | + </span> |
| 90 | + ) : ( |
| 91 | + <button |
| 92 | + className="px-4 py-2 bg-gray-200 hover:bg-gray-300 text-gray-800 rounded-md transition-colors" |
| 93 | + type="button" |
| 94 | + onClick={() => handleRevoke(credential.vc.id)} |
| 95 | + > |
| 96 | + Revoke |
| 97 | + </button> |
| 98 | + )} |
| 99 | + </div> |
| 100 | + </div> |
| 101 | + </td> |
| 102 | + </tr> |
| 103 | + ))} |
| 104 | + </tbody> |
| 105 | + </table> |
| 106 | + </div> |
| 107 | + </div> |
| 108 | + <CredentialModal /> |
| 109 | + </> |
| 110 | + ); |
| 111 | +}; |
| 112 | + |
| 113 | +const credentialSubject = (credential: any) => { |
| 114 | + const credentialSubject = credential.vc.credentialSubject; |
| 115 | + |
| 116 | + if (credential.vc.type.includes('EducationCredential')) { |
| 117 | + return ( |
| 118 | + <div> |
| 119 | + {credentialSubject.currentFamilyName}{' '} |
| 120 | + {credentialSubject.currentGivenName} |
| 121 | + </div> |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + if (credential.vc.type.includes('CouponCredential')) { |
| 126 | + return ( |
| 127 | + <div> |
| 128 | + {credentialSubject.couponId} {credentialSubject.couponName} |
| 129 | + </div> |
| 130 | + ); |
| 131 | + } |
| 132 | + |
| 133 | + return JSON.stringify(credentialSubject); |
| 134 | +}; |
0 commit comments