Fixed problem while 'reloading' a page with different route params not making the server to be hit

This commit is contained in:
Alejandro Celaya 2018-07-18 19:01:43 +02:00
parent 22406d1253
commit 5a7ac69aff
2 changed files with 6 additions and 12 deletions

View file

@ -2,7 +2,6 @@ import React from 'react';
import { Link } from 'react-router-dom'; import { Link } from 'react-router-dom';
import { Pagination, PaginationItem, PaginationLink } from 'reactstrap'; import { Pagination, PaginationItem, PaginationLink } from 'reactstrap';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { updateShortUrlsList } from './reducers/shortUrlsList';
export class Paginator extends React.Component { export class Paginator extends React.Component {
render() { render() {
@ -21,7 +20,6 @@ export class Paginator extends React.Component {
<PaginationLink <PaginationLink
tag={Link} tag={Link}
to={`/server/${serverId}/list-short-urls/${i}`} to={`/server/${serverId}/list-short-urls/${i}`}
onClick={() => this.updatePage(i)}
> >
{i} {i}
</PaginationLink> </PaginationLink>
@ -39,7 +37,6 @@ export class Paginator extends React.Component {
previous previous
tag={Link} tag={Link}
to={`/server/${serverId}/list-short-urls/${currentPage - 1}`} to={`/server/${serverId}/list-short-urls/${currentPage - 1}`}
onClick={() => this.updatePage(currentPage - 1)}
/> />
</PaginationItem> </PaginationItem>
{renderPages()} {renderPages()}
@ -48,18 +45,11 @@ export class Paginator extends React.Component {
next next
tag={Link} tag={Link}
to={`/server/${serverId}/list-short-urls/${currentPage + 1}`} to={`/server/${serverId}/list-short-urls/${currentPage + 1}`}
onClick={() => this.updatePage(currentPage + 1)}
/> />
</PaginationItem> </PaginationItem>
</Pagination> </Pagination>
); );
} }
updatePage(page) {
this.props.updateShortUrlsList({ ...this.props.shortUrlsListParams, page })
}
} }
export default connect(state => ({ export default connect()(Paginator);
shortUrlsListParams: state.shortUrlsListParams,
}), { updateShortUrlsList })(Paginator);

View file

@ -6,10 +6,14 @@ import './ShortUrls.scss';
import ShortUrlsList from './ShortUrlsList'; import ShortUrlsList from './ShortUrlsList';
export function ShortUrls(props) { export function ShortUrls(props) {
const { match: { params } } = props;
// Using a key on a component makes react to create a new instance every time the key changes
const urlsListKey = `${params.serverId}_${params.page}`;
return ( return (
<div className="short-urls-container"> <div className="short-urls-container">
<div className="form-group"><SearchBar /></div> <div className="form-group"><SearchBar /></div>
<ShortUrlsList {...props} shortUrlsList={props.shortUrlsList.data || []} /> <ShortUrlsList {...props} shortUrlsList={props.shortUrlsList.data || []} key={urlsListKey} />
<Paginator paginator={props.shortUrlsList.pagination} serverId={props.match.params.serverId} /> <Paginator paginator={props.shortUrlsList.pagination} serverId={props.match.params.serverId} />
</div> </div>
); );