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

Revert state on error #50

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/components/Pager/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const LastButton = ({currentPage, loadingCount, loadingData, totalPages, changeP
}

const buildNumberPageButton = n =>
<PagerButton loading={n.loading} key={n.page} page={n.page} currentPage={n.currentPage} changePage={n.changePage}>
<PagerButton loading={n.loading} key={n.page} page={n.page} currentPage={n.currentPage} changePage={n.changePage} loadingPage={n.loadingPage}>
{n.page + 1}
</PagerButton>

Expand Down
4 changes: 2 additions & 2 deletions src/components/Pager/pagerLogic.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ export const determinePages = (currentPage, loading, pageSize, count) => {
return pages
}

export const buildArrayOfNumberedPagerButtons = ({filter, count, loadingCount, loadingData, changePage}) => {
export const buildArrayOfNumberedPagerButtons = ({filter, count, loadingCount, loadingData, changePage, loadingPage}) => {
const currentPage = filter.page
const numberedPageButtons = []
const pages = determinePages(currentPage, loadingCount, filter.pageSize, count)

for (let i = 0; i < pages.length; i++) {
numberedPageButtons.push({page: pages[i], loading: loadingData, currentPage, changePage})
numberedPageButtons.push({page: pages[i], loading: loadingData, currentPage, changePage, loadingPage})
}

return numberedPageButtons
Expand Down
3 changes: 2 additions & 1 deletion src/components/Pager/story.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { buildArrayOfNumberedPagerButtons } from './pagerLogic'
const buttonData = buildArrayOfNumberedPagerButtons({
filter: {
page: 5,
pageSize: 10
pageSize: 10,
loadingPage: 6
},
count: 23423,
loadingCount: false,
Expand Down
6 changes: 4 additions & 2 deletions src/components/PagerButton/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React from 'react'
import PropTypes from 'prop-types'

const PagerButton = ({page, loading, currentPage, changePage, children, className}) => {
const PagerButton = ({page, loading, currentPage, changePage, children, className, loadingPage}) => {
let buttonStyle = 'btn pager-button'
if (className) buttonStyle += ' ' + className
buttonStyle += (currentPage === page) ? ' btn-primary' : ' btn-default'
buttonStyle += (loading && loadingPage === page) ? ' pager-button-loading' : ''
return (
<button data-page={page} className={buttonStyle} onClick={changePage} disabled={loading}>
{children}
Expand All @@ -18,7 +19,8 @@ PagerButton.propTypes = {
page: PropTypes.number.isRequired,
currentPage: PropTypes.number.isRequired,
children: PropTypes.any.isRequired,
className: PropTypes.string
className: PropTypes.string,
loadingPage: PropTypes.number
}

export default PagerButton
9 changes: 9 additions & 0 deletions src/components/PagerButton/index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@import '../../theme.scss';

.pager-button {
cursor: pointer;
}

.pager-button-loading {
background-color: $loading-blue;
}
1 change: 1 addition & 0 deletions src/components/PagerButton/story.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ stories.add('Basic', () => (
currentPage={number('currentPage', 3)}
children={text('children', 'Page 2')}
className={text('className', '')}
loadingPage={number('page', 1)}
/>
))
8 changes: 5 additions & 3 deletions src/containers/Pager.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const mapStateToProps = (state, props) => {
filter: namedState.filter,
count: namedState.count,
loadingCount: namedState.loadingCount,
loadingData: namedState.loadingData
loadingData: namedState.loadingData,
loadingPage: namedState.filter.loadingPage
}
}

Expand All @@ -24,8 +25,9 @@ const mapDispatchToProps = dispatch => bindActionCreators({

const handlers = {
changePage: props => event => {
const nextPage = window.parseInt(event.target.getAttribute('data-page'), 10)
const updatedFilter = {...props.filter, page: nextPage}
const currentPage = props.filter.page
const loadingPage = window.parseInt(event.target.getAttribute('data-page'), 10)
const updatedFilter = {...props.filter, page: currentPage, loadingPage}
if (updatedFilter && !updatedFilter.page) {
props.refreshCount(props.name, updatedFilter)
}
Expand Down
7 changes: 6 additions & 1 deletion src/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export const initialState = {
filter: {
page: 0,
pageSize: 10,
sorts: []
sorts: [],
loadingPage: 0
}
}

Expand Down Expand Up @@ -66,6 +67,10 @@ const reduceSetData = (state, action) => ({
loadingData: false,
data: action.data,
error: '',
filter: {
...state.filter,
page: state.filter.loadingPage
},
count: coalesce(action.count, state.count) || 0
})

Expand Down
1 change: 1 addition & 0 deletions src/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
@import './components/Headers/index.scss';
@import './components/Manifest/index.scss';
@import './components/SimpleHeader/index.scss';
@import './components/PagerButton/index.scss';
1 change: 1 addition & 0 deletions src/theme.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
$control-error: #900;
$loading-gray: #999;
$loading-blue: #add8e6;
$manifest-error: #a88;
$manifest-header-gray: #777;
$focused-row: #eeeeff;
Expand Down