Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Fi1osof
committed
Mar 5, 2021
1 parent
f236558
commit fc61e1b
Showing
14 changed files
with
274 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import React, { memo, useCallback } from 'react' | ||
|
||
import Link from 'next/link' | ||
|
||
import URI from 'urijs' | ||
import { PaginationProps } from './interfaces' | ||
import { useRouter } from 'next/router' | ||
import { PaginationStyled } from './styles' | ||
export * from './interfaces' | ||
|
||
const Pagination: React.FC<PaginationProps> = ({ | ||
page, | ||
limit, | ||
total, | ||
...other | ||
}) => { | ||
const router = useRouter() | ||
|
||
const getNewLocation = useCallback( | ||
(page: number) => { | ||
const asPath = router.asPath | ||
|
||
const uri = new URI(asPath) | ||
|
||
const query = uri.query(true) | ||
|
||
Object.assign(query, { | ||
page: page > 1 ? page : undefined, | ||
}) | ||
|
||
uri.query(query) | ||
|
||
return uri.resource() | ||
}, | ||
[router.asPath] | ||
) | ||
|
||
if (!page || !limit || !total) { | ||
return null | ||
} | ||
|
||
const pages = Math.ceil(total / limit) | ||
|
||
if (pages < 2) { | ||
return null | ||
} | ||
|
||
const rows = [] | ||
|
||
if (page > 1) { | ||
const href = getNewLocation(page - 1) | ||
|
||
rows.push( | ||
<li key="page-1-0" className={'control'}> | ||
<Link href={href}> | ||
<a className={'link'}>«</a> | ||
</Link> | ||
</li> | ||
) | ||
} | ||
|
||
let lstr = false | ||
let rstr = false | ||
for (let i = 1; i <= pages; i++) { | ||
if ( | ||
(page > 2 && i < page - 1 && i > 1) || | ||
(pages - page > 3 && i > page + 1 && i < pages - 1) | ||
) { | ||
if (!lstr && i > 1 && i < page) { | ||
rows.push( | ||
<li key={i} className={'control'}> | ||
<span>...</span> | ||
</li> | ||
) | ||
lstr = true | ||
} | ||
if (!rstr && i > page && i < pages) { | ||
rows.push( | ||
<li key={i} className={'control'}> | ||
<span>...</span> | ||
</li> | ||
) | ||
rstr = true | ||
} | ||
} else { | ||
const href = getNewLocation(i) | ||
|
||
rows.push( | ||
<li key={i} className={'control'}> | ||
<Link href={href}> | ||
<a className={['link', i === page ? 'active' : null].join(' ')}> | ||
{i} | ||
</a> | ||
</Link> | ||
</li> | ||
) | ||
} | ||
} | ||
if (page < pages) { | ||
const href = getNewLocation(page + 1) | ||
|
||
rows.push( | ||
<li key={'page-' + pages + '-0'} className={'control'}> | ||
<Link href={href}> | ||
<a className={'link'}>»</a> | ||
</Link> | ||
</li> | ||
) | ||
} | ||
|
||
return ( | ||
<PaginationStyled {...other}> | ||
<ul className={'row'}>{rows}</ul> | ||
</PaginationStyled> | ||
) | ||
} | ||
|
||
export default memo(Pagination) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export type PaginationProps = { | ||
page: number | ||
|
||
limit: number | ||
|
||
total: number | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import styled from 'styled-components' | ||
|
||
export const PaginationStyled = styled.nav` | ||
display: flex; | ||
.row { | ||
margin: 20px auto; | ||
display: flex; | ||
flex-direction: row; | ||
flex-wrap: nowrap; | ||
justify-content: center; | ||
list-style: none; | ||
padding: 0; | ||
border: 1px solid #ddd; | ||
border-radius: 2px; | ||
} | ||
.control { | ||
border-right: 1px solid #ddd; | ||
&:last-child { | ||
border-right: none; | ||
} | ||
> * { | ||
display: block; | ||
padding: 8px 12px; | ||
/* border: 1px solid #ddd; */ | ||
/* margin-left: 3px; | ||
margin-right: 3px; */ | ||
text-decoration: none; | ||
} | ||
} | ||
a { | ||
&.active { | ||
background: #ddd; | ||
} | ||
&:hover { | ||
background: #dfdfdf; | ||
} | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
import { PaginationProps } from 'src/components/ui/Pagination' | ||
import { UsersPageViewUserProps } from './User/interfaces' | ||
|
||
export interface UsersPageViewProps { | ||
export type UsersPageViewProps = { | ||
// data: UsersConnectionQuery | null | undefined | ||
|
||
users: UsersPageViewUserProps['user'][] | ||
|
||
pagination: PaginationProps | ||
} |
Oops, something went wrong.