Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(service marketplace): list all active services #1134

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,7 @@
border-radius: 16px;
z-index: 9;
}
.load-more-btn {
background: #f9f9f9;
}
}
195 changes: 127 additions & 68 deletions src/components/pages/ServiceMarketplace/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,24 @@ import {
SortOption,
CircleProgress,
ErrorBar,
LoadMoreButton,
} from '@catena-x/portal-shared-components'
import {
type ServiceRequest,
type ServiceRequestAPIResponse,
useFetchServicesQuery,
} from 'features/serviceMarketplace/serviceApiSlice'
import SortImage from 'components/shared/frame/SortImage'
import { ServiceTypeIdsEnum } from 'features/serviceManagement/apiSlice'
import NoItems from '../NoItems'
import { SORTING_TYPE } from 'features/serviceManagement/types'

dayjs.extend(isToday)
dayjs.extend(isYesterday)
dayjs.extend(relativeTime)

const indexToSplit = 2 //show only 2 services in recommended

export default function ServiceMarketplace() {
const { t } = useTranslation()
const theme = useTheme()
Expand All @@ -63,39 +68,61 @@ export default function ServiceMarketplace() {
const [selected, setSelected] = useState<string>('All Services')
const [sortOption, setSortOption] = useState<string>('new')
const [cardServices, setCardServices] = useState<ServiceRequest[]>([])

let serviceTypeId = ''

if (selected === ServiceTypeIdsEnum.DATASPACE_SERVICES) {
serviceTypeId = ServiceTypeIdsEnum.DATASPACE_SERVICE
} else if (selected === ServiceTypeIdsEnum.CONSULTANCY_SERVICES) {
serviceTypeId = ServiceTypeIdsEnum.CONSULTANCY_SERVICE
}

let sortingType = 'ReleaseDateDesc'
if (sortOption === 'provider') {
sortingType = 'ProviderDesc'
}

const indexToSplit = 2 //show only 2 services in recommended

const { data, error, isError, refetch } = useFetchServicesQuery({
const [page, setPage] = useState<number>(0)
const [serviceTypeId, setServiceTypeId] = useState<ServiceTypeIdsEnum>()
const [sortingType, setSortingType] = useState<SORTING_TYPE>(
SORTING_TYPE.RELEASE_DATE_DESC
)
const [argsData, setArgsData] = useState<{
page: number
serviceType?: ServiceTypeIdsEnum
sortingType: SORTING_TYPE
}>({
page: 0,
serviceType: serviceTypeId,
sortingType,
})

const { data, error, isError, refetch, isFetching } =
useFetchServicesQuery(argsData)
const services = data?.content

// To-Do fix the type issue with status and data from FetchBaseQueryError
// eslint-disable-next-line
const servicesError = error as any

useEffect(() => {
services && setCardServices(services)
}, [services])
if (services)
setCardServices(
data?.meta.page === 0
? setDataInfo(data)
: (serviceRequets: ServiceRequest[]) =>
serviceRequets.concat(setDataInfo(data))
)
}, [data])

const setDataInfo = (data: ServiceRequestAPIResponse) =>
data.content.map((item) => ({ ...item }))

const setView = (e: React.MouseEvent<HTMLInputElement>) => {
setSelected(e.currentTarget.value)
const viewValue = e.currentTarget.value
let serviceType: ServiceTypeIdsEnum | undefined

if (viewValue === ServiceTypeIdsEnum.DATASPACE_SERVICES) {
serviceType = ServiceTypeIdsEnum.DATASPACE_SERVICE
} else if (viewValue === ServiceTypeIdsEnum.CONSULTANCY_SERVICES) {
serviceType = ServiceTypeIdsEnum.CONSULTANCY_SERVICE
}

setServiceTypeId(serviceType)
setArgsData((arg) => ({
...arg,
serviceType,
page: 0,
}))

setSelected(viewValue)
setPage(0)
}

const sortOptions = [
Expand Down Expand Up @@ -156,6 +183,9 @@ export default function ServiceMarketplace() {
)

const setSortOptionFn = useCallback((value: string) => {
if (value === 'provider') {
setSortingType(SORTING_TYPE.PROVIDER_DESC)
}
setSortOption(value)
setShowModal(false)
}, [])
Expand All @@ -168,6 +198,14 @@ export default function ServiceMarketplace() {
setShowModal(true)
}, [])

const nextPage = () => {
setArgsData({
...argsData,
page: page + 1,
})
setPage(page + 1)
}

const renderServices = () => {
if (services && services.length === 0) return <NoItems />
if (!services)
Expand All @@ -190,58 +228,79 @@ export default function ServiceMarketplace() {

return (
<main className="serviceMarketplace">
<div className="mainContainer">
<div className="mainRow">
<Typography className="newServicesTitle" variant="h2">
{t('content.serviceMarketplace.newServices')}
</Typography>
<Typography className="recommendationsTitle" variant="body1">
{t('content.serviceMarketplace.recommendations')}
</Typography>
<div>
<div className="searchContainer">
<SearchInput
placeholder={t('notification.search')}
value={searchExpr}
autoFocus={false}
onChange={doFilter}
/>
</div>
<div className="filterSection" onMouseLeave={setModalFalse}>
<ViewSelector activeView={selected} views={filterButtons} />
<SortImage onClick={setModalTrue} selected={showModal} />
<div className="sortSection">
<SortOption
show={showModal}
selectedOption={sortOption}
setSortOption={setSortOptionFn}
sortOptions={sortOptions}
/>
{isFetching ? (
<div style={{ textAlign: 'center' }}>
<CircleProgress
variant="indeterminate"
colorVariant="primary"
size={50}
sx={{
color: theme.palette.primary.main,
}}
/>
</div>
) : (
<>
<div className="mainContainer">
<div className="mainRow">
<Typography className="newServicesTitle" variant="h2">
{t('content.serviceMarketplace.newServices')}
</Typography>
<Typography className="recommendationsTitle" variant="body1">
{t('content.serviceMarketplace.recommendations')}
</Typography>
<div>
<div className="searchContainer">
<SearchInput
placeholder={t('notification.search')}
value={searchExpr}
autoFocus={false}
onChange={doFilter}
/>
</div>
<div className="filterSection" onMouseLeave={setModalFalse}>
<ViewSelector activeView={selected} views={filterButtons} />
<SortImage onClick={setModalTrue} selected={showModal} />
<div className="sortSection">
<SortOption
show={showModal}
selectedOption={sortOption}
setSortOption={setSortOptionFn}
sortOptions={sortOptions}
/>
</div>
</div>
{!isError ? (
renderServices()
) : (
<ErrorBar
errorText={
servicesError?.data?.status >= 400 &&
servicesError?.data?.status < 500
? t('content.serviceMarketplace.dataLoadFailed')
: t('content.serviceMarketplace.loadFailed')
}
showButton={
servicesError.code >= 500 &&
servicesError?.data?.status < 600
}
buttonText={t('error.tryAgain')}
handleButton={refetch}
/>
)}
</div>
</div>
{!isError ? (
renderServices()
) : (
<ErrorBar
errorText={
servicesError?.data?.status >= 400 &&
servicesError?.data?.status < 500
? t('content.serviceMarketplace.dataLoadFailed')
: t('content.serviceMarketplace.loadFailed')
}
showButton={
servicesError.code >= 500 && servicesError?.data?.status < 600
}
buttonText={t('error.tryAgain')}
handleButton={refetch}
/>
)}
</div>
</div>
</div>
{cardServices && cardServices.length > 2 && (
<ServicesElements services={cardServices.slice(indexToSplit)} />
{cardServices && cardServices.length > 2 && (
<ServicesElements services={cardServices.slice(indexToSplit)} />
)}
</>
)}
<div className="load-more-btn">
{data?.meta && data?.meta?.totalPages > page + 1 && (
<LoadMoreButton onClick={nextPage} label={t('loadmore')} />
)}
</div>
</main>
)
}
5 changes: 5 additions & 0 deletions src/features/serviceManagement/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ export interface ServiceManagementState {
serviceStatusData: ServiceStatusDataState
}

export enum SORTING_TYPE {
RELEASE_DATE_DESC = 'ReleaseDateDesc',
PROVIDER_DESC = 'ProviderDesc',
}

export const initialState: ServiceManagementState = {
serviceReleaseActiveStep: 1,
serviceId: '',
Expand Down
6 changes: 3 additions & 3 deletions src/features/serviceMarketplace/serviceApiSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export type SubscriptionServiceRequest = {

export type ServiceBody = {
page: number
serviceType: string
serviceType?: string
sortingType: string
}

Expand All @@ -110,8 +110,8 @@ export const apiSlice = createApi({
const serviceType = `serviceTypeId=${body.serviceType}`
const sortingType = `sorting=${body.sortingType}`
return {
url: `/api/services/active?size${PAGE_SIZE}&page=${body.page}&${
body.serviceType && serviceType
url: `/api/services/active?size=${PAGE_SIZE}&page=${body.page}&${
(body.serviceType && serviceType) ?? ''
}&${body.sortingType && sortingType}`,
}
},
Expand Down
Loading