-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathCredentials.tsx
More file actions
344 lines (321 loc) · 9.51 KB
/
Credentials.tsx
File metadata and controls
344 lines (321 loc) · 9.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
'use client'
import {
ConnectionIdCell,
DateCell,
SchemaNameCell,
StatusCellForCredential,
} from './CredentialTableCells'
import {
IColumnData,
ITableMetadata,
SortActions,
TableStyling,
getColumns,
} from '../../../../components/ui/generic-table-component/columns'
import React, { JSX, useEffect, useState } from 'react'
import { AlertComponent } from '@/components/AlertComponent'
import { AxiosResponse } from 'axios'
import { ConnectionApiSortFields } from '@/features/connections/types/connections-interface'
import { DataTable } from '../../../../components/ui/generic-table-component/data-table'
import { DidMethod } from '@/features/common/enum'
import { Features } from '@/common/enums'
import { IssuedCredential } from '../type/Issuance'
import PageContainer from '@/components/layout/page-container'
import { RefreshCw } from 'lucide-react'
import RoleViewButton from '@/components/RoleViewButton'
import { apiStatusCodes } from '@/config/CommonConstant'
import { getIssuedCredentials } from '@/app/api/Issuance'
import { getOrganizationById } from '@/app/api/organization'
import { issuanceSvgComponent } from '@/config/svgs/issuanceSvgComponent'
import { pathRoutes } from '@/config/pathRoutes'
import { useAppSelector } from '@/lib/hooks'
import { useRouter } from 'next/navigation'
interface PaginationState {
pageIndex: number
pageSize: number
pageCount: number
searchTerm: string
sortBy: string
sortOrder: SortActions
}
const Credentials = (): JSX.Element => {
const router = useRouter()
const orgId = useAppSelector((state) => state.organization.orgId)
const [loading, setLoading] = useState<boolean>(true)
const [error, setError] = useState<string | null>(null)
const [issuedCredList, setIssuedCredList] = useState<IssuedCredential[]>([])
const [walletCreated, setWalletCreated] = useState(false)
const [isW3C, setIsW3C] = useState(false)
const [reloading, setReloading] = useState<boolean>(false)
// Consolidated pagination state
const [pagination, setPagination] = useState<PaginationState>({
pageIndex: 0,
pageSize: 10,
pageCount: 1,
searchTerm: '',
sortBy: 'createDateTime',
sortOrder: 'desc',
})
const schemeSelection = async (): Promise<void> => {
router.push(pathRoutes.organizations.Issuance.issue)
}
const getIssuedCredDefs = async (
isReload: boolean = false,
): Promise<void> => {
if (isReload) {
setReloading(true)
} else {
setLoading(true)
}
try {
const response = (await getOrganizationById(orgId)) as AxiosResponse
const { data } = response
const orgDid = data?.data?.org_agents[0]?.orgDid
if (!orgDid) {
setWalletCreated(false)
return
}
const isWalletCreated = Boolean(orgDid)
setWalletCreated(isWalletCreated)
if (
orgDid.includes(DidMethod.POLYGON) ||
orgDid.includes(DidMethod.KEY) ||
orgDid.includes(DidMethod.WEB)
) {
setIsW3C(true)
} else {
setIsW3C(false)
}
if (orgId && isWalletCreated) {
const response = await getIssuedCredentials({
itemPerPage: pagination.pageSize,
page: pagination.pageIndex + 1,
search: pagination.searchTerm,
sortBy: pagination.sortBy,
sortingOrder: pagination.sortOrder,
orgId,
})
const { data } = response as AxiosResponse
if (data?.statusCode === apiStatusCodes.API_STATUS_SUCCESS) {
setIssuedCredList(data?.data?.data ?? [])
setPagination((prev) => ({
...prev,
pageCount: data?.data.lastPage ?? 1,
}))
setError(null)
} else {
setIssuedCredList([])
}
}
} catch (error) {
setIssuedCredList([])
setError(error as string)
} finally {
if (isReload) {
setReloading(false)
} else {
setLoading(false)
}
}
}
const handleReload = async (): Promise<void> => {
await getIssuedCredDefs(true)
}
useEffect(() => {
if (!orgId) {
setLoading(false)
return
}
getIssuedCredDefs()
}, [
orgId,
pagination.pageIndex,
pagination.pageSize,
pagination.sortBy,
pagination.searchTerm,
pagination.sortOrder,
])
useEffect(() => {
if (!orgId) {
return
}
// Reset all params when org changes
setPagination({
pageIndex: 0,
pageSize: 10,
pageCount: 1,
searchTerm: '',
sortBy: 'createDateTime',
sortOrder: 'desc',
})
}, [orgId])
const columnData: IColumnData[] = [
{
id: 'credentialExchangeId',
title: 'credential Exchange Id',
accessorKey: 'credentialExchangeId',
columnFunction: [
'hide',
{
sortCallBack: async (order): Promise<void> => {
setPagination((prev) => ({
...prev,
sortBy: 'credentialExchangeId',
sortOrder: order,
}))
},
},
],
},
{
id: 'connectionId',
title: 'Issued to',
accessorKey: 'connectionId',
columnFunction: [
{
sortCallBack: async (order): Promise<void> => {
setPagination((prev) => ({
...prev,
sortBy: 'connectionId',
sortOrder: order,
}))
},
},
],
cell: ({ row }) => (
<ConnectionIdCell connectionId={row.original.connectionId} />
),
},
{
id: 'schemaName',
title: 'Schema Name',
accessorKey: 'schemaName',
columnFunction: [
{
sortCallBack: async (order): Promise<void> => {
setPagination((prev) => ({
...prev,
sortBy: 'schemaName',
sortOrder: order,
}))
},
},
],
cell: ({ row }) => (
<SchemaNameCell
schemaName={row.original.schemaName}
schemaId={row.original.schemaId}
isW3C={isW3C}
/>
),
},
{
id: 'createDateTime',
title: 'Issued On',
accessorKey: 'createDateTime',
columnFunction: [
{
sortCallBack: async (order): Promise<void> => {
setPagination((prev) => ({
...prev,
sortBy: ConnectionApiSortFields.CREATE_DATE_TIME,
sortOrder: order,
}))
},
},
],
cell: ({ row }) => <DateCell date={row.original.createDateTime} />,
},
{
id: 'state',
title: 'Status',
accessorKey: 'state',
columnFunction: [
{
sortCallBack: async (order): Promise<void> => {
setPagination((prev) => ({
...prev,
sortBy: 'state',
sortOrder: order,
}))
},
},
],
cell: ({ row }) => <StatusCellForCredential state={row.original.state} />,
},
]
const metadata: ITableMetadata = {
enableSelection: false,
}
const tableStyling: TableStyling = { metadata, columnData }
const column = getColumns<IssuedCredential>(tableStyling)
return (
<PageContainer>
<div className="mb-2 flex flex-wrap items-center justify-between space-y-2 gap-x-4">
<div>
<h2 className="text-2xl font-bold tracking-tight">Credentials</h2>
<p className="text-muted-foreground">
Here's a list of issued credentials
</p>
</div>
{walletCreated && (
<div className="flex items-center gap-2">
{/* Reload Button */}
<button
onClick={handleReload}
disabled={reloading}
title="Reload table data"
className="bg-secondary text-secondary-foreground focus-visible:ring-ring inline-flex items-center justify-center gap-2 rounded-md px-3 py-2 text-sm font-medium transition-colors focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:outline-none disabled:pointer-events-none disabled:opacity-50"
>
<RefreshCw
className={`h-5 w-5 ${reloading ? 'animate-spin' : ''}`}
/>
</button>
{/* Issue Button */}
<RoleViewButton
buttonTitle="Issue"
feature={Features.ISSUANCE}
svgComponent={issuanceSvgComponent()}
onClickEvent={schemeSelection}
/>
</div>
)}
</div>
{error && (
<AlertComponent
message={typeof error === 'string' ? error : 'Something Went Wrong'}
type={'failure'}
onAlertClose={() => {
setError(null)
}}
/>
)}
<div className="-mx-4 flex-1 overflow-auto px-4 py-1 lg:flex-row lg:space-y-0 lg:space-x-12">
<DataTable
isLoading={loading}
placeHolder="Filter by Connection Id and Schema Name"
data={issuedCredList}
columns={column}
index={'credentialExchangeId'}
pageIndex={pagination.pageIndex}
pageSize={pagination.pageSize}
pageCount={pagination.pageCount}
onPageChange={(index) =>
setPagination((prev) => ({ ...prev, pageIndex: index }))
}
onPageSizeChange={(size) => {
setPagination((prev) => ({
...prev,
pageSize: size,
pageIndex: 0,
}))
}}
onSearchTerm={(term) =>
setPagination((prev) => ({ ...prev, searchTerm: term }))
}
/>
</div>
</PageContainer>
)
}
export default Credentials