Skip to content

Commit 941c28d

Browse files
deo002heliocastro
authored andcommitted
fix(projects): Fix errors appearing in project details page
1 parent 75e2647 commit 941c28d

File tree

3 files changed

+49
-43
lines changed

3 files changed

+49
-43
lines changed

src/app/[locale]/projects/detail/[id]/components/ProjectDetailTab.tsx

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
ActionType,
2222
AdministrationDataType,
2323
ClearingDetailsCount,
24+
ErrorDetails,
2425
SummaryDataType,
2526
UserGroupType,
2627
} from '@/object-types'
@@ -43,7 +44,7 @@ import VulnerabilityTrackingStatusComponent from './VulnerabilityTrackingStatus'
4344

4445
export default function ViewProjects({ projectId }: { projectId: string }): JSX.Element {
4546
const t = useTranslations('default')
46-
const { data: session, status } = useSession()
47+
const session = useSession()
4748
const [summaryData, setSummaryData] = useState<SummaryDataType | undefined>(undefined)
4849
const [clearingDetailCount, setClearingDetailCount] = useState<ClearingDetailsCount | undefined>()
4950
const [administrationData, setAdministrationData] = useState<AdministrationDataType | undefined>(undefined)
@@ -59,11 +60,11 @@ export default function ViewProjects({ projectId }: { projectId: string }): JSX.
5960
})
6061

