-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
reactMoviesListAddFormCopy1 #2680
base: master
Are you sure you want to change the base?
Changes from 1 commit
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,31 @@ import './App.scss'; | |
import { MoviesList } from './components/MoviesList'; | ||
import { NewMovie } from './components/NewMovie'; | ||
import moviesFromServer from './api/movies.json'; | ||
import React, { useState, useCallback } from 'react'; | ||
import { Movie } from './types/Movie'; | ||
|
||
export const App = () => { | ||
const visibleMovies = [...moviesFromServer]; | ||
const [movies, setMovies] = useState<Movie[]>(visibleMovies); | ||
|
||
const addMovie = useCallback((newMovie: Movie) => { | ||
setMovies(currentMovies => { | ||
return [...currentMovies, newMovie]; | ||
}); | ||
}, []); | ||
|
||
return ( | ||
<div className="page"> | ||
<div className="page-content"> | ||
<MoviesList movies={moviesFromServer} /> | ||
<MoviesList movies={movies} /> | ||
</div> | ||
<div className="sidebar"> | ||
<NewMovie /* onAdd={(movie) => {}} */ /> | ||
<NewMovie | ||
onAdd={addMovie} | ||
movies={movies} | ||
errorMessage={''} | ||
titleErrorMessage={''} | ||
Comment on lines
+23
to
+27
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
Comment on lines
+26
to
+27
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 |
||
/> | ||
</div> | ||
</div> | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,9 @@ export const MovieCard: React.FC<Props> = ({ movie }) => ( | |
<p className="title is-8">{movie.title}</p> | ||
</div> | ||
</div> | ||
|
||
<div className="media-content"> | ||
<p className="title is-8"></p> | ||
</div> | ||
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 second |
||
<div className="content"> | ||
{movie.description} | ||
<br /> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,126 @@ | ||
import { useState } from 'react'; | ||
import { TextField } from '../TextField'; | ||
import React from 'react'; | ||
import { Movie } from '../../types/Movie'; | ||
|
||
export const NewMovie = () => { | ||
type Props = { | ||
onAdd: (newMovie: Movie) => void; | ||
//getMaxId: (movies: Movie[]) => number | (() => number); | ||
movies: Movie[]; | ||
errorMessage: string; | ||
titleErrorMessage: string; | ||
}; | ||
function getMaxId(movies: Movie[]) { | ||
return Math.max(...movies.map(m => movies.indexOf(m))); | ||
} | ||
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 |
||
|
||
export const NewMovie: React.FC<Props> = ({ onAdd, movies }) => { | ||
// Increase the count after successful form submission | ||
// to reset touched status of all the `Field`s | ||
const [count] = useState(0); | ||
|
||
const [count, setCount] = useState(getMaxId(movies) + 1); | ||
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 initial count is set using |
||
|
||
const [title, setTitle] = useState(''); | ||
const [description, setDescription] = useState(''); | ||
const [imgUrl, setImgUrl] = useState(''); | ||
const [imdbUrl, setImdbUrl] = useState(''); | ||
const [imdbId, setImdbId] = useState(''); | ||
const [errorMessage, setErrorMessage] = useState(''); | ||
const [titleErrorMessage, setTitleErrorMessage] = useState(''); | ||
Comment on lines
+25
to
+26
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 error messages are cleared when the form is reset. This will prevent errors from persisting after clearing the form. |
||
const pattern = | ||
// eslint-disable-next-line max-len | ||
/^((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=+$,\w]+@)?[A-Za-z0-9.-]+|(?:www\.|[-;:&=+$,\w]+@)[A-Za-z0-9.-]+)((?:\/[+~%/.\w-_]*)?\??(?:[-+=&;%@,.\w_]*)#?(?:[,.!/\\\w]*))?)$/; | ||
const reset = () => { | ||
setTitle(''); | ||
setImgUrl(''); | ||
setImdbUrl(''); | ||
setImdbId(''); | ||
setDescription(''); | ||
setTitleErrorMessage(''); | ||
setErrorMessage(''); | ||
}; | ||
|
||
const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => { | ||
event.preventDefault(); | ||
const movie = { | ||
title: title, | ||
description: description, | ||
imgUrl: imgUrl, | ||
imdbUrl: imdbUrl, | ||
imdbId: imdbId, | ||
count: count, | ||
}; | ||
|
||
//if (!title || !imdbUrl || !imgUrl) { | ||
// return; | ||
//} | ||
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. This block of code is commented out but seems to be a necessary validation check. Consider uncommenting and using it to ensure that all required fields are filled before submission. |
||
if (!title) { | ||
setTitleErrorMessage('should have some text'); | ||
} else if (title.length < 5) { | ||
setTitleErrorMessage('should have at least 5 chars'); | ||
} | ||
|
||
if (!pattern.test(imgUrl) || !pattern.test(imdbUrl)) { | ||
setErrorMessage('not valid'); | ||
|
||
return; | ||
} | ||
|
||
if (!title || !imdbUrl || !imgUrl || title.length < 5) { | ||
return; | ||
} | ||
Comment on lines
+69
to
+71
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. This validation check is redundant because similar checks are already performed earlier. Consider removing this block to avoid unnecessary code.
Comment on lines
+69
to
+71
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 submit button should be disabled until all required fields are filled and trimmed. Currently, this logic is not implemented, allowing form submission even when fields are incomplete. |
||
|
||
onAdd(movie); | ||
setCount(currentCount => currentCount + 1); | ||
reset(); | ||
}; | ||
|
||
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 | ||
errorMessage={titleErrorMessage} | ||
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 |
||
/> | ||
|
||
<TextField name="description" label="Description" value="" /> | ||
<TextField | ||
name="description" | ||
label="Description" | ||
value={description} | ||
onChange={setDescription} | ||
errorMessage="" | ||
/> | ||
|
||
<TextField name="imgUrl" label="Image URL" value="" /> | ||
<TextField | ||
name="imgUrl" | ||
label="Image URL" | ||
value={imgUrl} | ||
onChange={setImgUrl} | ||
required | ||
errorMessage={errorMessage} | ||
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 |
||
/> | ||
|
||
<TextField name="imdbUrl" label="Imdb URL" value="" /> | ||
<TextField | ||
name="imdbUrl" | ||
label="Imdb URL" | ||
value={imdbUrl} | ||
onChange={setImdbUrl} | ||
required | ||
errorMessage={errorMessage} | ||
/> | ||
|
||
<TextField name="imdbId" label="Imdb ID" value="" /> | ||
<TextField | ||
name="imdbId" | ||
label="Imdb ID" | ||
value={imdbId} | ||
onChange={setImdbId} | ||
errorMessage="" | ||
/> | ||
|
||
<div className="field is-grouped"> | ||
<div className="control"> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,8 @@ type Props = { | |
label?: string; | ||
placeholder?: string; | ||
required?: boolean; | ||
errorMessage: string; | ||
|
||
onChange?: (newValue: string) => void; | ||
}; | ||
|
||
|
@@ -18,6 +20,7 @@ export const TextField: React.FC<Props> = ({ | |
name, | ||
value, | ||
label = name, | ||
errorMessage, | ||
placeholder = `Enter ${label}`, | ||
required = false, | ||
onChange = () => {}, | ||
|
@@ -27,14 +30,14 @@ export const TextField: React.FC<Props> = ({ | |
|
||
// To show errors only if the field was touched (onBlur) | ||
const [touched, setTouched] = useState(false); | ||
const hasError = touched && required && !value; | ||
|
||
let hasError = touched && required && !value; | ||
|
||
return ( | ||
<div className="field"> | ||
<label className="label" htmlFor={id}> | ||
{label} | ||
</label> | ||
|
||
<div className="control"> | ||
<input | ||
type="text" | ||
|
@@ -49,8 +52,11 @@ export const TextField: React.FC<Props> = ({ | |
onBlur={() => setTouched(true)} | ||
/> | ||
</div> | ||
|
||
{errorMessage && (hasError = false)} | ||
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 line 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 logic here resets |
||
{hasError && <p className="help is-danger">{`${label} is required`}</p>} | ||
{errorMessage && ( | ||
<p className="help is-danger">{`${label} ${errorMessage}`}</p> | ||
)} | ||
</div> | ||
); | ||
}; |
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.
You don't need to create a copy of the
moviesFromServer
array using the spread operator here. According to the checklist, if you are using a non-mutating array method, you don't need to create a copy of the array. You can directly usemoviesFromServer
.