-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathConnectionList.tsx
More file actions
216 lines (198 loc) · 5.88 KB
/
ConnectionList.tsx
File metadata and controls
216 lines (198 loc) · 5.88 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
'use client'
import {
type IConnectionListAPIParameter,
getConnectionsByOrg,
} from '@/app/api/connection'
import {
ITableMetadata,
getColumns,
} from '@/components/ui/generic-table-component/columns'
import React, { JSX, useEffect, useState } from 'react'
import {
clearSelectedConnection,
clearSelectedUser,
setSelectedConnection,
} from '@/lib/storageKeys'
import { useAppDispatch, useAppSelector } from '@/lib/hooks'
import { AlertComponent } from '@/components/AlertComponent'
import { DataTable } from '@/components/ui/generic-table-component/data-table'
import type { IConnectionList } from '../type/Connections'
import { RootState } from '@/lib/store'
import { generateColumns } from '@/features/verification/components/ConnectionHelperData'
const initialPageState = {
itemPerPage: 10,
page: 1,
search: '',
sortBy: 'createDateTime',
sortingOrder: 'desc',
}
type LocalOrgs = {
connectionId: string
theirLabel: string
createDateTime: string
}
const ConnectionList = (props: {
selectConnection: (connections: IConnectionList[]) => void
}): JSX.Element => {
const [listAPIParameterIssuance, setListAPIParameterIssuance] =
useState(initialPageState)
const [connectionListIssuance, setConnectionListIssuance] = useState<
IConnectionList[]
>([])
const [localOrgs, setLocalOrgs] = useState<LocalOrgs[]>([])
const [loadingIssuance, setLoadingIssuance] = useState<boolean>(false)
const [totalItem, setTotalItem] = useState(0)
const [error, setError] = useState<string | null>(null)
const dispatch = useAppDispatch()
const orgId = useAppSelector((state: RootState) => state.organization.orgId)
const selectOrganization = async (
item: IConnectionList,
checked: boolean,
): Promise<void> => {
try {
const index =
localOrgs?.length > 0
? localOrgs.findIndex((ele) => ele.connectionId === item.connectionId)
: -1
const { connectionId, theirLabel, createDateTime } = item ?? {}
if (index === -1) {
setLocalOrgs((prev: LocalOrgs[]) => [
...prev,
{
connectionId,
theirLabel,
createDateTime,
},
])
} else {
const updateLocalOrgs = [...localOrgs]
if (!checked) {
updateLocalOrgs.splice(index, 1)
}
setLocalOrgs(updateLocalOrgs)
}
} catch (error) {
console.error('SELECTED ORGANIZATION:::', error)
}
}
const getConnections = async (
apiParameter: IConnectionListAPIParameter,
): Promise<void> => {
setLoadingIssuance(true)
try {
const response = await getConnectionsByOrg({ ...apiParameter, orgId })
if (!response) {
return
}
const { data } = response
if (Array.isArray(data)) {
const { totalItems } = response
setTotalItem(totalItems)
setConnectionListIssuance(data)
setError(null)
} else {
setConnectionListIssuance([])
}
} catch (error) {
setConnectionListIssuance([])
setError(error as string)
} finally {
setLoadingIssuance(false)
}
}
const refreshPage = (): void => {
setLocalOrgs([])
}
useEffect(() => {
const clearStorageAndRefresh = async (): Promise<void> => {
refreshPage()
dispatch(clearSelectedConnection())
dispatch(clearSelectedUser())
dispatch(setSelectedConnection([]))
setConnectionListIssuance([])
setLocalOrgs([])
}
clearStorageAndRefresh()
}, [])
useEffect(() => {
props.selectConnection(localOrgs)
}, [localOrgs])
useEffect(() => {
dispatch(setSelectedConnection(localOrgs))
}, [localOrgs])
useEffect(() => {
let getData: NodeJS.Timeout | null = null
if (listAPIParameterIssuance?.search?.length >= 1) {
getData = setTimeout(() => {
getConnections(listAPIParameterIssuance)
}, 1000)
return () => clearTimeout(getData ?? undefined)
} else {
getConnections(listAPIParameterIssuance)
}
return () => clearTimeout(getData ?? undefined)
}, [listAPIParameterIssuance])
const metadata: ITableMetadata = {
enableSelection: false,
}
const columnsIssuance = getColumns<IConnectionList>({
metadata,
columnData: generateColumns(
setListAPIParameterIssuance,
selectOrganization,
),
})
return (
<div id=" issuance_connection_list" className="px-4">
<div
className="mb-4 flex items-center justify-between"
id="issued-credentials-list"
></div>
{error && (
<AlertComponent
message={JSON.stringify(error)}
type={'failure'}
onAlertClose={() => {
setError(null)
}}
/>
)}
<DataTable
placeHolder="Search Connections ..."
data={connectionListIssuance}
columns={columnsIssuance}
index="connectionId"
isLoading={loadingIssuance}
pageIndex={listAPIParameterIssuance.page - 1}
pageSize={listAPIParameterIssuance.itemPerPage}
pageCount={Math.ceil(totalItem / listAPIParameterIssuance.itemPerPage)}
onPageChange={(index) =>
setListAPIParameterIssuance((prev) => {
const newPage = index + 1
if (prev.page === newPage) {
return prev
}
return { ...prev, page: newPage }
})
}
onPageSizeChange={(size) =>
setListAPIParameterIssuance((prev) => {
if (prev.itemPerPage === size && prev.page === 1) {
return prev
}
return { ...prev, itemPerPage: size, page: 1 }
})
}
onSearchTerm={(term) =>
setListAPIParameterIssuance((prev) => {
if (prev.search === term && prev.page === 1) {
return prev
}
return { ...prev, search: term, page: 1 }
})
}
/>
</div>
)
}
export default ConnectionList