-
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
solution #2131
Open
Mariana-VV
wants to merge
5
commits into
mate-academy:master
Choose a base branch
from
Mariana-VV:develop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
solution #2131
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,104 @@ | ||
import { useState } from 'react'; | ||
import React, { useState } from 'react'; | ||
import { TextField } from '../TextField'; | ||
import { Movie } from '../../types/Movie'; | ||
|
||
export const NewMovie = () => { | ||
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 [count, setCount] = useState(0); | ||
|
||
const [newMovie, setNewMovie] = useState({ | ||
title: '', | ||
description: '', | ||
imgUrl: '', | ||
imdbUrl: '', | ||
imdbId: '', | ||
}); | ||
|
||
const ifAllFieldsFilled = newMovie.title.trim() && newMovie.imgUrl.trim() | ||
&& newMovie.imdbUrl.trim() && newMovie.imdbId.trim(); | ||
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. let's rename this var to something like isAllFieldsFilled |
||
|
||
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
const { name, value } = e.target; | ||
|
||
setNewMovie((prevMovie) => ({ ...prevMovie, [name]: value })); | ||
}; | ||
|
||
const reset = () => { | ||
newMovie.title = ''; | ||
newMovie.description = ''; | ||
newMovie.imgUrl = ''; | ||
newMovie.imdbUrl = ''; | ||
newMovie.imdbId = ''; | ||
}; | ||
|
||
const handleFormOnSubmit = (event: React.FormEvent) => { | ||
event.preventDefault(); | ||
|
||
setCount(counter => counter + 1); | ||
|
||
if (!ifAllFieldsFilled) { | ||
return; | ||
} | ||
|
||
onAdd(newMovie); | ||
reset(); | ||
}; | ||
|
||
return ( | ||
<form className="NewMovie" key={count}> | ||
<form | ||
className="NewMovie" | ||
key={count} | ||
onSubmit={handleFormOnSubmit} | ||
> | ||
<h2 className="title">Add a movie</h2> | ||
|
||
<TextField | ||
name="title" | ||
label="Title" | ||
value="" | ||
onChange={() => {}} | ||
value={newMovie.title} | ||
onChange={handleChange} | ||
required | ||
/> | ||
|
||
<TextField | ||
name="description" | ||
label="Description" | ||
value="" | ||
value={newMovie.description} | ||
onChange={handleChange} | ||
/> | ||
|
||
<TextField | ||
name="imgUrl" | ||
label="Image URL" | ||
value="" | ||
value={newMovie.imgUrl} | ||
onChange={handleChange} | ||
required | ||
// isValidUrl={isValidUrl(imgUrl)} | ||
|
||
/> | ||
|
||
<TextField | ||
name="imdbUrl" | ||
label="Imdb URL" | ||
value="" | ||
value={newMovie.imdbUrl} | ||
onChange={handleChange} | ||
required | ||
// isValidUrl={isValidUrl(imgUrl)} | ||
|
||
/> | ||
|
||
<TextField | ||
name="imdbId" | ||
label="Imdb ID" | ||
value="" | ||
value={newMovie.imdbId} | ||
onChange={handleChange} | ||
required | ||
|
||
/> | ||
|
||
<div className="field is-grouped"> | ||
|
@@ -48,6 +107,7 @@ export const NewMovie = () => { | |
type="submit" | ||
data-cy="submit-button" | ||
className="button is-link" | ||
disabled={!ifAllFieldsFilled} | ||
> | ||
Add | ||
</button> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ type Props = { | |
label?: string, | ||
placeholder?: string, | ||
required?: boolean, | ||
onChange?: (newValue: string) => void, | ||
onChange?: (e: React.ChangeEvent<HTMLInputElement>) => void, | ||
}; | ||
|
||
function getRandomDigits() { | ||
|
@@ -23,6 +23,7 @@ export const TextField: React.FC<Props> = ({ | |
placeholder = `Enter ${label}`, | ||
required = false, | ||
onChange = () => {}, | ||
// isValidUrl = () => {}, | ||
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. remove commented code |
||
}) => { | ||
// generage a unique id once on component load | ||
const [id] = useState(() => `${name}-${getRandomDigits()}`); | ||
|
@@ -47,8 +48,9 @@ export const TextField: React.FC<Props> = ({ | |
})} | ||
placeholder={placeholder} | ||
value={value} | ||
onChange={event => onChange(event.target.value)} | ||
onChange={(event) => onChange(event)} | ||
onBlur={() => setTouched(true)} | ||
|
||
/> | ||
</div> | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
move this obj to a variable, let's say InitialValues to be able to reuse it, for example in reset method
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.
isn't fixed. Create a variable outside the component and reuse it