Skip to content

Commit

Permalink
solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilya-Ilchuk committed Dec 6, 2024
1 parent f27ae36 commit 2208610
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 13 deletions.
12 changes: 10 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@ import './App.scss';
import { MoviesList } from './components/MoviesList';
import { NewMovie } from './components/NewMovie';
import moviesFromServer from './api/movies.json';
import { useState } from 'react';
import { Movie } from './types/Movie';

export const App = () => {
const [movies, setNewMovie] = useState<Movie[]>(moviesFromServer);

const addNewMovie = (movie: Movie) => {
setNewMovie(currentMovies => [...currentMovies, movie]);
};

return (
<div className="page">
<div className="page-content">
<MoviesList movies={moviesFromServer} />
<MoviesList movies={movies} />
</div>
<div className="sidebar">
<NewMovie /* onAdd={(movie) => {}} */ />
<NewMovie onAdd={addNewMovie} />
</div>
</div>
);
Expand Down
82 changes: 71 additions & 11 deletions src/components/NewMovie/NewMovie.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,97 @@
import { useState } from 'react';
import { TextField } from '../TextField';
import { Movie } from '../../types/Movie';

export const NewMovie = () => {
// Increase the count after successful form submission
// to reset touched status of all the `Field`s
const [count] = useState(0);
interface NewMovieProps {
onAdd?: (movie: Movie) => void;
}

export const NewMovie: React.FC<NewMovieProps> = ({ onAdd }) => {
const [count, setCount] = useState(0);
const [title, setTitle] = useState('');
const [description, setDescription] = useState('');
const [imgUrl, setImgUrl] = useState('');
const [imdbUrl, setImdbUrl] = useState('');
const [imdbId, setImdbId] = useState('');

const areAllQueries: boolean =
title.trim() !== '' &&
imgUrl.trim() !== '' &&
imdbUrl.trim() !== '' &&
imdbId.trim() !== '';

const addNewMovie = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();

if (areAllQueries && onAdd) {
onAdd({
title: title.trim(),
description: description.trim(),
imgUrl: imgUrl.trim(),
imdbUrl: imdbUrl.trim(),
imdbId: imdbId.trim(),
});
}

setTitle('');
setDescription('');
setImgUrl('');
setImdbUrl('');
setImdbId('');

setCount(count + 1);
};

return (
<form className="NewMovie" key={count}>
<form className="NewMovie" key={count} onSubmit={addNewMovie}>
<h2 className="title">Add a movie</h2>

<TextField
name="title"
label="Title"
value=""
onChange={() => {}}
value={title}
onChange={(e: string) => setTitle(e)}
required
/>

<TextField name="description" label="Description" value="" />
<TextField
name="description"
label="Description"
value={description}
onChange={(e: string) => setDescription(e)}
/>

<TextField name="imgUrl" label="Image URL" value="" />
<TextField
name="imgUrl"
label="Image URL"
value={imgUrl}
onChange={(e: string) => setImgUrl(e)}
required
/>

<TextField name="imdbUrl" label="Imdb URL" value="" />
<TextField
name="imdbUrl"
label="Imdb URL"
value={imdbUrl}
onChange={(e: string) => setImdbUrl(e)}
required
/>

<TextField name="imdbId" label="Imdb ID" value="" />
<TextField
name="imdbId"
label="Imdb ID"
value={imdbId}
onChange={(e: string) => setImdbId(e)}
required
/>

<div className="field is-grouped">
<div className="control">
<button
type="submit"
data-cy="submit-button"
className="button is-link"
disabled={!areAllQueries}
>
Add
</button>
Expand Down

0 comments on commit 2208610

Please sign in to comment.