6162
useEffect(() => {
62-
if (status === 'unauthenticated') {
63+
if (session.status === 'unauthenticated') {
6364
signOut()
6465
}
6566
}, [
66-
status,
67+
session,
6768
])
6869

6970
useEffect(() => {
@@ -79,56 +80,58 @@ export default function ViewProjects({ projectId }: { projectId: string }): JSX.
7980
}
8081

8182
useEffect(() => {
83+
if (session.status === 'loading') return
8284
const controller = new AbortController()
8385
const signal = controller.signal
8486

8587
void (async () => {
8688
try {
87-
const session = await getSession()
88-
if (CommonUtils.isNullOrUndefined(session)) return signOut()
89+
if (CommonUtils.isNullOrUndefined(session.data)) return signOut()
8990
const response = await ApiUtils.GET(
9091
`projects/${projectId}/summaryAdministration`,
91-
session.user.access_token,
92+
session.data.user.access_token,
9293
signal,
9394
)
94-
if (response.status === StatusCodes.UNAUTHORIZED) {
95-
return signOut()
96-
} else if (response.status !== StatusCodes.OK) {
97-
return notFound()
95+
if (response.status !== StatusCodes.OK) {
96+
const err = (await response.json()) as ErrorDetails
97+
throw new Error(err.message)
9898
}
9999

100100
const data = (await response.json()) as SummaryDataType | AdministrationDataType
101101

102102
setSummaryData(data as SummaryDataType)
103103
setAdministrationData(data as AdministrationDataType)
104-
} catch (e) {
105-
console.error(e)
104+
} catch (error) {
105+
if (error instanceof DOMException && error.name === 'AbortError') {
106+
return
107+
}
108+
const message = error instanceof Error ? error.message : String(error)
109+
MessageService.error(message)
106110
}
107111
})()
108112

109113
return () => controller.abort()
110114
}, [
111115
projectId,
112116
session,
113-
status,
117+
session,
114118
])
115119

116120
useEffect(() => {
121+
if (session.status === 'loading') return
117122
const controller = new AbortController()
118123
const signal = controller.signal
119124
void (async () => {
120125
try {
121-
const session = await getSession()
122-
if (CommonUtils.isNullOrUndefined(session)) return signOut()
126+
if (CommonUtils.isNullOrUndefined(session.data)) return signOut()
123127
const response = await ApiUtils.GET(
124128
`projects/${projectId}/clearingDetailsCount`,
125-
session.user.access_token,
129+
session.data.user.access_token,
126130
signal,
127131
)
128-
if (response.status === StatusCodes.UNAUTHORIZED) {
129-
return signOut()
130-
} else if (response.status !== StatusCodes.OK) {
131-
return notFound()
132+
if (response.status !== StatusCodes.OK) {
133+
const err = (await response.json()) as ErrorDetails
134+
throw new Error(err.message)
132135
}
133136
const data = (await response.json()) as ClearingDetailsCount
134137
setClearingDetailCount(data)
@@ -147,8 +150,8 @@ export default function ViewProjects({ projectId }: { projectId: string }): JSX.
147150
])
148151

149152
const handleEditProject = (projectId: string) => {
150-
if (CommonUtils.isNullOrUndefined(session)) return signOut()
151-
if (session.user.email === summaryData?.['_embedded']?.['createdBy']?.['email']) {
153+
if (CommonUtils.isNullOrUndefined(session.data)) return signOut()
154+
if (session?.data.user.email === summaryData?.['_embedded']?.['createdBy']?.['email']) {
152155
MessageService.success(t('You are editing the original document'))
153156
router.push(`/projects/edit/${projectId}?tab=${activeKey}`)
154157
} else {
@@ -205,8 +208,8 @@ export default function ViewProjects({ projectId }: { projectId: string }): JSX.
205208
action
206209
eventKey='licenseClearing'
207210
hidden={
208-
status === 'authenticated' &&
209-
session?.user?.userGroup === UserGroupType.SECURITY_USER
211+
session.status === 'authenticated' &&
212+
session?.data.user?.userGroup === UserGroupType.SECURITY_USER
210213
}
211214
>
212215
<div className='my-2'>{t('License Clearing')}</div>
@@ -221,8 +224,8 @@ export default function ViewProjects({ projectId }: { projectId: string }): JSX.
221224
action
222225
eventKey='obligations'
223226
hidden={
224-
status === 'authenticated' &&
225-
session?.user?.userGroup === UserGroupType.SECURITY_USER
227+
session.status === 'authenticated' &&
228+
session?.data.user?.userGroup === UserGroupType.SECURITY_USER
226229
}
227230
>
228231
<div className='my-2'>{t('Obligations')}</div>
@@ -231,8 +234,8 @@ export default function ViewProjects({ projectId }: { projectId: string }): JSX.
231234
action
232235
eventKey='ecc'
233236
hidden={
234-
status === 'authenticated' &&
235-
session?.user?.userGroup === UserGroupType.SECURITY_USER
237+
session.status === 'authenticated' &&
238+
session?.data.user?.userGroup === UserGroupType.SECURITY_USER
236239
}
237240
>
238241
<div className='my-2'>{t('ECC')}</div>
@@ -247,8 +250,8 @@ export default function ViewProjects({ projectId }: { projectId: string }): JSX.
247250
action
248251
eventKey='attachments'
249252
hidden={
250-
status === 'authenticated' &&
251-
session?.user?.userGroup === UserGroupType.SECURITY_USER
253+
session.status === 'authenticated' &&
254+
session?.data.user?.userGroup === UserGroupType.SECURITY_USER
252255
}
253256
>
254257
<div className='my-2'>{t('Attachments')}</div>
@@ -257,8 +260,8 @@ export default function ViewProjects({ projectId }: { projectId: string }): JSX.
257260
action
258261
eventKey='attachmentUsages'
259262
hidden={
260-
status === 'authenticated' &&
261-
session?.user?.userGroup === UserGroupType.SECURITY_USER
263+
session.status === 'authenticated' &&
264+
session?.data.user?.userGroup === UserGroupType.SECURITY_USER
262265
}
263266
>
264267
<div className='my-2'>{t('Attachment Usages')}</div>
@@ -273,8 +276,8 @@ export default function ViewProjects({ projectId }: { projectId: string }): JSX.
273276
action
274277
eventKey='changeLog'
275278
hidden={
276-
status === 'authenticated' &&
277-
session?.user?.userGroup === UserGroupType.SECURITY_USER
279+
session.status === 'authenticated' &&
280+
session?.data.user?.userGroup === UserGroupType.SECURITY_USER
278281
}
279282
>
280283
<div className='my-2'>{t('Change Log')}</div>
@@ -290,8 +293,8 @@ export default function ViewProjects({ projectId }: { projectId: string }): JSX.
290293
className='me-2 col-auto'
291294
onClick={() => handleEditProject(projectId)}
292295
disabled={
293-
status === 'authenticated' &&
294-
session?.user?.userGroup === UserGroupType.SECURITY_USER
296+
session.status === 'authenticated' &&
297+
session?.data.user?.userGroup === UserGroupType.SECURITY_USER
295298
}
296299
>
297300
{t('Edit Projects')}
@@ -301,8 +304,8 @@ export default function ViewProjects({ projectId }: { projectId: string }): JSX.
301304
className='col-auto'
302305
onClick={() => setShow(true)}
303306
disabled={
304-
status === 'authenticated' &&
305-
session?.user?.userGroup === UserGroupType.SECURITY_USER
307+
session.status === 'authenticated' &&
308+
session?.data.user?.userGroup === UserGroupType.SECURITY_USER
306309
}
307310
>
308311
{t('Link to Projects')}
@@ -313,8 +316,8 @@ export default function ViewProjects({ projectId }: { projectId: string }): JSX.
313316
id='exportSBOM'
314317
className='px-2'
315318
hidden={
316-
status === 'authenticated' &&
317-
session?.user?.userGroup === UserGroupType.SECURITY_USER
319+
session.status === 'authenticated' &&
320+
session?.data.user?.userGroup === UserGroupType.SECURITY_USER
318321
}
319322
>
320323
{t('Import SBOM')}
@@ -378,8 +381,8 @@ export default function ViewProjects({ projectId }: { projectId: string }): JSX.
378381
id='exportSBOM'
379382
className='px-2'
380383
hidden={
381-
status === 'authenticated' &&
382-
session?.user?.userGroup === UserGroupType.SECURITY_USER
384+
session.status === 'authenticated' &&
385+
session?.data.user?.userGroup === UserGroupType.SECURITY_USER
383386
}
384387
>
385388
{t('Export SBOM')}

src/components/Attachments/Attachments.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ function Attachments({ documentId, documentType }: { documentId: string; documen
283283

284284
const data = (await response.json()) as EmbeddedAttachments
285285
setAttachmentData(
286-
CommonUtils.isNullOrUndefined(data['_embedded']['sw360:attachments'])
286+
CommonUtils.isNullOrUndefined(data['_embedded']?.['sw360:attachments'])
287287
? []
288288
: data['_embedded']['sw360:attachments'].map(
289289
(att) =>

src/components/ResourcesUsing/ResourcesUsing.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ const ResourcesUsing = ({ documentId, documentType, documentName }: Props): JSX.
5252
session.data.user.access_token,
5353
signal,
5454
)
55+
if (response.status === StatusCodes.NO_CONTENT) {
56+
return
57+
}
5558
if (response.status !== StatusCodes.OK) {
5659
const err = (await response.json()) as ErrorDetails
5760
throw new Error(err.message)

0 commit comments

Comments
 (0)