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

first solution #1926

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
10 changes: 7 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import './App.scss';
import { useState } from 'react';
import { MoviesList } from './components/MoviesList';
import { NewMovie } from './components/NewMovie';
import { Movie } from './types/Movie';
import moviesFromServer from './api/movies.json';

export const App = () => {
export const App: React.FC = () => {
const [movies, setMovies] = useState<Movie[]>(moviesFromServer);

return (
<div className="page">
<div className="page-content">
<MoviesList movies={moviesFromServer} />
<MoviesList movies={movies} />
</div>
<div className="sidebar">
<NewMovie /* onAdd={(movie) => {}} */ />
<NewMovie onAdd={movie => setMovies([...movies, movie])} />
</div>
</div>
);
Expand Down
66 changes: 57 additions & 9 deletions src/components/NewMovie/NewMovie.tsx
Original file line number Diff line number Diff line change
@@ -1,45 +1,92 @@
import { useState } from 'react';
import { TextField } from '../TextField';
import { Movie } from '../../types/Movie';

export const NewMovie = () => {
interface Props {
onAdd: (movie: Movie) => void;
}

export const NewMovie: React.FC<Props> = ({ onAdd }) => {
// Increase the count after successful form submission
// to reset touched status of all the `Field`s
const [count] = useState(0);
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 reset = () => {
setTitle('');
setDescription('');
setImgUrl('');
setImdbUrl('');
setImdbId('');
};

const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();

onAdd({
title,
description,
imgUrl,
imdbUrl,
imdbId,
});

setCount(newCount => newCount + 1);

reset();
};

const isDisabled = !title || !imgUrl || !imdbUrl || !imdbId;

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

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

<TextField
name="description"
label="Description"
value=""
value={description}
onChange={setDescription}
/>

<TextField
name="imgUrl"
label="Image URL"
value=""
value={imgUrl}
onChange={setImgUrl}
required
/>

<TextField
name="imdbUrl"
label="Imdb URL"
value=""
value={imdbUrl}
onChange={setImdbUrl}
required
/>

<TextField
name="imdbId"
label="Imdb ID"
value=""
value={imdbId}
onChange={setImdbId}
required
/>

<div className="field is-grouped">
Expand All @@ -48,6 +95,7 @@ export const NewMovie = () => {
type="submit"
data-cy="submit-button"
className="button is-link"
disabled={isDisabled}
>
Add
</button>
Expand Down