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 #2131

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
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
14 changes: 12 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import './App.scss';
import { useState } from 'react';
import { MoviesList } from './components/MoviesList';
import { NewMovie } from './components/NewMovie';
import moviesFromServer from './api/movies.json';
import { Movie } from './types/Movie';

const initMovies = [...moviesFromServer];

export const App = () => {
const [movies, setMovies] = useState(initMovies);

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

return (
<div className="page">
<div className="page-content">
<MoviesList movies={moviesFromServer} />
<MoviesList movies={movies} />
</div>
<div className="sidebar">
<NewMovie /* onAdd={(movie) => {}} */ />
<NewMovie onAdd={onAdd} />
</div>
</div>
);
Expand Down
80 changes: 70 additions & 10 deletions src/components/NewMovie/NewMovie.tsx
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: '',
});

Comment on lines +14 to +20

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

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

const initialMovieState = {
  title: '',
  description: '',
  imgUrl: '',
  imdbUrl: '',
  imdbId: ''
}
Suggested change
const [newMovie, setNewMovie] = useState({
title: '',
description: '',
imgUrl: '',
imdbUrl: '',
imdbId: '',
});
const [newMovie, setNewMovie] = useState(initialMovieState);

const ifAllFieldsFilled = newMovie.title.trim() && newMovie.imgUrl.trim()
&& newMovie.imdbUrl.trim() && newMovie.imdbId.trim();

Choose a reason for hiding this comment

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

let's rename this var to something like isAllFieldsFilled
And read this article about naming boolean variables)


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">
Expand All @@ -48,6 +107,7 @@ export const NewMovie = () => {
type="submit"
data-cy="submit-button"
className="button is-link"
disabled={!ifAllFieldsFilled}
>
Add
</button>
Expand Down
6 changes: 4 additions & 2 deletions src/components/TextField/TextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type Props = {
label?: string,
placeholder?: string,
required?: boolean,
onChange?: (newValue: string) => void,
onChange?: (e: React.ChangeEvent<HTMLInputElement>) => void,
};

function getRandomDigits() {
Expand All @@ -23,6 +23,7 @@ export const TextField: React.FC<Props> = ({
placeholder = `Enter ${label}`,
required = false,
onChange = () => {},
// isValidUrl = () => {},

Choose a reason for hiding this comment

The 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()}`);
Expand All @@ -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>

Expand Down
Loading