diff --git a/src/App.scss b/src/App.scss index 64693840b..2a0875434 100644 --- a/src/App.scss +++ b/src/App.scss @@ -1,6 +1,7 @@ iframe { display: none; } + .page { display: grid; grid-template: auto / 1fr 400px; diff --git a/src/App.tsx b/src/App.tsx index 63405ffe3..4eb41b39f 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -6,7 +6,7 @@ import moviesFromServer from './api/movies.json'; import { Movie } from './types/Movie'; export const App = () => { - const [movieList, setMovieList] = useState([...moviesFromServer]); + const [movieList, setMovieList] = useState(moviesFromServer); const handleAddMovie = (newMovie: Movie) => { setMovieList((prevMovies) => [...prevMovies, newMovie]); diff --git a/src/components/NewMovie/NewMovie.tsx b/src/components/NewMovie/NewMovie.tsx index 9481cf546..8402273aa 100644 --- a/src/components/NewMovie/NewMovie.tsx +++ b/src/components/NewMovie/NewMovie.tsx @@ -25,12 +25,6 @@ export const NewMovie: React.FC = ({ onAdd }) => { imdbId: '', }); - // const [title, setTitle] = useState(''); - // const [description, setDescription] = useState(''); - // const [imgUrl, setImgUrl] = useState(''); - // const [imdbUrl, setimdbUrl] = useState(''); - // const [imdbId, setImdbId] = useState(''); - const urlValidator = (urlString: string): boolean => { // eslint-disable-next-line max-len const pattern = /^((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=+$,\w]+@)?[A-Za-z0-9.-]+|(?:www\.|[-;:&=+$,\w]+@)[A-Za-z0-9.-]+)((?:\/[+~%/.\w-_]*)?\??(?:[-+=&;%@,.\w_]*)#?(?:[,.!/\\\w]*))?)$/; @@ -42,6 +36,10 @@ export const NewMovie: React.FC = ({ onAdd }) => { return true; }; + const isDisabled = !newMovie.title.trim() + || !urlValidator(newMovie.imgUrl) || !urlValidator(newMovie.imdbUrl) + || !newMovie.imdbId.trim(); + function addMovie(event: React.FormEvent) { event.preventDefault(); @@ -82,20 +80,20 @@ export const NewMovie: React.FC = ({ onAdd }) => { name="title" label="Title" value={newMovie.title} - onChange={e => handleChange(e)} + onChange={handleChange} required /> handleChange(e)} + onChange={handleChange} /> handleChange(e)} + onChange={handleChange} checkUrl={urlValidator} required /> @@ -103,7 +101,7 @@ export const NewMovie: React.FC = ({ onAdd }) => { handleChange(e)} + onChange={handleChange} checkUrl={urlValidator} required /> @@ -111,7 +109,7 @@ export const NewMovie: React.FC = ({ onAdd }) => { handleChange(e)} + onChange={handleChange} required /> @@ -121,10 +119,7 @@ export const NewMovie: React.FC = ({ onAdd }) => { type="submit" data-cy="submit-button" className="button is-link" - disabled={!newMovie.title - || !urlValidator(newMovie.imgUrl) - || !urlValidator(newMovie.imdbUrl) - || !newMovie.imdbId} + disabled={isDisabled} > Add diff --git a/src/components/TextField/TextField.tsx b/src/components/TextField/TextField.tsx index 6f4a70d40..538a47da1 100644 --- a/src/components/TextField/TextField.tsx +++ b/src/components/TextField/TextField.tsx @@ -31,7 +31,7 @@ export const TextField: React.FC = ({ // To show errors only if the field was touched (onBlur) const [touched, setTouched] = useState(false); - const hasError = touched && required && !value; + const hasError = touched && required && !value.trim(); return (