Skip to content

Commit 18e890b

Browse files
amritkvheliocastro
authored andcommitted
fix(Changelog): Fixed changelog errors when API response status is OK
but empty body
1 parent d6bdeeb commit 18e890b

File tree

5 files changed

+63
-42
lines changed

5 files changed

+63
-42
lines changed

src/app/[locale]/admin/obligations/changelog/[id]/components/Changelog.tsx

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,18 @@ function ChangeLog({ obligationId }: { obligationId: string }): JSX.Element {
5656
const err = (await response.json()) as ErrorDetails
5757
throw new Error(err.message)
5858
}
59-
60-
const data = (await response.json()) as EmbeddedChangeLogs
61-
62-
setChangeLogList(
63-
CommonUtils.isNullOrUndefined(data['_embedded']['sw360:changeLogs'])
64-
? []
65-
: data['_embedded']['sw360:changeLogs'],
66-
)
59+
const responseText = await response.text()
60+
if (CommonUtils.isNullEmptyOrUndefinedString(responseText)) {
61+
setChangeLogList([])
62+
return
63+
} else {
64+
const data = JSON.parse(responseText) as EmbeddedChangeLogs
65+
setChangeLogList(
66+
CommonUtils.isNullOrUndefined(data['_embedded']['sw360:changeLogs'])
67+
? []
68+
: data['_embedded']['sw360:changeLogs'],
69+
)
70+
}
6771
} catch (error) {
6872
if (error instanceof DOMException && error.name === 'AbortError') {
6973
return

src/app/[locale]/components/detail/[id]/components/DetailOverview.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,11 @@ const DetailOverview = ({ componentId }: Props): ReactNode => {
112112

113113
fetchData(`changelog/document/${componentId}`)
114114
.then((changeLogs: EmbeddedChangelogs | undefined) => {
115-
if (changeLogs === undefined) return
116-
setChangeLogList(
117-
CommonUtils.isNullOrUndefined(changeLogs['_embedded']['sw360:changeLogs'])
118-
? []
119-
: changeLogs['_embedded']['sw360:changeLogs'],
120-
)
115+
if (changeLogs && CommonUtils.isNullOrUndefined(changeLogs._embedded['sw360:changeLogs'])) {
116+
setChangeLogList([])
117+
} else {
118+
setChangeLogList(changeLogs?._embedded?.['sw360:changeLogs'] || [])
119+
}
121120
})
122121
.catch((err) => console.error(err))
123122

src/app/[locale]/packages/detail/[id]/components/Changelog.tsx

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,16 @@
1111

1212
import ChangeLogDetail from '@/components/ChangeLog/ChangeLogDetail/ChangeLogDetail'
1313
import ChangeLogList from '@/components/ChangeLog/ChangeLogList/ChangeLogList'
14-
import { Changelogs, HttpStatus } from '@/object-types'
15-
import { ApiUtils } from '@/utils'
14+
import { Changelogs, Embedded, HttpStatus } from '@/object-types'
15+
import MessageService from '@/services/message.service'
16+
import { ApiUtils, CommonUtils } from '@/utils'
1617
import { signOut, useSession } from 'next-auth/react'
1718
import { useTranslations } from 'next-intl'
1819
import { notFound } from 'next/navigation'
1920
import { ReactNode, useEffect, useState } from 'react'
2021
import { Nav, Tab } from 'react-bootstrap'
2122

22-
interface PackageChangelogs {
23-
_embedded: {
24-
'sw360:changeLogs': Array<Changelogs>
25-
}
26-
}
23+
type EmbeddedChangeLogs = Embedded<Changelogs, 'sw360:changeLogs'>
2724

2825
function ChangeLog({ packageId }: { packageId: string }): ReactNode {
2926
const t = useTranslations('default')
@@ -56,12 +53,24 @@ function ChangeLog({ packageId }: { packageId: string }): ReactNode {
5653
} else if (response.status !== HttpStatus.OK) {
5754
return notFound()
5855
}
59-
60-
const data = (await response.json()) as PackageChangelogs
61-
62-
setChangeLogList(data['_embedded']['sw360:changeLogs'])
63-
} catch (e) {
64-
console.error(e)
56+
const responseText = await response.text()
57+
if (CommonUtils.isNullEmptyOrUndefinedString(responseText)) {
58+
setChangeLogList([])
59+
return
60+
} else {
61+
const data = JSON.parse(responseText) as EmbeddedChangeLogs
62+
setChangeLogList(
63+
CommonUtils.isNullOrUndefined(data['_embedded']['sw360:changeLogs'])
64+
? []
65+
: data['_embedded']['sw360:changeLogs'],
66+
)
67+
}
68+
} catch (error: unknown) {
69+
if (error instanceof DOMException && error.name === 'AbortError') {
70+
return
71+
}
72+
const message = error instanceof Error ? error.message : String(error)
73+
MessageService.error(message)
6574
}
6675
})()
6776

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

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { AccessControl } from '@/components/AccessControl/AccessControl'
1919
import ChangeLogDetail from '@/components/ChangeLog/ChangeLogDetail/ChangeLogDetail'
2020
import ChangeLogList from '@/components/ChangeLog/ChangeLogList/ChangeLogList'
2121
import { Changelogs, Embedded, HttpStatus, UserGroupType } from '@/object-types'
22+
import MessageService from '@/services/message.service'
2223
import { ApiUtils, CommonUtils } from '@/utils'
2324

2425
type EmbeddedChangeLogs = Embedded<Changelogs, 'sw360:changeLogs'>
@@ -60,16 +61,24 @@ function ChangeLog({
6061
} else if (response.status !== HttpStatus.OK) {
6162
return notFound()
6263
}
63-
64-
const data = (await response.json()) as EmbeddedChangeLogs
65-
66-
setChangeLogList(
67-
CommonUtils.isNullOrUndefined(data['_embedded']['sw360:changeLogs'])
68-
? []
69-
: data['_embedded']['sw360:changeLogs'],
70-
)
71-
} catch (e) {
72-
console.error(e)
64+
const responseText = await response.text()
65+
if (CommonUtils.isNullEmptyOrUndefinedString(responseText)) {
66+
setChangeLogList([])
67+
return
68+
} else {
69+
const data = JSON.parse(responseText) as EmbeddedChangeLogs
70+
setChangeLogList(
71+
CommonUtils.isNullOrUndefined(data['_embedded']['sw360:changeLogs'])
72+
? []
73+
: data['_embedded']['sw360:changeLogs'],
74+
)
75+
}
76+
} catch (error: unknown) {
77+
if (error instanceof DOMException && error.name === 'AbortError') {
78+
return
79+
}
80+
const message = error instanceof Error ? error.message : String(error)
81+
MessageService.error(message)
7382
}
7483
})()
7584

src/app/[locale]/requests/moderationRequest/[id]/components/currentComponent/CurrentComponentDetail.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,11 @@ const CurrentComponentDetail = ({ componentId }: Props): ReactNode => {
9595

9696
fetchData(`changelog/document/${componentId}`)
9797
.then((changeLogs: EmbeddedChangelogs | undefined) => {
98-
setChangeLogList(
99-
CommonUtils.isNullOrUndefined(changeLogs?._embedded['sw360:changeLogs'])
100-
? []
101-
: changeLogs._embedded['sw360:changeLogs'],
102-
)
98+
if (changeLogs && CommonUtils.isNullOrUndefined(changeLogs._embedded['sw360:changeLogs'])) {
99+
setChangeLogList([])
100+
} else {
101+
setChangeLogList(changeLogs?._embedded?.['sw360:changeLogs'] || [])
102+
}
103103
})
104104
.catch((err) => console.error(err))
105105
}, [componentId, fetchData])

0 commit comments

Comments
 (0)