From 7ede6effcc3f26fcb9d127a56e6f35bbdc8eb94f Mon Sep 17 00:00:00 2001 From: Daniel Cortes Date: Fri, 5 Jun 2020 18:13:22 -0400 Subject: [PATCH] Usando componentes funcionales --- src/components/Search.jsx | 502 +++++++++++++++++--------------------- 1 file changed, 219 insertions(+), 283 deletions(-) diff --git a/src/components/Search.jsx b/src/components/Search.jsx index 51b1021..ee0e8c4 100644 --- a/src/components/Search.jsx +++ b/src/components/Search.jsx @@ -1,4 +1,4 @@ -import React, {Fragment} from "react"; +import React, {Fragment, useEffect, useState} from "react"; import queryString from "query-string"; import {searchArtist, searchDisc, searchSong} from "../services/search_service"; @@ -9,33 +9,24 @@ import {Tab, TabList, TabPanel, Tabs} from "react-tabs"; import {ReactComponent as DiscSVG} from "../svg/disc.svg"; -class CoverArt extends React.Component { - constructor(props) { - super(props); - this.state = { - loading: true - } - } +const CoverArt = (props) => { + const [loading, setLoading] = useState(true); - handleLoad = () => { - this.setState({loading: false}) - }; + const handleLoad = () => setLoading(false) - render() { - if (this.props.cover_art) { - if (this.state.loading) { - return ( - - {this.props.alt} - - - ) - } else { - return {this.props.alt}/ - } + if (props.cover_art) { + if (loading) { + return ( + + {props.alt} + + + ) } else { - return + return {props.alt}/ } + } else { + return } } @@ -48,336 +39,281 @@ const SearchPlaceholder = (props) => { ) } -class SearchSong extends React.Component { - render() { - const song = this.props.song; - return ( -
  • - -
    - {song.title} - {song.artist[0].name} -
    - -
  • - ) - } +const SearchSong = (props) => { + const song = props.song; + return ( +
  • + +
    + {song.title} + {song.artist[0].name} +
    + +
  • + ) } -class SearchSongs extends React.Component { - constructor(props) { - super(props); - this.state = {songs: null, paginate: null, page: this.props.page ? this.props.page : 1} +const SearchSongs = (props) => { + const [songs, setSongs] = useState(null); + const [paginate, setPaginate] = useState(null); + const [page, setPage] = useState(props.page ? props.page : 1); + + useEffect(() => { + setSongs(null); + setPaginate(null); + loadSongs(props.query, page) + }, [props.query, page]); + + const makeLink = page => `/search/song?query=${props.query}&page=${page}`; + + const handlePageChange = page => { + setSongs(null); + setPage(page); + props.onPageChange(page); } - componentDidMount = _ => this.loadSongs(this.props.query, this.state.page); - - componentDidUpdate = (prevProps, prevState) => { - if (prevProps.query !== this.props.query || prevState.page !== this.state.page) { - this.loadSongs(this.props.query, this.state.page) - - if (prevState.page !== this.state.page) { - this.setState({songs: null}) - } else { - this.setState({songs: null, paginate: null}) - } - } - }; - - makeLink = page => `/search/song?query=${this.props.query}&page=${page}`; - - handlePageChange = page => { - this.setState({songs: null, page: page}) - this.props.onPageChange(page); - } - - loadSongs = (query, page) => { + const loadSongs = (query, page) => { page = page ? page : 1; searchSong(query, page).then((response) => { - this.setState({ - songs: response.recordings, - paginate: response.paginate - }) + setSongs(response.recordings); + setPaginate(response.paginate); }) }; - render = () => { - let songs = ; - if (this.state.songs) { - songs = this.state.songs.map((song) => ); - } + let songsComponent = ; + if (songs) { + songsComponent = songs.map((song) => ); + } - let paginate; - if (this.state.paginate) { - const total = this.state.paginate.total; - const currentPage = this.state.paginate.current_page; - const pageLimit = this.state.paginate.per_page; + let paginateComponent; + if (paginate) { + const total = paginate.total; + const currentPage = paginate.current_page; + const pageLimit = paginate.per_page; - paginate = - } + paginateComponent = + } - return ( - -
      - {songs} -
    - {paginate} -
    - ) - }; + return ( + +
      + {songsComponent} +
    + {paginateComponent} +
    + ) } -class SearchDisc extends React.Component { - getReleaseYear = (release) => { - const releaseDate = new Date(release); - return ( - {releaseDate.getFullYear()} - ) - } - - render() { - const disc = this.props.disc; - return ( -
  • - - -
    - {disc.title} - {disc.artist[0].name} -
    - -
  • - ) - } +const SearchDisc = (props) => { + const disc = props.disc; + return ( +
  • + + +
    + {disc.title} + {disc.artist[0].name} +
    + +
  • + ) } -class SearchDiscs extends React.Component { - constructor(props) { - super(props); - this.state = {discs: null, paginate: null, page: this.props.page ? this.props.page : 1} +const SearchDiscs = (props) => { + const [discs, setDiscs] = useState(null); + const [paginate, setPaginate] = useState(null); + const [page, setPage] = useState(null); + + useEffect(_ => { + setDiscs(null) + setPaginate(null) + loadDiscs(props.query, page) + }, [props.query, page]) + + const makeLink = page => `/search/disc?query=${props.query}&page=${page}`; + + const handlePageChange = page => { + setDiscs(null); + setPage(page); + props.onPageChange(page); } - componentDidMount = _ => this.loadDiscs(this.props.query, this.state.page); - - componentDidUpdate = (prevProps, prevState) => { - if (prevProps.query !== this.props.query || prevState.page !== this.state.page) { - this.loadDiscs(this.props.query, this.state.page) - - if (prevState.page !== this.state.page) { - this.setState({discs: null}) - } else { - this.setState({discs: null, paginate: null}) - } - } - }; - - makeLink = page => `/search/disc?query=${this.props.query}&page=${page}`; - - handlePageChange = page => { - this.setState({discs: null, page: page}) - this.props.onPageChange(page); - } - - loadDiscs = (query, page) => { + const loadDiscs = (query, page) => { page = page ? page : 1; searchDisc(query, page).then((response) => { - this.setState({ - discs: response.discs, - paginate: response.paginate - }) + setDiscs(response.discs); + setPaginate(response.paginate); }) }; - render = () => { - let discs = ; - if (this.state.discs) { - discs = this.state.discs.map((disc) => ); - } + let discsComponent = ; + if (discs) { + discsComponent = discs.map((disc) => ); + } - let paginate; - if (this.state.paginate) { - const total = this.state.paginate.total; - const currentPage = this.state.paginate.current_page; - const pageLimit = this.state.paginate.per_page; + let paginateComponent; + if (paginate) { + const total = paginate.total; + const currentPage = paginate.current_page; + const pageLimit = paginate.per_page; - paginate = - } + paginateComponent = + } - return ( - -
      - {discs} -
    - {paginate} -
    - ) - }; + return ( + +
      + {discsComponent} +
    + {paginateComponent} +
    + ) } -class SearchArtist extends React.Component { - render() { - const artist = this.props.artist; - return ( -
  • - - - {artist.name} - {[artist.type, artist.country].filter(Boolean).join(' - ')} - - -
  • - ) - } +const SearchArtist = (props) => { + const artist = props.artist; + return ( +
  • + + + {artist.name} + {[artist.type, artist.country].filter(Boolean).join(' - ')} + + +
  • + ) } -class SearchArtists extends React.Component { - constructor(props) { - super(props); - this.state = {artists: null, paginate: null, page: this.props.page ? this.props.page : 1} +const SearchArtists = (props) => { + const [artists, setArtist] = useState(null); + const [paginate, setPaginate] = useState(null); + const [page, setPage] = useState(props.page ? props.page : 1); + + useEffect(_ => { + setArtist(null) + setPaginate(null) + loadArtists(props.query, page) + }, [props.query, page]) + + const makeLink = page => `/search/artist?query=${props.query}&page=${page}`; + + const handlePageChange = page => { + setArtist(null); + setPage(page); + props.onPageChange(page); } - componentDidMount = _ => this.loadArtists(this.props.query, this.state.page); - - componentDidUpdate = (prevProps, prevState) => { - if (prevProps.query !== this.props.query || prevState.page !== this.state.page) { - this.loadArtists(this.props.query, this.state.page) - - if (prevState.page !== this.state.page) { - this.setState({artists: null}) - } else { - this.setState({artists: null, paginate: null}) - } - } - }; - - makeLink = page => `/search/artist?query=${this.props.query}&page=${page}`; - - handlePageChange = page => { - this.setState({artists: null, page: page}) - this.props.onPageChange(page); - } - - loadArtists = (query, page) => { + const loadArtists = (query, page) => { page = page ? page : 1; searchArtist(query, page).then((response) => { - this.setState({ - artists: response.artists, - paginate: response.paginate - }) + setArtist(response.artists); + setPaginate(response.paginate); }) }; - render = () => { - let artists = ; - if (this.state.artists) { - artists = this.state.artists.map((artist) => ); - } - - let paginate; - 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 = - } - - return ( - -
      - {artists} -
    - {paginate} -
    - ) - }; -} - -class SearchTabs extends React.Component { - constructor(props) { - super(props); - this.handleSelect = this.handleSelect.bind(this); + let artistsContent = ; + if (artists) { + artistsContent = artists.map((artist) => ); } - nameToIndex(name) { + let paginateContent; + if (paginate) { + const total = paginate.total; + const currentPage = paginate.current_page; + const pageLimit = paginate.per_page; + + paginateContent = + } + + return ( + +
      + {artistsContent} +
    + {paginateContent} +
    + ) +} + +const SearchTabs = (props) => { + const nameToIndex = (name) => { if (name === 'artist') return 0 if (name === 'disc') return 1 if (name === 'song') return 2 else return 0; } - indexToName(index) { + const indexToName = (index) => { if (index === 0) return 'artist' if (index === 1) return 'disc' if (index === 2) return 'song' else return 'artist'; } - handleSelect(index) { - if (this.props.onTabChanged) this.props.onTabChanged(this.indexToName(index)) + const handleSelect = (index) => { + props.onTabChanged(indexToName(index)) } - handlePageChange = (who) => (index) => { - this.props.onPageChange(who, index); - } + const handlePageChange = (who) => (index) => props.onPageChange(who, index); - render() { - return ( - - - Artistas - Discos - Canciones - - - - - - ) - } + return ( + + + Artistas + Discos + Canciones + + + + + + ) } -export class Search extends React.Component { - constructor(props) { - super(props); - const parsed = queryString.parse(this.props.location.search); - this.state = { - who: this.props.match.params['who'] ? this.props.match.params['who'] : 'artist', - query: parsed.query, - page: !isNaN(+parsed.page) ? +parsed.page : 1, - }; +export const Search = (props) => { + const parsedParams = queryString.parse(props.location.search); + const [who, setWho] = useState(props.match.params['who'] ? props.match.params['who'] : 'artist') + const [query, setQuery] = useState(parsedParams.query) + const [page, setPage] = useState(!isNaN(+parsedParams.page) ? +parsedParams.page : 1) - console.log(this.state); - } + const navigateTo = (who, query, page) => props.history.push(`/search/${who}?query=${query}&page=${page}`); - navigateToState = () => { - const link = `/search/${this.state.who}?query=${this.state.query}&page=${this.state.page}`; - this.props.history.push(link); + const handleQueryChange = query => { + setQuery(query); + setPage(1); + navigateTo(who, query, 1) }; - handleQueryChange = query => { - this.setState({query: query, page: 1}, this.navigateToState); + const handleTabChange = who => { + setWho(who); + setPage(1) + navigateTo(who, query, 1) }; - handleTabChange = name => { - this.setState({who: name, page: 1}, this.navigateToState); - }; + const handlePageChange = (who, page) => { + setWho(who); + setPage(page); + navigateTo(who, query, page); - handlePageChange = (who, page) => { - this.setState({who: who, page: page}, this.navigateToState); window.scroll({left: 0, top: 0, behavior: 'smooth'}); }; - render = () => ( + const content = _ => { + if (query) { + return + } else { + return null; + } + } + + return (

    Búsqueda

    - - {this.state.query && - - } + + {content()}
    ); }