-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
make task #2706
base: master
Are you sure you want to change the base?
make task #2706
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,15 +2,30 @@ import './App.scss'; | |
import { MoviesList } from './components/MoviesList'; | ||
import { NewMovie } from './components/NewMovie'; | ||
import moviesFromServer from './api/movies.json'; | ||
import { useState } from 'react'; | ||
|
||
type Movie = { | ||
title: string; | ||
description: string; | ||
imgUrl: string; | ||
imdbUrl: string; | ||
imdbId: string; | ||
}; | ||
|
||
export const App = () => { | ||
const [movies, setMovies] = useState(moviesFromServer); | ||
|
||
const handleAddMovie = (newMovie: Movie) => { | ||
setMovies(prevMovies => [...prevMovies, newMovie]); | ||
}; | ||
|
||
return ( | ||
<div className="page"> | ||
<div className="page-content"> | ||
<MoviesList movies={moviesFromServer} /> | ||
<MoviesList movies={movies} /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Verify that the |
||
</div> | ||
<div className="sidebar"> | ||
<NewMovie /* onAdd={(movie) => {}} */ /> | ||
<NewMovie onAdd={handleAddMovie} /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure that the |
||
</div> | ||
</div> | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,126 @@ | ||
import { useState } from 'react'; | ||
import { TextField } from '../TextField'; | ||
|
||
export const NewMovie = () => { | ||
type Movie = { | ||
title: string; | ||
description: string; | ||
imgUrl: string; | ||
imdbUrl: string; | ||
imdbId: string; | ||
}; | ||
|
||
type 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 [title, setTitle] = useState(''); | ||
const [description, setDescription] = useState(''); | ||
const [image, setImage] = useState(''); | ||
const [imdbUrl, setImdbUrl] = useState(''); | ||
const [imdbId, setImdbId] = useState(''); | ||
|
||
const ifFormValid = () => { | ||
return ( | ||
title.trim() !== '' && | ||
image.trim() !== '' && | ||
imdbUrl.trim() !== '' && | ||
imdbId.trim() !== '' | ||
); | ||
}; | ||
|
||
const handleTitleChange = (value: string) => { | ||
setTitle(value); // Тепер отримуємо тільки значення, а не event | ||
}; | ||
|
||
const handleDescriptionChange = (value: string) => { | ||
setDescription(value); | ||
}; | ||
|
||
const handleImageChange = (value: string) => { | ||
setImage(value); | ||
}; | ||
|
||
const handleImdbUrlChange = (value: string) => { | ||
setImdbUrl(value); | ||
}; | ||
|
||
const handleImdbIdChange = (value: string) => { | ||
setImdbId(value); | ||
}; | ||
|
||
const handleSubmit = (event: React.FormEvent) => { | ||
event.preventDefault(); | ||
|
||
onAdd({ | ||
title, | ||
description, | ||
imgUrl: image, | ||
imdbUrl, | ||
imdbId, | ||
}); | ||
|
||
setTitle(''); | ||
setDescription(''); | ||
setImage(''); | ||
setImdbUrl(''); | ||
setImdbId(''); | ||
}; | ||
|
||
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={handleTitleChange} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure that the |
||
required | ||
/> | ||
|
||
<TextField name="description" label="Description" value="" /> | ||
<TextField | ||
name="description" | ||
label="Description" | ||
value={description} | ||
onChange={handleDescriptionChange} | ||
/> | ||
|
||
<TextField name="imgUrl" label="Image URL" value="" /> | ||
<TextField | ||
name="imgUrl" | ||
label="Image URL" | ||
value={image} | ||
onChange={handleImageChange} | ||
required | ||
/> | ||
|
||
<TextField name="imdbUrl" label="Imdb URL" value="" /> | ||
<TextField | ||
name="imdbUrl" | ||
label="Imdb URL" | ||
value={imdbUrl} | ||
onChange={handleImdbUrlChange} | ||
required | ||
/> | ||
|
||
<TextField name="imdbId" label="Imdb ID" value="" /> | ||
<TextField | ||
name="imdbId" | ||
label="Imdb ID" | ||
value={imdbId} | ||
onChange={handleImdbIdChange} | ||
required | ||
/> | ||
|
||
<div className="field is-grouped"> | ||
<div className="control"> | ||
<button | ||
type="submit" | ||
data-cy="submit-button" | ||
className="button is-link" | ||
disabled={!ifFormValid()} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
> | ||
Add | ||
</button> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure that the
moviesFromServer
import on line 4 is correctly pointing to the JSON file and that the file exists. This is crucial for initializing the state with the movie data.