Por fin un paginado que funciona
Entiedo mucho mejor esto de react que angular~~~ Tengo que usar mas los callbacks cuando haog navegaciones para volver a leer las props, como cambia la query string y esta en props~
This commit is contained in:
@@ -149,21 +149,21 @@ export class Paginate extends Component {
|
|||||||
<ul className="pagination">
|
<ul className="pagination">
|
||||||
{pages.map((page, index) => {
|
{pages.map((page, index) => {
|
||||||
if (page === LEFT_PAGE) return (
|
if (page === LEFT_PAGE) return (
|
||||||
<li key={index} className="page-item">
|
<li key={page} className="page-item">
|
||||||
<a className="page-link" href={this.makePageLink(this.state.currentPage - 1)} onClick={this.handleMoveLeft}>
|
<a className="page-link" href={this.makePageLink(this.state.currentPage - 1)} onClick={this.handleMoveLeft}>
|
||||||
<span>«</span>
|
<span>« Anterior</span>
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
);
|
);
|
||||||
if (page === RIGHT_PAGE) return (
|
if (page === RIGHT_PAGE) return (
|
||||||
<li key={index} className="page-item">
|
<li key={page} className="page-item">
|
||||||
<a className="page-link" href={this.makePageLink(this.state.currentPage + 1)} onClick={this.handleMoveRight}>
|
<a className="page-link" href={this.makePageLink(this.state.currentPage + 1)} onClick={this.handleMoveRight}>
|
||||||
<span>»</span>
|
<span>Siguiente »</span>
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
);
|
);
|
||||||
return (
|
return (
|
||||||
<li key={index} className={`page-item ${currentPage === page ? 'active' : ''}`}>
|
<li key={page} className={`page-item ${currentPage === page ? 'active' : ''}`}>
|
||||||
<a className="page-link" href={this.makePageLink(index)}
|
<a className="page-link" href={this.makePageLink(index)}
|
||||||
onClick={this.handleClick(page)}>{page}</a>
|
onClick={this.handleClick(page)}>{page}</a>
|
||||||
</li>
|
</li>
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
import React from "react";
|
import React, {Fragment} from "react";
|
||||||
import queryString from "query-string";
|
import queryString from "query-string";
|
||||||
import ReactJson from "react-json-view";
|
import ReactJson from "react-json-view";
|
||||||
import {searchArtist} from "../services/search_service";
|
import {searchArtist} from "../services/search_service";
|
||||||
import SearchBar from "./SearchBar";
|
import SearchBar from "./SearchBar";
|
||||||
import {Paginate} from "./Paginate";
|
import {Paginate} from "./Paginate";
|
||||||
import {navigate} from "@reach/router";
|
|
||||||
|
|
||||||
|
|
||||||
class SearchList extends React.Component {
|
class SearchList extends React.Component {
|
||||||
@@ -25,85 +24,95 @@ class SearchList extends React.Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class Search extends React.Component {
|
export class Search extends React.Component {
|
||||||
|
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props);
|
super(props);
|
||||||
|
|
||||||
this.state = {
|
this.loadParams = this.loadParams.bind(this);
|
||||||
artists: null
|
|
||||||
};
|
|
||||||
|
|
||||||
this.getParams = this.getParams.bind(this);
|
|
||||||
this.makeLink = this.makeLink.bind(this);
|
this.makeLink = this.makeLink.bind(this);
|
||||||
this.handlePageChange = this.handlePageChange.bind(this);
|
this.handlePageChange = this.handlePageChange.bind(this);
|
||||||
}
|
this.handleQueryChange = this.handleQueryChange.bind(this);
|
||||||
|
|
||||||
|
this.state = {
|
||||||
static getDerivedStateFromProps(props, state) {
|
|
||||||
if (props.location.search !== state.prevSearch) {
|
|
||||||
return {
|
|
||||||
artists: null,
|
artists: null,
|
||||||
prevSearch: props.location.search
|
paginate: null,
|
||||||
}
|
params: this.loadParams()
|
||||||
}
|
};
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
this.loadArtists(this.getParams().query, this.getParams().page);
|
this.loadArtists(this.state.params.query, this.state.params.page);
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidUpdate(prevProps, prevState, snapshot) {
|
componentDidUpdate(prevProps, prevState, snapshot) {
|
||||||
if (this.state.artists == null) {
|
if (prevState.params !== this.state.params) {
|
||||||
this.loadArtists(this.getParams().query, this.getParams().page);
|
this.loadArtists(this.state.params.query, this.state.params.page);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
makeLink(page) {
|
makeLink(page) {
|
||||||
return `/search?query=${this.getParams().query}&page=${page}`
|
return `/search?query=${this.state.params.query}&page=${page}`
|
||||||
}
|
}
|
||||||
|
|
||||||
handlePageChange(page) {
|
handlePageChange(page) {
|
||||||
navigate(page);
|
this.props.navigate(page).then(() => {
|
||||||
|
this.setState({
|
||||||
|
artists: null,
|
||||||
|
params: this.loadParams()
|
||||||
|
});
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
handleQueryChange(query) {
|
||||||
|
this.setState({artists: null, paginate: null, params: this.loadParams()})
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
let paginate = null;
|
||||||
|
let artists = null;
|
||||||
|
|
||||||
|
if (this.state.paginate) {
|
||||||
|
const total = this.state.paginate.total;
|
||||||
|
const currentPage = this.state.paginate.current_page;
|
||||||
|
const pageLimit = this.state.paginate.per_page;
|
||||||
|
|
||||||
|
paginate = <Paginate totalRecords={total} pageLimit={pageLimit} currentPage={currentPage}
|
||||||
|
pageNeighbours={2} onPageChanged={this.handlePageChange}
|
||||||
|
makeLink={this.makeLink}/>
|
||||||
|
}
|
||||||
|
|
||||||
if (this.state.artists) {
|
if (this.state.artists) {
|
||||||
const total = this.state.artists.paginate.total;
|
artists = (
|
||||||
const currentPage = this.state.artists.paginate.current_page;
|
<Fragment>
|
||||||
const pageLimit = this.state.artists.paginate.per_page;
|
<SearchList artists={this.state.artists}/>
|
||||||
|
|
||||||
return (
|
|
||||||
<main>
|
|
||||||
<h1>Busqueda</h1>
|
|
||||||
<SearchBar query={this.getParams().query}/>
|
|
||||||
<SearchList artists={this.state.artists.artists}/>
|
|
||||||
<Paginate totalRecords={total} pageLimit={pageLimit} currentPage={currentPage} pageNeighbours={1} onPageChanged={this.handlePageChange} makeLink={this.makeLink}/>
|
|
||||||
<ReactJson src={this.state.artists} enableClipboard={false} displayDataTypes={false}/>
|
<ReactJson src={this.state.artists} enableClipboard={false} displayDataTypes={false}/>
|
||||||
</main>
|
</Fragment>
|
||||||
);
|
);
|
||||||
} else {
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<main>
|
<main>
|
||||||
<h1>Busqueda</h1>
|
<h1>Busqueda</h1>
|
||||||
<SearchBar query={this.query}/>
|
<SearchBar query={this.state.params.query} onQueryChanged={this.handleQueryChange}/>
|
||||||
<p>Loading...</p>
|
{paginate && paginate}
|
||||||
|
{artists ? artists : <p>Loading...</p>}
|
||||||
</main>
|
</main>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
getParams() {
|
loadParams() {
|
||||||
return queryString.parse(this.props.location.search);
|
return queryString.parse(this.props.location.search);
|
||||||
}
|
}
|
||||||
|
|
||||||
loadArtists(query, page) {
|
loadArtists(query, page) {
|
||||||
if(!page){
|
if (!page) {
|
||||||
page = 1;
|
page = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
searchArtist(query, page).then((response) => {
|
searchArtist(query, page).then((response) => {
|
||||||
this.setState({artists: response})
|
this.setState({
|
||||||
|
artists: response.artists,
|
||||||
|
paginate: response.paginate,
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,11 +5,9 @@ import {navigate} from "@reach/router";
|
|||||||
export default class SearchBar extends React.Component {
|
export default class SearchBar extends React.Component {
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props);
|
super(props);
|
||||||
this.state = {query: ''};
|
this.state = {
|
||||||
|
query: this.props.query ? this.props.query : ''
|
||||||
if (this.props.query) {
|
};
|
||||||
this.state = {query: this.props.query};
|
|
||||||
}
|
|
||||||
|
|
||||||
this.handleChange = this.handleChange.bind(this);
|
this.handleChange = this.handleChange.bind(this);
|
||||||
this.handleSubmit = this.handleSubmit.bind(this);
|
this.handleSubmit = this.handleSubmit.bind(this);
|
||||||
@@ -22,15 +20,17 @@ export default class SearchBar extends React.Component {
|
|||||||
|
|
||||||
handleSubmit(event, who) {
|
handleSubmit(event, who) {
|
||||||
if (event.key === 'Enter' || who === 'button') {
|
if (event.key === 'Enter' || who === 'button') {
|
||||||
navigate(`/search?query=${this.state.query}`);
|
const query = this.state.query;
|
||||||
|
const onQueryChanged = this.props.onQueryChanged;
|
||||||
|
|
||||||
|
navigate(`/search?query=${query}`).then(() => onQueryChanged(query));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<div className='input-with-icon'>
|
<div className='input-with-icon'>
|
||||||
<input className='full-width' value={this.state.query} onKeyUp={this.handleSubmit}
|
<input className='full-width' value={this.state.query} onKeyUp={this.handleSubmit} onChange={this.handleChange}/>
|
||||||
onChange={this.handleChange}/>
|
|
||||||
<button onClick={(e) => this.handleSubmit(e, 'button')}><FaSearch/></button>
|
<button onClick={(e) => this.handleSubmit(e, 'button')}><FaSearch/></button>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user