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

react_movies-list-add-form #1873

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
13 changes: 11 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
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';

export const App = () => {
const [moviesServer, setMoviesServer] = useState<Movie[]>(moviesFromServer);
const onAdd = (newmovie:Movie) => {
setMoviesServer([...moviesServer, newmovie]);

Choose a reason for hiding this comment

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

Suggested change
setMoviesServer([...moviesServer, newmovie]);
setMoviesServer((moviesServer) => [...moviesServer, newmovie]);

Consider using a callback here to avoid bugs

};

return (
<div className="page">
<div className="page-content">
<MoviesList movies={moviesFromServer} />
<MoviesList movies={moviesServer} />
</div>
<div className="sidebar">
<NewMovie /* onAdd={(movie) => {}} */ />
<NewMovie
addMovie={onAdd}
/>
</div>
</div>
);
Expand Down
75 changes: 66 additions & 9 deletions src/components/NewMovie/NewMovie.tsx
Original file line number Diff line number Diff line change
@@ -1,50 +1,107 @@
import { useState } from 'react';
import { TextField } from '../TextField';
import { Movie } from '../../types/Movie';

export const NewMovie = () => {
type Props = {
addMovie: (movie: Movie) => void
};
export const NewMovie:React.FC<Props> = ({ addMovie }) => {
// 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 [nameValue, setNameValue] = useState('');
// const [descriptionValue, setDescriptionValue] = useState('');
// const [img, setImg] = useState('');
// const [imdbUrlValue, setImdbUrlValue] = useState('');
// const [imdbIdValue, setImdbIdValue] = useState('');
const [movi, setMovie] = useState<Movie>({
title: '',
description: '',
imgUrl: '',
imdbUrl: '',
imdbId: '',
});

const buttonDisablet = () => {
if (movi.title.trim()
&& movi.imdbId.trim()
&& movi.imdbUrl.trim()
&& movi.imgUrl.trim()) {
return false;
}

return true;
};

const resetFild = () => {
setMovie({
...movi,
title: '',
description: '',
imgUrl: '',
imdbUrl: '',
imdbId: '',
});
};

const handleSubmit = (event: React.FormEvent) => {
event.preventDefault();
addMovie(movi);
setCount(count + 1);
resetFild();
};

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

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

<TextField
name="description"
label="Description"
value=""
value={movi}
onChange={setMovie}
/>

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

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

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

<div className="field is-grouped">
<div className="control">
<button
disabled={buttonDisablet()}
type="submit"
data-cy="submit-button"
className="button is-link"
Expand Down
18 changes: 12 additions & 6 deletions src/components/TextField/TextField.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import classNames from 'classnames';
import React, { useState } from 'react';
import { Movie } from '../../types/Movie';

type Props = {
name: string,
value: string,
name: keyof Movie,
value: Movie,
label?: string,
placeholder?: string,
required?: boolean,
onChange?: (newValue: string) => void,
onChange?: (newValue: Movie) => void,
};

function getRandomDigits() {
Expand All @@ -29,7 +30,7 @@ export const TextField: React.FC<Props> = ({

// To show errors only if the field was touched (onBlur)
const [touched, setTouched] = useState(false);
const hasError = touched && required && !value;
const hasError = touched && required && !value[name];

return (
<div className="field">
Expand All @@ -46,8 +47,13 @@ export const TextField: React.FC<Props> = ({
'is-danger': hasError,
})}
placeholder={placeholder}
value={value}
onChange={event => onChange(event.target.value)}
value={value[name]}
onChange={(event: React.ChangeEvent<HTMLInputElement>) => {
onChange({
...value,
[name]: event.target.value,
});
}}
onBlur={() => setTouched(true)}
/>
</div>
Expand Down