-
Notifications
You must be signed in to change notification settings - Fork 7
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
#165273536: Pagination support for articles #41
Open
kalsmic
wants to merge
1
commit into
andela:develop
Choose a base branch
from
kalsmic:feat-article-pagination-165273536
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.article-pagination { | ||
display: flex; | ||
flex-direction: row; | ||
justify-content: space-around; | ||
margin: 1rem; | ||
} |
49 changes: 49 additions & 0 deletions
49
src/components/ArticlePagination/ArticlePagination.test.js
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,49 @@ | ||
import React from 'react'; | ||
|
||
import { mount } from 'enzyme'; | ||
|
||
import ArticlePagination from 'components/ArticlePagination'; | ||
import Button from 'components/Button'; | ||
|
||
describe('ArticlePagination', () => { | ||
const props = { | ||
previous: null, | ||
next: 'next', | ||
paginate: jest.fn(), | ||
count: 20, | ||
}; | ||
let wrapper = mount(<ArticlePagination {...props} />); | ||
|
||
it('should render without crushing', () => { | ||
expect(wrapper).toMatchSnapshot(); | ||
}); | ||
|
||
const previousButton = wrapper.find(Button).at(0); | ||
const nextButton = wrapper.find(Button).at(1); | ||
|
||
it('should disable buttons if null', () => { | ||
expect(previousButton.props().disabled).toBe(true); | ||
expect(nextButton.props().disabled).toBe(false); | ||
}); | ||
|
||
|
||
it('should call the paginate function on button click', () => { | ||
const event = { | ||
target: { | ||
type: 'button', | ||
name: '', | ||
}, | ||
preventDefault: jest.fn(), | ||
}; | ||
|
||
nextButton.simulate('click', event); | ||
expect(props.paginate).toBeCalled(); | ||
previousButton.simulate('click', event); | ||
|
||
|
||
props.previous = 'previous'; | ||
wrapper = mount(<ArticlePagination {...props} />); | ||
wrapper.find(Button).at(0).simulate('click', event); | ||
expect(props.paginate).toBeCalled(); | ||
}); | ||
}); |
50 changes: 50 additions & 0 deletions
50
src/components/ArticlePagination/__snapshots__/ArticlePagination.test.js.snap
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,50 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`ArticlePagination should render without crushing 1`] = ` | ||
<ArticlePagination | ||
count={20} | ||
next="next" | ||
paginate={[MockFunction]} | ||
previous={null} | ||
> | ||
<div | ||
className="article-pagination" | ||
> | ||
<Button | ||
btnClass="fa fa-backward" | ||
btnEvent={[Function]} | ||
btnName="" | ||
disabled={true} | ||
> | ||
<button | ||
className="fa fa-backward" | ||
disabled={true} | ||
onClick={[Function]} | ||
type="button" | ||
/> | ||
</Button> | ||
<span> | ||
1 | ||
|
||
/ | ||
|
||
2 | ||
|
||
pages | ||
</span> | ||
<Button | ||
btnClass="fa fa-forward" | ||
btnEvent={[Function]} | ||
btnName="" | ||
disabled={false} | ||
> | ||
<button | ||
className="fa fa-forward" | ||
disabled={false} | ||
onClick={[Function]} | ||
type="button" | ||
/> | ||
</Button> | ||
</div> | ||
</ArticlePagination> | ||
`; |
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,65 @@ | ||
// react | ||
import React from 'react'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Parsing error: 'import' and 'export' may appear only with 'sourceType: module' |
||
|
||
// Proptypes for validation | ||
import PropTypes from 'prop-types'; | ||
|
||
// stylesheets | ||
import './ArticlePagination.scss'; | ||
|
||
// Button component | ||
import Button from 'components/Button'; | ||
|
||
const ArticlePagination = ({ | ||
previous, next, paginate, count, page | ||
}) => { | ||
const pages = Math.ceil(count / 10); | ||
return ( | ||
|
||
<div className="article-pagination"> | ||
<Button | ||
btnName="" | ||
btnClass="fa fa-backward" | ||
btnEvent={(e) => { | ||
e.preventDefault(); | ||
paginate(previous, -1); | ||
}} | ||
disabled={!previous} | ||
/> | ||
<span> | ||
{page || 1} | ||
{' '} | ||
{'/'} | ||
{' '} | ||
{pages} | ||
{' '} | ||
{'pages '} | ||
</span> | ||
<Button | ||
btnName="" | ||
btnClass="fa fa-forward" | ||
btnEvent={(e) => { | ||
e.preventDefault(); | ||
paginate(next, 1); | ||
}} | ||
disabled={!next} | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
ArticlePagination.defaultProps = { | ||
next: null, | ||
previous: null, | ||
}; | ||
|
||
ArticlePagination.propTypes = { | ||
next: PropTypes.string, | ||
page: PropTypes.number.isRequired, | ||
count: PropTypes.number.isRequired, | ||
previous: PropTypes.string, | ||
paginate: PropTypes.func.isRequired, | ||
}; | ||
|
||
|
||
export default ArticlePagination; |
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 |
---|---|---|
|
@@ -3,7 +3,6 @@ import React, { Component } from 'react'; | |
|
||
// third-party libraries | ||
import { ToastContainer } from 'react-toastify'; | ||
import { bindActionCreators } from 'redux'; | ||
import { connect } from 'react-redux'; | ||
import PropTypes from 'prop-types'; | ||
|
||
|
@@ -15,19 +14,44 @@ import Navbar from 'components/NavBar'; | |
import Footer from 'components/Footer'; | ||
import Loader from 'components/Loader'; | ||
import { getAllArticles } from 'store/actions/articleActions'; | ||
import ArticlePagination from 'components/ArticlePagination'; | ||
import ArticleColumn from './ArticleColumn'; | ||
|
||
|
||
export class LandingPage extends Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
page: 1, | ||
}; | ||
} | ||
|
||
componentDidMount() { | ||
const { getArticles } = this.props; | ||
|
||
// fetch articles | ||
getArticles(); | ||
} | ||
|
||
|
||
paginate = (apiCallUrl, Increment) => { | ||
const { getArticles } = this.props; | ||
const { page } = this.state; | ||
this.setState({ | ||
page: page + Increment | ||
}); | ||
|
||
getArticles(apiCallUrl); | ||
}; | ||
|
||
|
||
render() { | ||
const { articles, isFetching } = this.props; | ||
const { | ||
articles, isFetching, previous, count, next | ||
} = this.props; | ||
const { page } = this.state; | ||
|
||
|
||
return ( | ||
|
||
// Navbar | ||
|
@@ -65,7 +89,18 @@ export class LandingPage extends Component { | |
<ArticleColumn columnTitle="Most Popular" articles={articles} /> | ||
<ArticleColumn columnTitle="Most Liked" articles={articles} /> | ||
<ArticleColumn columnTitle="Most Recent" articles={articles} /> | ||
|
||
</div> | ||
<div> | ||
<ArticlePagination | ||
paginate={this.paginate} | ||
next={next} | ||
previous={previous} | ||
count={count} | ||
page={page} | ||
/> | ||
</div> | ||
|
||
<Footer /> | ||
</div> | ||
</div> | ||
|
@@ -75,6 +110,9 @@ export class LandingPage extends Component { | |
|
||
// proptype validation | ||
LandingPage.propTypes = { | ||
next: PropTypes.string, | ||
count: PropTypes.number, | ||
previous: PropTypes.string, | ||
getArticles: PropTypes.func, | ||
isFetching: PropTypes.bool, | ||
articles: PropTypes.arrayOf(PropTypes.shape({ | ||
|
@@ -105,6 +143,9 @@ LandingPage.propTypes = { | |
|
||
LandingPage.defaultProps = { | ||
articles: [], | ||
next: null, | ||
previous: null, | ||
count: 0, | ||
isFetching: false, | ||
getArticles: () => { }, | ||
}; | ||
|
@@ -115,15 +156,25 @@ LandingPage.defaultProps = { | |
* @return {articles} - An aray of articles | ||
* | ||
*/ | ||
const mapStateToProps = state => state.articles; | ||
// const mapStateToProps = state => state.articles; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove the comment line. |
||
const mapStateToProps = (state) => { | ||
const { | ||
next, count, previous, articles | ||
} = state.articles; | ||
return { | ||
next, count, previous, articles | ||
}; | ||
}; | ||
|
||
/** | ||
* This is a function to pass the state as a prop. | ||
* @param {dispatch} - The dispatch function | ||
* @return {getArticles} - The thunk to fetch articles | ||
* | ||
*/ | ||
const mapDispatchToProps = dispatch => bindActionCreators({ | ||
getArticles: () => getAllArticles(), | ||
}, dispatch); | ||
|
||
const mapDispatchToProps = dispatch => ({ | ||
getArticles(apiCallUrl) { dispatch(getAllArticles(apiCallUrl)); }, | ||
}); | ||
|
||
export default connect(mapStateToProps, mapDispatchToProps)(LandingPage); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parsing error: 'import' and 'export' may appear only with 'sourceType: module'