-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
9 changed files
with
316 additions
and
282 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
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,62 @@ | ||
import React, { useEffect, useState } from 'react' | ||
import { tailwindWrapper } from "formula_one/src/utils/tailwindWrapper" | ||
import { ChevronDoubleLeft, ChevronDoubleRight, ChevronLeft, ChevronRight } from './icons' | ||
import { themeBg } from '../constants/theme' | ||
|
||
import {getTheme} from 'formula_one' | ||
|
||
const Pagination = ({ totalPages, activePage, onPageChange }) => { | ||
const pages = Array.from({ length: totalPages }, (_, index) => index + 1) | ||
const [visibleBtns, setVisibleBtns] = useState([]) | ||
const [maxPageLimit, setMaxPageLimit] = useState(5) | ||
const [minPageLimit, setMinPageLimit] = useState(1) | ||
const pageLimit = 5 | ||
const theme = getTheme() | ||
|
||
useEffect(() => { | ||
if (activePage > maxPageLimit) { | ||
setMaxPageLimit(maxPageLimit + pageLimit) | ||
setMinPageLimit(minPageLimit + pageLimit) | ||
} | ||
if (activePage < minPageLimit) { | ||
setMaxPageLimit(maxPageLimit - pageLimit) | ||
setMinPageLimit(minPageLimit - pageLimit) | ||
} | ||
|
||
const visiblePages = pages.filter( | ||
(page) => page >= minPageLimit && page <= maxPageLimit | ||
) | ||
|
||
setVisibleBtns(visiblePages) | ||
}, [activePage, totalPages, maxPageLimit, minPageLimit, pageLimit]) | ||
|
||
const dotsStyle = tailwindWrapper(`px-4 py-2 hover:bg-gray-200`) | ||
const btnStyle = (index) => tailwindWrapper(`${activePage === index ? 'opacity-50 pointer-events-none' : ''} px-4 py-2 hover:bg-gray-200`) | ||
|
||
return ( | ||
<div className={tailwindWrapper("flex items-stretch items-center border justify-start")}> | ||
|
||
<div onClick={() => onPageChange(1)} className={btnStyle(1)}><ChevronDoubleLeft /></div> | ||
<div onClick={() => onPageChange(activePage - 1)} className={btnStyle(1)}><ChevronLeft /></div> | ||
|
||
{minPageLimit !== 1 && | ||
<div onClick={() => onPageChange(Math.max(1 , minPageLimit - pageLimit))} className={dotsStyle}>...</div>} | ||
|
||
{totalPages > 0 | ||
? visibleBtns.map((page) => ( | ||
<div key={page} onClick={() => onPageChange(page)} className={tailwindWrapper(`px-4 py-2 ${page === activePage ? themeBg[theme] + " text-white" : 'hover:bg-gray-200'}`)}> | ||
{page} | ||
</div> | ||
)) | ||
: <div className={dotsStyle}>...</div>} | ||
|
||
{totalPages > maxPageLimit && | ||
<div onClick={() => onPageChange(Math.min(totalPages, maxPageLimit + pageLimit))} className={dotsStyle}>...</div>} | ||
|
||
<div onClick={() => onPageChange(activePage + 1)} className={btnStyle(totalPages)}><ChevronRight /></div> | ||
<div onClick={() => onPageChange(totalPages)} className={btnStyle(totalPages)}><ChevronDoubleRight /></div> | ||
</div> | ||
) | ||
} | ||
|
||
export default 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
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,117 +1,88 @@ | ||
import React, { Component } from 'react' | ||
import React, { useEffect, useRef, useState } from 'react' | ||
import { connect } from 'react-redux' | ||
import { Icon, Input, Pagination } from 'semantic-ui-react' | ||
import { MobileView, BrowserView } from 'react-device-detect' | ||
import { BrowserView, MobileView } from 'react-device-detect' | ||
|
||
import Pagination from './pagination' | ||
import { tailwindWrapper } from "formula_one/src/utils/tailwindWrapper" | ||
import { setIssueList, changePage, setUser, setStatusNumbers } from '../actions' | ||
|
||
class TabPagination extends Component { | ||
componentDidMount () { | ||
this.props.SetUser() | ||
this.props.SetStatusNumbers() | ||
this.props.SetIssueList(1, 'opened') | ||
} | ||
const TabPagination = ({ issueList, page, SetUser, SetStatusNumbers, SetIssueList, ChangePage }) => { | ||
const inputRef = useRef(null) | ||
const [activePage, setActivePage ] = useState(1) | ||
const [totalPages, setTotalPages] = useState(1) | ||
|
||
handleRef = c => { | ||
this.inputRef = c | ||
} | ||
useEffect(() => { | ||
setTotalPages(Math.ceil(issueList['count'] / 10)) | ||
}, [issueList]) | ||
|
||
handlePageChange = (event, data) => { | ||
this.props.ChangePage(data['activePage'], this.props.page['status']) | ||
this.props.SetIssueList(data['activePage'], this.props.page['status']) | ||
} | ||
useEffect(() => { | ||
setActivePage(page.index) | ||
}, [page]) | ||
|
||
useEffect(() => { | ||
SetUser() | ||
SetStatusNumbers() | ||
SetIssueList(1, 'opened') | ||
}, [SetUser, SetStatusNumbers, SetIssueList]) | ||
|
||
handleInputChange = event => { | ||
this.props.ChangePage(event.target.value, this.props.page['status']) | ||
this.props.SetIssueList(event.target.value, this.props.page['status']) | ||
} | ||
const handlePageChange = (activePage) => { | ||
if(activePage < 1 || activePage > totalPages) | ||
return | ||
ChangePage(activePage, page.status) | ||
SetIssueList(activePage, page.status) | ||
setCurrentPage(activePage) | ||
} | ||
|
||
handleKeyPress = event => { | ||
if (event.key === 'Enter') { | ||
this.props.ChangePage(event.target.value, this.props.page['status']) | ||
this.props.SetIssueList(event.target.value, this.props.page['status']) | ||
this.inputRef.blur() | ||
const handleInputChange = (event) => { | ||
ChangePage(event.target.value, page.status) | ||
SetIssueList(event.target.value, page.status) | ||
} | ||
|
||
const handleKeyPress = (event) => { | ||
if (event.key === 'Enter') { | ||
ChangePage(event.target.value, page.status) | ||
SetIssueList(event.target.value, page.status) | ||
inputRef.current.blur() | ||
} | ||
} | ||
} | ||
render () { | ||
|
||
return ( | ||
<React.Fragment> | ||
<BrowserView> | ||
<Pagination | ||
activePage={this.props.page['index'] && this.props.page['index']} | ||
totalPages={ | ||
this.props.issueList | ||
? Math.ceil(this.props.issueList['count'] / 10) | ||
: '1' | ||
} | ||
onPageChange={this.handlePageChange} | ||
firstItem={{ | ||
'aria-label': 'First item', | ||
content: <Icon name='angle double left' />, | ||
key: '1' | ||
}} | ||
prevItem={{ | ||
'aria-label': 'Previous item', | ||
content: <Icon name='angle left' />, | ||
key: '4' | ||
}} | ||
nextItem={{ | ||
'aria-label': 'Next item', | ||
content: <Icon name='angle right' />, | ||
key: '3' | ||
}} | ||
lastItem={{ | ||
'aria-label': 'Last item', | ||
content: <Icon name='angle double right' />, | ||
key: '2' | ||
}} | ||
/> | ||
</BrowserView> | ||
<MobileView> | ||
<Input | ||
type='number' | ||
placeholder={this.props.page['index'] && this.props.page['index']} | ||
style={{ width: '4em' }} | ||
onKeyPress={this.handleKeyPress} | ||
ref={this.handleRef} | ||
/> | ||
{' of '} | ||
{this.props.issueList | ||
? Math.ceil(this.props.issueList['count'] / 10) | ||
: '1'} | ||
</MobileView> | ||
</React.Fragment> | ||
) | ||
} | ||
<> | ||
<div className={tailwindWrapper('flex items-center justify-end p-4')}> | ||
<BrowserView> | ||
<Pagination totalPages={totalPages} activePage={activePage} onPageChange={handlePageChange}/> | ||
</BrowserView> | ||
<MobileView> | ||
<div className={tailwindWrapper('flex items-center')}> | ||
<input | ||
type="number" | ||
placeholder={page.index} | ||
onKeyPress={handleKeyPress} | ||
ref={inputRef} | ||
className={tailwindWrapper('border p-1 text-center focus:outline-none w-14')} | ||
/> | ||
<span className={tailwindWrapper('mx-2')}> | ||
of {Math.ceil(issueList.count / 10)} | ||
</span> | ||
</div> | ||
</MobileView> | ||
</div> | ||
</> | ||
) | ||
} | ||
|
||
function mapStateToProps (state) { | ||
return { | ||
whoAmI: state.whoAmI, | ||
issueList: state.issueList, | ||
page: state.paginationIndex, | ||
statusNumbers: state.statusNumbers | ||
} | ||
} | ||
const mapStateToProps = (state) => ({ | ||
whoAmI: state.whoAmI, | ||
issueList: state.issueList, | ||
page: state.paginationIndex, | ||
statusNumbers: state.statusNumbers, | ||
}) | ||
|
||
const mapDispatchToProps = dispatch => { | ||
return { | ||
SetUser: () => { | ||
dispatch(setUser) | ||
}, | ||
SetIssueList: (id, status) => { | ||
dispatch(setIssueList(id, status)) | ||
}, | ||
ChangePage: (index, status) => { | ||
dispatch(changePage(index, status)) | ||
}, | ||
SetStatusNumbers: () => { | ||
dispatch(setStatusNumbers()) | ||
} | ||
} | ||
} | ||
const mapDispatchToProps = (dispatch) => ({ | ||
SetUser: () => dispatch(setUser), | ||
SetIssueList: (id, status) => dispatch(setIssueList(id, status)), | ||
ChangePage: (index, status) => dispatch(changePage(index, status)), | ||
SetStatusNumbers: () => dispatch(setStatusNumbers()), | ||
}) | ||
|
||
export default connect( | ||
mapStateToProps, | ||
mapDispatchToProps | ||
)(TabPagination) | ||
export default connect(mapStateToProps, mapDispatchToProps)(TabPagination) |
Oops, something went wrong.