Skip to content

Commit

Permalink
fix count
Browse files Browse the repository at this point in the history
  • Loading branch information
yepolotn1ak committed Jan 22, 2024
1 parent a43fbc9 commit d8c4642
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions src/components/NewMovie/NewMovie.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ type Props = {
};

export const NewMovie: React.FC<Props> = ({ onAdd }) => {
const [count, setCount] = useState(0);
const [submitDisabled, setSubmitDisabled] = useState(true);
let count = 0;

const [newMovie, setNewMovie] = useState<Movie>({
title: '',
Expand All @@ -34,17 +34,14 @@ export const NewMovie: React.FC<Props> = ({ onAdd }) => {
[name]: value,
}));

setSubmitDisabled(() => {
const trimmedTitle = name === 'title' ? value.trim() : title.trim();
const trimmedImgUrl = name === 'imgUrl' ? value.trim() : imgUrl.trim();
const trimmedImdbUrl = name === 'imdbUrl' ? value.trim() : imdbUrl.trim();
const trimmedImdbId = name === 'imdbId' ? value.trim() : imdbId.trim();
const trimmedTitle = name === 'title' ? value.trim() : title.trim();
const trimmedImgUrl = name === 'imgUrl' ? value.trim() : imgUrl.trim();
const trimmedImdbUrl = name === 'imdbUrl' ? value.trim() : imdbUrl.trim();
const trimmedImdbId = name === 'imdbId' ? value.trim() : imdbId.trim();

return !trimmedTitle
|| !trimmedImgUrl
|| !trimmedImdbUrl
|| !trimmedImdbId;
});
setSubmitDisabled(
!trimmedTitle || !trimmedImgUrl || !trimmedImdbUrl || !trimmedImdbId,
);
};

const handleSubmit = (event: React.FormEvent) => {
Expand All @@ -67,15 +64,11 @@ export const NewMovie: React.FC<Props> = ({ onAdd }) => {
});

setSubmitDisabled(true);
setCount((prevCount) => prevCount + 1);
count += 1;
};

return (
<form
key={count}
className="NewMovie"
onSubmit={handleSubmit}
>
<form key={count} className="NewMovie" onSubmit={handleSubmit}>
<h2 className="title">Add a movie</h2>

<TextField
Expand Down

0 comments on commit d8c4642

Please sign in to comment.