-
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
add react movies list add form #2751
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,30 @@ | ||
import React, { useState } from 'react'; | ||
import './App.scss'; | ||
import { MoviesList } from './components/MoviesList'; | ||
import { NewMovie } from './components/NewMovie'; | ||
import moviesFromServer from './api/movies.json'; | ||
|
||
export const App = () => { | ||
interface Movie { | ||
title: string; | ||
imgUrl: string; | ||
imdbUrl: string; | ||
imdbId: string; | ||
description: string; | ||
} | ||
|
||
export const App: React.FC = () => { | ||
const [movies, setMovies] = useState<Movie[]>(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. Ensure 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import axios from 'axios'; | ||
|
||
const API_KEY = 'YOUR_OMDB_API_KEY'; // Замінити на ваш ключ API | ||
|
||
export async function searchMovie(query: string) { | ||
try { | ||
const response = await axios.get(`http://www.omdbapi.com/?apikey=${API_KEY}&t=${query}`); | ||
return response.data; | ||
} catch (error) { | ||
console.error('Error fetching movie data:', error); | ||
return null; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ export const MoviesList: React.FC<Props> = ({ movies }) => ( | |
))} | ||
</div> | ||
); | ||
export default MoviesList; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,158 @@ | ||
import { useState } from 'react'; | ||
import { TextField } from '../TextField'; | ||
|
||
export const NewMovie = () => { | ||
// Increase the count after successful form submission | ||
// to reset touched status of all the `Field`s | ||
const [count] = useState(0); | ||
interface Props { | ||
onAdd: (movie: { | ||
title: string; | ||
imgUrl: string; | ||
imdbUrl: string; | ||
imdbId: string; | ||
description: string; | ||
}) => void; | ||
} | ||
|
||
export const NewMovie: React.FC<Props> = ({ onAdd }) => { | ||
const [title, setTitle] = useState(''); | ||
const [imgUrl, setImgUrl] = useState(''); | ||
const [imdbUrl, setImdbUrl] = useState(''); | ||
const [imdbId, setImdbId] = useState(''); | ||
const [description, setDescription] = useState(''); | ||
const [errors, setErrors] = useState({ | ||
title: '', | ||
imgUrl: '', | ||
imdbUrl: '', | ||
imdbId: '', | ||
description: '', | ||
}); | ||
const [successMessage, setSuccessMessage] = useState(''); | ||
const clearForm = () => { | ||
setTitle(''); | ||
setImgUrl(''); | ||
setImdbUrl(''); | ||
setImdbId(''); | ||
setDescription(''); | ||
}; | ||
|
||
const handleSubmit = (event: React.FormEvent) => { | ||
event.preventDefault(); | ||
if (title && imgUrl && imdbUrl && imdbId) { | ||
if (typeof onAdd === 'function') { | ||
onAdd({ title, imgUrl, imdbUrl, imdbId, description }); | ||
setSuccessMessage('Movie added successfully!'); | ||
setTimeout(() => setSuccessMessage(''), 3000); | ||
clearForm(); | ||
} | ||
} | ||
}; | ||
|
||
const isValidUrl = (url: string) => { | ||
try { | ||
new URL(url); | ||
|
||
return true; | ||
} catch (_) { | ||
return false; | ||
} | ||
}; | ||
|
||
const isValidImdbUrl = (url: string) => | ||
url.startsWith('https://www.imdb.com/title/'); | ||
|
||
const isValidImdbId = (id: string) => /^tt\d+$/.test(id); | ||
|
||
const handleBlur = (field: string, value: string) => { | ||
if (!value.trim()) { | ||
setErrors(prev => ({ ...prev, [field]: `${field} is required` })); | ||
} else { | ||
if (field === 'imgUrl' && !isValidUrl(value)) { | ||
setErrors(prev => ({ ...prev, [field]: 'Please provide a valid URL' })); | ||
} else if (field === 'imdbUrl' && !isValidImdbUrl(value)) { | ||
setErrors(prev => ({ | ||
...prev, | ||
[field]: 'Please provide a valid IMDb URL', | ||
})); | ||
} else if (field === 'imdbId' && !isValidImdbId(value)) { | ||
setErrors(prev => ({ | ||
...prev, | ||
[field]: 'Please provide a valid IMDb ID', | ||
})); | ||
} else { | ||
setErrors(prev => ({ ...prev, [field]: '' })); | ||
} | ||
} | ||
}; | ||
|
||
return ( | ||
<form className="NewMovie" key={count}> | ||
<h2 className="title">Add a movie</h2> | ||
<form onSubmit={handleSubmit} data-cy="new-movie-form"> | ||
<h2>Add a movie</h2> | ||
|
||
<TextField | ||
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 |
||
name="title" | ||
label="Title" | ||
value="" | ||
onChange={() => {}} | ||
value={title} | ||
onChange={e => setTitle(e.target.value)} | ||
onBlur={e => handleBlur('title', e.target.value)} | ||
required | ||
data-cy="movie-title" | ||
/> | ||
|
||
<TextField | ||
name="description" | ||
label="Description" | ||
value={description} | ||
onChange={e => setDescription(e.target.value)} | ||
data-cy="movie-description" | ||
/> | ||
|
||
<TextField | ||
name="imgUrl" | ||
label="Image URL" | ||
value={imgUrl} | ||
onChange={e => setImgUrl(e.target.value)} | ||
onBlur={e => handleBlur('imgUrl', e.target.value)} | ||
required | ||
data-cy="movie-imgUrl" | ||
/> | ||
|
||
<TextField name="description" label="Description" value="" /> | ||
<TextField | ||
name="imdbUrl" | ||
label="Imdb URL" | ||
value={imdbUrl} | ||
onChange={e => setImdbUrl(e.target.value)} | ||
onBlur={e => handleBlur('imdbUrl', e.target.value)} | ||
required | ||
data-cy="movie-imdbUrl" | ||
/> | ||
|
||
<TextField name="imgUrl" label="Image URL" value="" /> | ||
<TextField | ||
name="imdbId" | ||
label="Imdb ID" | ||
value={imdbId} | ||
onChange={e => setImdbId(e.target.value)} | ||
onBlur={e => handleBlur('imdbId', e.target.value)} | ||
required | ||
data-cy="movie-imdbId" | ||
/> | ||
|
||
<TextField name="imdbUrl" label="Imdb URL" value="" /> | ||
<button | ||
type="submit" | ||
data-cy="submit-button" | ||
className="button is-link" | ||
disabled={!(title && imgUrl && imdbUrl && imdbId)} | ||
> | ||
Add | ||
</button> | ||
|
||
<TextField name="imdbId" label="Imdb ID" value="" /> | ||
{Object.entries(errors).map( | ||
([key, value]) => | ||
value && ( | ||
<p key={key} className="error-message"> | ||
{value} | ||
</p> | ||
), | ||
)} | ||
|
||
<div className="field is-grouped"> | ||
<div className="control"> | ||
<button | ||
type="submit" | ||
data-cy="submit-button" | ||
className="button is-link" | ||
> | ||
Add | ||
</button> | ||
</div> | ||
</div> | ||
{successMessage && <p className="success-message">{successMessage}</p>} | ||
</form> | ||
); | ||
}; |
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 is correctly pointing to a valid JSON file. Verify that the JSON structure matches theMovie
interface defined in this file.