Skip to content

Commit

Permalink
Merge pull request #44 from oceanprotocol/feature/issue-35-paginated-…
Browse files Browse the repository at this point in the history
…history

ui component for pagination ready
  • Loading branch information
EnzoVezzaro authored Sep 6, 2023
2 parents 02f99b3 + 128696c commit bde33cd
Show file tree
Hide file tree
Showing 6 changed files with 284 additions and 75 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@oceanprotocol/dbs-ui-lib",
"version": "0.0.2",
"version": "0.1.0",
"description": "Ocean Protocol DBS component library",
"repository": {
"type": "git",
Expand All @@ -23,7 +23,7 @@
"@babel/preset-react": "^7.18.6",
"@babel/preset-typescript": "^7.21.5",
"@ethersproject/units": "^5.7.0",
"@oceanprotocol/dbs": "^0.0.2-alpha.25",
"@oceanprotocol/dbs": "^0.0.2-alpha.27",
"@oceanprotocol/typographies": "^0.1.0",
"@rollup/plugin-commonjs": "^25.0.2",
"@rollup/plugin-json": "^6.0.0",
Expand Down
123 changes: 70 additions & 53 deletions src/components/HistoryList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,28 @@ import { addEllipsesToText } from '../../@utils/textFormat'
import Loader from '../Loader';
import { getStatusMessage } from '../../@utils/statusCode'
import { getLink } from '../../@utils/linkAsset'
import Pagination from './pagination';

const HistoryList = ({
items,
tabIndex,
uploads,
historyUnlocked,
getHistoryList,
historyLoading
historyLoading,
historyPage,
historyTotalPages,
changeHistoryPage
}: {
items: any
tabIndex: number
uploads: any
historyUnlocked: boolean
getHistoryList: any
historyLoading: boolean
historyPage: number
historyTotalPages: number
changeHistoryPage: any
}): ReactElement => {
const [files, setFiles] = React.useState<any>({})

Expand All @@ -28,59 +35,69 @@ const HistoryList = ({
}, [uploads])

return (
<div className={styles.historyListTable}>
<table className={styles.tableAssets} key={`table_${items[tabIndex].type}`}>
<thead>
<tr>
<th>Quote ID</th>
<th>Status Message</th>
<th>Status Code</th>
<th>DDO Link</th>
<th>{'Preview'}</th>
</tr>
</thead>
<tbody className={historyUnlocked ? styles.historyUnlocked : styles.historyLocked}>
{files.length > 0 && files.map((file: any, index: number) => (
<tr key={`table_uploads_${items[tabIndex].type}_${index}`}>
<td>{addEllipsesToText(file.quoteId, 15)}</td>
<td>{getStatusMessage(file.status, items[tabIndex].type)}</td>
<td>{file.status}</td>
<td>{file?.transactionHash || file?.cid}</td>
<td>
<Button
style="primary"
className={styles.tableButton}
size="small"
onClick={(e: React.SyntheticEvent) => {
e.preventDefault()
getLink(items[tabIndex].type, file?.transactionHash || file?.cid)
}}
disabled={file.status !== 400}
>
{file.status === 400 ? 'Open Asset' : 'Not available'}
</Button>
</td>
<>
<div className={styles.historyListTable}>
<table className={styles.tableAssets} key={`table_${items[tabIndex].type}`}>
<thead>
<tr>
<th>Quote ID</th>
<th>Status Message</th>
<th>Status Code</th>
<th>DDO Link</th>
<th>{'Preview'}</th>
</tr>
))}
</tbody>
{
!historyUnlocked &&
<Button
className={styles.unlockButton}
style="primary"
size="small"
onClick={(e: React.SyntheticEvent) => {
if (historyLoading) return
e.preventDefault()
getHistoryList()
}}
disabled={false}
>
{historyLoading ? <Loader /> : `Unlock`}
</Button>
}
</table>
</div>
</thead>
<tbody className={historyUnlocked ? styles.historyUnlocked : styles.historyLocked}>
{files.length > 0 && files.map((file: any, index: number) => (
<tr key={`table_uploads_${items[tabIndex].type}_${index}`}>
<td>{addEllipsesToText(file.quoteId, 15)}</td>
<td>{getStatusMessage(file.status, items[tabIndex].type)}</td>
<td>{file.status}</td>
<td>{file?.transactionHash || file?.cid}</td>
<td>
<Button
style="primary"
className={styles.tableButton}
size="small"
onClick={(e: React.SyntheticEvent) => {
e.preventDefault()
getLink(items[tabIndex].type, file?.transactionHash || file?.cid)
}}
disabled={file.status !== 400}
>
{file.status === 400 ? 'Open Asset' : 'Not available'}
</Button>
</td>
</tr>
))}
</tbody>
{
!historyUnlocked &&
<Button
className={styles.unlockButton}
style="primary"
size="small"
onClick={(e: React.SyntheticEvent) => {
if (historyLoading) return
e.preventDefault()
getHistoryList()
}}
disabled={false}
>
{historyLoading ? <Loader /> : `Unlock`}
</Button>
}
</table>
</div>
{
historyUnlocked && historyTotalPages > 1 &&
<Pagination
totalPages={historyTotalPages}
currentPage={historyPage}
onPageChange={(page: number) => { changeHistoryPage(page)}}
/>
}
</>
)
}

Expand Down
89 changes: 89 additions & 0 deletions src/components/HistoryList/pagination.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
.paginationContainer {
display: flex;
justify-content: center;
}

.pagination {
display: flex;
flex-wrap: wrap;
justify-content: center;
margin-top: var(--spacer);
margin-bottom: var(--spacer);
padding-left: 0;
font-size: var(--font-size-small);
}

.number {
font-weight: var(--font-weight-bold);
margin-left: -1px;
margin-top: -1px;
display: block;
cursor: pointer;
min-width: 3rem;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
color: var(--color-secondary);
}

li:first-child .number,
:global(li.selected):nth-child(2) .number {
border-top-left-radius: var(--border-radius);
border-bottom-left-radius: var(--border-radius);
}

li:last-child .number {
border-top-right-radius: var(--border-radius);
border-bottom-right-radius: var(--border-radius);
}

.number,
.number:hover,
.number:focus,
.number:active {
transform: none;
outline: 0;
}

.number:hover {
color: var(--brand-pink);
}

.current,
.prev,
.next,
.break {
composes: number;
}

.current {
cursor: default;
pointer-events: none;
}

.current > a,
.current:hover,
.current:focus,
.current:active {
color: var(--brand-pink) !important;
}

.next {
text-align: right;
}

.prevNextDisabled {
opacity: 0;
}

.arrow {
width: 1rem;
height: 1rem;
fill: currentColor;
}

.arrow.previous {
transform: rotate(180deg);
}

98 changes: 98 additions & 0 deletions src/components/HistoryList/pagination.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import React from 'react';
import styles from './pagination.module.css';

interface PaginationProps {
totalPages: number;
currentPage: number;
onPageChange: (page: number) => void;
}

const Pagination = ({ totalPages, currentPage, onPageChange }: PaginationProps) => {
// Define the number of pages to show at the beginning and end
const pagesToShow = 3;

// Calculate the start and end range of pages to display
let startPage = Math.max(1, currentPage - pagesToShow);
let endPage = Math.min(totalPages, currentPage + pagesToShow);

// If we have less than the required number of pages to show at the beginning,
// adjust the endPage to display enough pages.
if (currentPage <= pagesToShow) {
endPage = Math.min(currentPage + pagesToShow + (pagesToShow - currentPage), totalPages);
}

// If we have less than the required number of pages to show at the end,
// adjust the startPage to display enough pages.
if (currentPage >= totalPages - pagesToShow) {
startPage = Math.max(currentPage - pagesToShow - (pagesToShow - (totalPages - currentPage - 1)), 1);
}

// Generate the pageNumbers array based on the calculated startPage and endPage
const pageNumbers = [];
if (startPage > 1) {
pageNumbers.push(1);
if (startPage > 2) {
pageNumbers.push('...');
}
}
for (let i = startPage; i <= endPage; i++) {
pageNumbers.push(i);
}
if (endPage < totalPages) {
if (endPage < totalPages - 1) {
pageNumbers.push('...');
}
pageNumbers.push(totalPages);
}

const handlePageChange = (page: number) => {
if (page < 1 || page > totalPages) return;
onPageChange(page);
};

return (
<div className={styles.paginationContainer}>
<ul className={styles.pagination}>
<li
className={`${currentPage === 1 ? styles.prevNextDisabled : ''}`}
>
<a
className={`${styles.prev}`}
onClick={() => handlePageChange(currentPage - 1)}
>
Previous
</a>
</li>

{pageNumbers.map((page) => (
<li
key={page}
className={`${currentPage === page ? styles.current : ''}`}
>
<a
className={`${styles.number}`}
onClick={() => { if (typeof page === 'number') handlePageChange(page) }}
>
{page}
</a>
</li>
))}

<li
className={`${
currentPage === totalPages ? styles.prevNextDisabled : ''
} ${styles.next}`}
>
<a
className={`${styles.next}`}
onClick={() => handlePageChange(currentPage + 1)}
>
Next
</a>
</li>
</ul>
</div>
);
};

export default Pagination;
Loading

0 comments on commit bde33cd

Please sign in to comment.