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

Add solution #1880

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

JaneShavrukova
Copy link

Comment on lines 11 to 15
const [title, setTitle] = useState('');
const [description, setDescription] = useState('');
const [imgUrl, setImgUrl] = useState('');
const [imdbUrl, setImdbUrl] = useState('');
const [imdbId, setImdbId] = useState('');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not advisable to generate such a large number of states when we only have one instance (movie).
It'd be much better to utilize a single state with an object inside.

  1. Create a variable outside the Component
const initialMovieState = {
  title: '',
  description: '',
  imgUrl: '',
  imdbUrl: '',
  imdbId: ''
}
  1. Use one state for movie fields
  2. Pass initialMovieState object when you invoke reset function
  3. Whenever you need to change value of any field you should pass two arguments (key, value) and then just change a value of a movie object by key. In this case you will reuse one handler instead of handlers for each input
    Example:
const handleInputChange = (key, value) => {
  setMovie(prevInputs => ({...prevInputs, [key]: value}))
}
Suggested change
const [title, setTitle] = useState('');
const [description, setDescription] = useState('');
const [imgUrl, setImgUrl] = useState('');
const [imdbUrl, setImdbUrl] = useState('');
const [imdbId, setImdbId] = useState('');
const [movie, setMovie] = useState(initialMovieState);

Comment on lines 17 to 23
const resetForm = () => {
setTitle('');
setDescription('');
setImgUrl('');
setImdbUrl('');
setImdbId('');
};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const resetForm = () => {
setTitle('');
setDescription('');
setImgUrl('');
setImdbUrl('');
setImdbId('');
};
const resetForm = () => {
setMovie(initialMovieState);
};

resetForm();
};

const isSubmitDisabled = !(title

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I shouldn't be allowed to write only spaces
Screenshot 2023-09-30 at 09 50 52

Copy link

@plodnikate plodnikate left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants