diff --git a/src/app/[locale]/home/components/MySubscriptionsWidget.tsx b/src/app/[locale]/home/components/MySubscriptionsWidget.tsx index d2f79bda5..830883fde 100644 --- a/src/app/[locale]/home/components/MySubscriptionsWidget.tsx +++ b/src/app/[locale]/home/components/MySubscriptionsWidget.tsx @@ -10,60 +10,158 @@ 'use-client' -import React, { useEffect, useState } from 'react' +import React, { useCallback, useEffect, useState } from 'react' import { useTranslations } from 'next-intl' import HomeTableHeader from './HomeTableHeader' +import { signOut, useSession } from 'next-auth/react' +import { ApiUtils, CommonUtils } from '@/utils/index' +import { HttpStatus, Component, Embedded, ReleaseDetail } from '@/object-types' +import { notFound } from 'next/navigation' +import Link from 'next/link' +import { Spinner } from 'react-bootstrap' + +type EmbeddedComponent = Embedded +type EmbeddedRelease = Embedded +interface MyEmptyComponentSubscription { + _embedded: { + 'sw360:components': [] + } +} +interface MyEmptyReleaseSubscription { + _embedded: { + 'sw360:releases': [] + } +} + function MySubscriptionsWidget() { - const [componentData] = useState([]) - const [releaseData] = useState([]) + const [componentData, setComponentData] = useState([]) + const [releaseData, setReleaseData] = useState([]) const t = useTranslations('default') + const [loading, setLoading] = useState(true) + const { data: session, status } = useSession() - useEffect(() => { - // const fetchData = async () => { - // const componentData = await sw360FetchData('/components/mySubscriptions', 'components') - // setComponentData(componentData.map((item: { name: string }) => [item.name])) - // const releaseData = await sw360FetchData('/releases/mySubscriptions', 'releases') - // setReleaseData(releaseData.map((item: { name: string }) => [item.name])) - // } - // fetchData() - }, []) + const fetchData = useCallback( + async (url: string) => { + const response = await ApiUtils.GET(url, session.user.access_token) + if (response.status == HttpStatus.OK) { + const data = await response.json() + return data + } else if (response.status == HttpStatus.NO_CONTENT) { + if (url.includes('component')){ + const interimData: MyEmptyComponentSubscription = { + '_embedded': { + 'sw360:components': [] + } + } + return interimData + } + else if (url.includes('release')){ + const interimData: MyEmptyReleaseSubscription = { + '_embedded': { + 'sw360:releases': [] + } + } + return interimData + } - return ( -
- - {componentData.length > 0 && ( - <> -

{t('Components')}

-
    - {componentData.map((item) => ( -
  • - {item} -
  • - ))} -
- - )} - {releaseData.length > 0 && ( - <> -

{t('Releases')}

-
    - {releaseData.map((item) => ( -
  • - {item} -
  • - ))} -
- - )} - {releaseData.length === 0 && componentData.length === 0 && ( - <> -
{t('No subscriptions available')}
- - )} -
+ } else if (response.status == HttpStatus.UNAUTHORIZED) { + return signOut() + } else { + notFound() + } + },[session] ) + + useEffect(() => { + setLoading(true) + fetchData('components/mySubscriptions').then((components: EmbeddedComponent) => { + if ( + !CommonUtils.isNullOrUndefined(components['_embedded']) && + !CommonUtils.isNullOrUndefined(components['_embedded']['sw360:components']) + ) { + setComponentData(components['_embedded']['sw360:components']) + } else { + setComponentData([]) + } + }) + fetchData('releases/mySubscriptions').then((releases: EmbeddedRelease) => { + if ( + !CommonUtils.isNullOrUndefined(releases['_embedded']) && + !CommonUtils.isNullOrUndefined(releases['_embedded']['sw360:releases']) + ) { + setReleaseData(releases['_embedded']['sw360:releases']) + } else { + setReleaseData([]) + } + }) + setLoading(false) + }, [fetchData, session]) + + if (status === 'unauthenticated') { + signOut() + } else { + return ( +
+ + { loading === false ? ( + <> + { componentData.length > 0 && ( + <> +

+ {t('Components')} +

+
    + {componentData.map((item:Component) => ( +
  • + + {item.name} + +
  • + ))} +
+ + )} + { releaseData.length > 0 && ( + <> +

+ {t('Releases')} +

+
    + {releaseData.map((item:ReleaseDetail) => ( +
  • + + {item.name} + +
  • + ))} +
+ + )} + {releaseData.length === 0 && componentData.length === 0 && ( + <> +
+ {t('No subscriptions available')} +
+ + )} + + ) : ( +
+ +
+ ) + } +
+ ) + } } // We need use this to override typescript issue