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

solution #2735

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@ import './App.scss';
import { MoviesList } from './components/MoviesList';
import { NewMovie } from './components/NewMovie';
import moviesFromServer from './api/movies.json';
import { Movie } from './types/Movie';
import { useState } from 'react';

export const App = () => {
const [movies, setMovies] = useState<Movie[]>([...moviesFromServer]);

const handleAddMovie = (movie: Movie) => {
setMovies((prevMovies: Movie[]) => [...prevMovies, movie]);
};

return (
<div className="page">
<div className="page-content">
<MoviesList movies={moviesFromServer} />
<MoviesList movies={movies} />
</div>
<div className="sidebar">
<NewMovie /* onAdd={(movie) => {}} */ />
<NewMovie onAdd={handleAddMovie} />
</div>
</div>
);
Expand Down
83 changes: 74 additions & 9 deletions src/components/NewMovie/NewMovie.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,102 @@
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);

Choose a reason for hiding this comment

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

The count state is initialized but never updated, which means the key prop on the form will not change. Consider using setCount to update the count after form submission if you intend to reset the form fields' touched status.

const [title, setTitle] = useState('');
const [description, setDescription] = useState('');
const [imgUrl, setImgUrl] = useState('');
const [imdbUrl, setImdbUrl] = useState('');
const [imdbId, setImdbId] = useState('');

const handleSubmit = (event: React.FormEvent) => {
event.preventDefault();
if (!title || !imgUrl || !imdbUrl || !imdbId) {
return;
}

const newMovie: Movie = {
title,
description,
imgUrl,
imdbUrl,
imdbId,
};

if (title) {

Choose a reason for hiding this comment

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

The condition if (title) is unnecessary here since the form submission already checks for required fields. You can remove this condition to simplify the code.

setTitle('');
setDescription('');
setImgUrl('');
setImdbUrl('');
setImdbId('');
}

onAdd(newMovie);
};

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

<TextField
name="title"
label="Title"
value=""
onChange={() => {}}
value={title}
onChange={setTitle}
required
/>

<TextField name="description" label="Description" value="" />
<TextField
name="description"
label="Description"
value={description}
onChange={setDescription}
required
/>

<TextField name="imgUrl" label="Image URL" value="" />
<TextField
name="imgUrl"
label="Image URL"
value={imgUrl}
onChange={setImgUrl}
required
/>

<TextField name="imdbUrl" label="Imdb URL" value="" />
<TextField
name="imdbUrl"
label="Imdb URL"
value={imdbUrl}
onChange={setImdbUrl}
required
/>

<TextField name="imdbId" label="Imdb ID" value="" />
<TextField
name="imdbId"
label="Imdb ID"
value={imdbId}
onChange={setImdbId}
required
/>

<div className="field is-grouped">
<div className="control">
<button
type="submit"
data-cy="submit-button"
className="button is-link"
onClick={() => {}}

Choose a reason for hiding this comment

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

The onClick handler for the submit button is an empty function. If it's not needed, consider removing it to clean up the code.

disabled={
!title && !description && !imgUrl && !imdbUrl && !imdbId
? true
: false
}

Choose a reason for hiding this comment

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

The disabled attribute logic for the button is incorrect. It should disable the button if any of the required fields are empty, not all. Change the condition to disable the button when any required field is missing.

>
Add
</button>
Expand Down