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

Feat(aas list) pagination #15

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 59 additions & 58 deletions src/app/[locale]/list/_components/AasList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
Typography,
useTheme,
} from '@mui/material';
import { FormattedMessage, useIntl } from 'react-intl';
import { FormattedMessage } from 'react-intl';
import { messages } from 'lib/i18n/localization';
import CompareArrowsIcon from '@mui/icons-material/CompareArrows';
import { AasListTableRow } from 'app/[locale]/list/_components/AasListTableRow';
import { AasListDto } from 'lib/services/list-service/ListService';
import { useTranslations } from 'next-intl';

type AasListProps = {
shells: AasListDto | undefined;
Expand All @@ -25,20 +26,17 @@ type AasListProps = {
export default function AasList(props: AasListProps) {
const { shells, selectedAasList, updateSelectedAasList, comparisonFeatureFlag } = props;
const theme = useTheme();
const intl = useIntl();
const t = useTranslations('aas-list');
const MAX_SELECTED_ITEMS = 3;

const tableHeaders = [
{ label: intl.formatMessage(messages.mnestix.aasList.picture) },
{ label: intl.formatMessage(messages.mnestix.aasList.manufacturerHeading) },
{ label: intl.formatMessage(messages.mnestix.aasList.productDesignationHeading) },
{ label: t('picture') },
{ label: t('manufacturerHeading') },
{ label: t('productDesignationHeading') },
{
label:
intl.formatMessage(messages.mnestix.aasList.assetIdHeading) +
' / ' +
intl.formatMessage(messages.mnestix.aasList.aasIdHeading),
label: t('assetIdHeading') + ' / ' + t('aasIdHeading'),
},
{ label: intl.formatMessage(messages.mnestix.aasList.productClassHeading) },
{ label: t('productClassHeading') },
];

/**
Expand All @@ -50,59 +48,62 @@ export default function AasList(props: AasListProps) {
};

return (
<>
<TableContainer>
<Table>
<TableHead>
<TableRow
sx={{
color: 'primary',
lineHeight: '150%',
letterSpacing: '0.16px',
fontSize: '16px',
}}
>
{comparisonFeatureFlag && (
<TableCell align="center" width="50px">
<Tooltip
title={<FormattedMessage {...messages.mnestix.aasList.compareTooltip} />}
arrow
>
<CompareArrowsIcon
sx={{ width: '35px', height: '35px', verticalAlign: 'middle' }}
/>
</Tooltip>
</TableCell>
)}
{!!tableHeaders &&
tableHeaders.map((header: { label: string }, index) => (
<TableCell key={index}>
<Typography fontWeight="bold">{header.label}</Typography>
</TableCell>
))}
</TableRow>
</TableHead>
<TableBody>
{shells && shells.entities?.map((aasListEntry) => (
<Table>
<TableHead>
<TableRow
key={aasListEntry.aasId}
sx={{
'&:last-child td, &:last-child th': { border: 0 },
backgroundColor: theme.palette?.common?.white,
'&:hover': { backgroundColor: theme.palette.action.hover },
color: 'primary',
lineHeight: '150%',
letterSpacing: '0.16px',
fontSize: '16px',
}}
data-testid={`list-row-${aasListEntry.aasId}`}
>
<AasListTableRow
aasListEntry={aasListEntry}
comparisonFeatureFlag={comparisonFeatureFlag}
checkBoxDisabled={checkBoxDisabled}
selectedAasList={selectedAasList}
updateSelectedAasList={updateSelectedAasList}
/>
{comparisonFeatureFlag && (
<TableCell align="center" width="50px">
<Tooltip
title={<FormattedMessage {...messages.mnestix.aasList.compareTooltip} />}
arrow
>
<CompareArrowsIcon
sx={{ width: '35px', height: '35px', verticalAlign: 'middle' }}
/>
</Tooltip>
</TableCell>
)}
{!!tableHeaders &&
tableHeaders.map((header: { label: string }, index) => (
<TableCell key={index}>
<Typography fontWeight="bold">{header.label}</Typography>
</TableCell>
))}
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
</TableHead>
<TableBody>
{shells &&
shells.entities?.map((aasListEntry) => (
<TableRow
key={aasListEntry.aasId}
sx={{
'&:last-child td, &:last-child th': { border: 0 },
backgroundColor: theme.palette?.common?.white,
'&:hover': { backgroundColor: theme.palette.action.hover },
}}
data-testid={`list-row-${aasListEntry.aasId}`}
>
<AasListTableRow
aasListEntry={aasListEntry}
comparisonFeatureFlag={comparisonFeatureFlag}
checkBoxDisabled={checkBoxDisabled}
selectedAasList={selectedAasList}
updateSelectedAasList={updateSelectedAasList}
/>
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
</>
);
}
102 changes: 79 additions & 23 deletions src/app/[locale]/list/_components/AasListDataWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,71 @@ import AasList from './AasList';
import { useEnv } from 'app/env/provider';
import { SelectProductType } from './filter/SelectProductType';
import { AasListComparisonHeader } from './AasListComparisonHeader';
import { Box } from '@mui/material';
import { Box, IconButton, Typography } from '@mui/material';
import ArrowBackIosNewIcon from '@mui/icons-material/ArrowBackIosNew';
import ArrowForwardIosIcon from '@mui/icons-material/ArrowForwardIos';
import { SelectRepository } from './filter/SelectRepository';
import { useTranslations } from 'next-intl';

export default function AasListDataWrapper() {
const [isLoadingList, setIsLoadingList] = useState(false);
const [aasList, setAasList] = useState<AasListDto>();
const [, setAasListFiltered] = useState<ListEntityDto[]>();
const [selectedAasList, setSelectedAasList] = useState<string[]>();
const env = useEnv();
const notificationSpawner = useNotificationSpawner();
const [selectedRepository, setSelectedRepository] = useState<string | undefined>();
const env = useEnv();
const t = useTranslations('aas-list');

//Pagination
const [currentCursor, setCurrentCursor] = useState<string>();
const [cursorHistory, setCursorHistory] = useState<(string | undefined)[]>([]);
const [currentPage, setCurrentPage] = useState(0);

useAsyncEffect(async () => {
resetPagination();
await fetchListData();
}, [selectedRepository]);

const fetchListData = async (newCursor?: string | undefined, isNext = true) => {
if (!selectedRepository) return;

try {
setIsLoadingList(true);
const list = await getAasListEntities(selectedRepository!, 10);
setAasList(list);
setAasListFiltered(list.entities);
} catch (e) {
showError(e, notificationSpawner);
} finally {
setIsLoadingList(false);
setIsLoadingList(true);
const response = await getAasListEntities(selectedRepository!, 10, newCursor);

if (response.success) {
setAasList(response);
setAasListFiltered(response.entities);
setCurrentCursor(response.cursor);

if (isNext) {
setCursorHistory((prevHistory) => [...prevHistory, newCursor]);
}
} else {
showError(response.error, notificationSpawner);
}
}, [selectedRepository]);
setIsLoadingList(false);
};

const handleNextPage = async () => {
await fetchListData(currentCursor, true);
setCurrentPage((prevPage) => prevPage + 1);
};

/**
* Handle a click on the back button.
* To load the page one step back, we need to use the cursor from two pages back.
*/
const handleGoBack = async () => {
const previousCursor = cursorHistory[currentPage - 2] ?? undefined;
await fetchListData(previousCursor, false);
setCurrentPage((prevPage) => prevPage - 1);
};

const resetPagination = () => {
setCursorHistory([]);
setCurrentPage(0);
};

/**
* Update the list of currently selected aas
Expand All @@ -60,10 +98,14 @@ export default function AasListDataWrapper() {

return (
<>
<Box display="flex" justifyContent="space-between">
<Box display="flex" gap={4} marginBottom={2}>
<SelectRepository onSelectedRepositoryChanged={setSelectedRepository}/>
<SelectProductType aasList={aasList?.entities} setAasListFiltered={setAasListFiltered}/>
<Typography marginBottom={2} marginTop={-1}>
{t('subHeader')}
</Typography>

<Box display="flex" justifyContent="space-between" marginBottom={2}>
<Box display="flex" gap={4}>
<SelectRepository onSelectedRepositoryChanged={setSelectedRepository} />
<SelectProductType aasList={aasList?.entities} setAasListFiltered={setAasListFiltered} />
</Box>
{env.COMPARISON_FEATURE_FLAG && (
<AasListComparisonHeader
Expand All @@ -72,13 +114,27 @@ export default function AasListDataWrapper() {
/>
)}
</Box>
{isLoadingList ? <CenteredLoadingSpinner sx={{ mt: 10 }}/> :
<AasList
shells={aasList}
selectedAasList={selectedAasList}
updateSelectedAasList={updateSelectedAasList}
comparisonFeatureFlag={env.COMPARISON_FEATURE_FLAG}>
</AasList>}
{isLoadingList ? (
<CenteredLoadingSpinner sx={{ mt: 10 }} />
) : (
<>
<AasList
shells={aasList}
selectedAasList={selectedAasList}
updateSelectedAasList={updateSelectedAasList}
comparisonFeatureFlag={env.COMPARISON_FEATURE_FLAG}
></AasList>
<Box display="flex" justifyContent="flex-end" alignItems="center" gap={4} marginTop={2}>
<IconButton onClick={handleGoBack} disabled={currentPage === 0}>
<ArrowBackIosNewIcon />
</IconButton>
<Typography>{t('page') + ' ' + (currentPage + 1)}</Typography>
<IconButton onClick={handleNextPage} disabled={!currentCursor}>
<ArrowForwardIosIcon />
</IconButton>
</Box>
</>
)}
</>
);
}
9 changes: 6 additions & 3 deletions src/app/[locale]/list/_components/AasListHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ export default async function AasListHeader() {
const t = await getTranslations('aas-list');

return (
<Typography variant="h2" textAlign="left" marginBottom={2}>
{t('header')}
</Typography>
<>
<Typography variant="h2" textAlign="left" marginBottom={2}>
{t('header')}dsafdsffdas
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dsafdsffdas 😆

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ups, will remove this in the test branch 😄

</Typography>
<Typography variant="h5">{t('subHeader')}</Typography>
</>
);
}
19 changes: 11 additions & 8 deletions src/app/[locale]/list/_components/AasListTableRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { useState } from 'react';
import { mapFileDtoToBlob } from 'lib/util/apiResponseWrapper/apiResponseWrapper';
import { useEnv } from 'app/env/provider';
import { ListEntityDto } from 'lib/services/list-service/ListService';
import { useTranslations } from 'next-intl';

type AasTableRowProps = {
aasListEntry: ListEntityDto;
Expand All @@ -39,17 +40,19 @@ export const AasListTableRow = (props: AasTableRowProps) => {
const [, setAasOriginUrl] = useAasOriginSourceState();
const notificationSpawner = useNotificationSpawner();
const [thumbnailUrl, setThumbnailUrl] = useState<string>('');
const env = useEnv();
const t = useTranslations('aas-list');

const navigateToAas = (listEntry: ListEntityDto) => {
setAas(null);
setAasOriginUrl(null);
if (listEntry.aasId) navigate.push(`/viewer/${encodeBase64(listEntry.aasId)}`);
};
const env = useEnv();

/* const translateListText = (property: { [key: string]: string } | undefined) => {
if (!property) return '';
return property[intl.locale] ?? Object.values(property)[0] ?? '';
};*/
/* const translateListText = (property: { [key: string]: string } | undefined) => {
if (!property) return '';
return property[intl.locale] ?? Object.values(property)[0] ?? '';
};*/

useAsyncEffect(async () => {
if (isValidUrl(aasListEntry.thumbnail ?? '')) {
Expand Down Expand Up @@ -106,16 +109,16 @@ export const AasListTableRow = (props: AasTableRowProps) => {
</TableCell>
<TableCell align="left" sx={tableBodyText}>
<Typography fontWeight="bold" sx={{ letterSpacing: '0.16px' }}>
<FormattedMessage {...messages.mnestix.aasList.assetIdHeading} />
{t('assetIdHeading')}
</Typography>
{tooltipText(aasListEntry.assetId, 80)} <br />
<Typography fontWeight="bold" sx={{ letterSpacing: '0.16px' }}>
<FormattedMessage {...messages.mnestix.aasList.aasIdHeading} />
{t('aasIdHeading')}
</Typography>
{tooltipText(aasListEntry.aasId, 80)}
</TableCell>
<TableCell align="left">
{/* {aasListEntry.productGroup ? (
{/* {aasListEntry.productGroup ? (
<ProductClassChip productClassId={getProductClassId(aasListEntry.productGroup)} maxChars={25} />
) : (
<Chip
Expand Down
Loading
Loading