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

[05-react-buscador-peliculas] Fix bug no validation on first input #117

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
41 changes: 18 additions & 23 deletions projects/05-react-buscador-peliculas/src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,25 @@
import debounce from 'just-debounce-it'
import { useCallback, useEffect, useState } from 'react'
import './App.css'
import { useMovies } from './hooks/useMovies.js'
import { Movies } from './components/Movies.jsx'
import { useState, useEffect, useRef, useCallback } from 'react'
import debounce from 'just-debounce-it'
import { useMovies } from './hooks/useMovies.js'

function useSearch () {
const [search, updateSearch] = useState('')
function useSearch() {
const [search, updateSearch] = useState(null)
const [error, setError] = useState(null)
const isFirstInput = useRef(true)

useEffect(() => {
if (isFirstInput.current) {
isFirstInput.current = search === ''
return
}

if (search === '') {
setError('No se puede buscar una película vacía')
return
}

if (search.match(/^\d+$/)) {
if (search?.match(/^\d+$/)) {
setError('No se puede buscar una película con un número')
return
}

if (search.length < 3) {
if (search?.length < 3) {
setError('La búsqueda debe tener al menos 3 caracteres')
return
}
Expand All @@ -36,18 +30,18 @@ function useSearch () {
return { search, updateSearch, error }
}

function App () {
function App() {
const [sort, setSort] = useState(false)

const { search, updateSearch, error } = useSearch()
const { movies, loading, getMovies } = useMovies({ search, sort })

const debouncedGetMovies = useCallback(
debounce(search => {
debounce((search) => {
console.log('search', search)
getMovies({ search })
}, 300)
, [getMovies]
}, 300),
[getMovies]
)

const handleSubmit = (event) => {
Expand All @@ -67,15 +61,18 @@ function App () {

return (
<div className='page'>

<header>
<h1>Buscador de películas</h1>
<form className='form' onSubmit={handleSubmit}>
<input
style={{
border: '1px solid transparent',
borderColor: error ? 'red' : 'transparent'
}} onChange={handleChange} value={search} name='query' placeholder='Avengers, Star Wars, The Matrix...'
borderColor: error ? 'red' : 'transparent',
}}
onChange={handleChange}
value={search ?? ''}
name='query'
placeholder='Avengers, Star Wars, The Matrix...'
/>
<input type='checkbox' onChange={handleSort} checked={sort} />
<button type='submit'>Buscar</button>
Expand All @@ -84,9 +81,7 @@ function App () {
</header>

<main>
{
loading ? <p>Cargando...</p> : <Movies movies={movies} />
}
{loading ? <p>Cargando...</p> : <Movies movies={movies} />}
</main>
</div>
)
Expand Down