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

task done #2101

Open
wants to merge 4 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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ You have the `App` with the `MoviesList` and `NewMovie` form containing ready
to use `TextField` components. Learn how it works and implement an ability to
add movies from [IMDB](https://www.imdb.com/).

If you want to test your page you can get first image from a [movie page](https://www.imdb.com/title/tt1312171) using `DevTools` -> `Network` -> `Img`
If you want to test your page you can get first image from a [movie page](https://www.imdb.com/title/tt1312171) using `DevTools` -> `Network` -> `Img`

> Here is [the demo page](https://mate-academy.github.io/react_movies-list-add-form/)

Expand All @@ -30,4 +30,4 @@ const pattern = /^((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=+$,\w]+@)?[A-Za-z0-9.-]+|(
- Implement a solution following the [React task guideline](https://github.com/mate-academy/react_task-guideline#react-tasks-guideline).
- Use the [React TypeScript cheat sheet](https://mate-academy.github.io/fe-program/js/extra/react-typescript).
- Open one more terminal and run tests with `npm test` to ensure your solution is correct.
- Replace `<your_account>` with your Github username in the [DEMO LINK](https://<your_account>.github.io/react_movies-list-add-form/) and add it to the PR description.
- Replace `<your_account>` with your Github username in the [DEMO LINK](https://Manankin.github.io/react_movies-list-add-form/) and add it to the PR description.
11 changes: 9 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import { useState } from 'react';
import './App.scss';
import { MoviesList } from './components/MoviesList';
import { NewMovie } from './components/NewMovie';
import moviesFromServer from './api/movies.json';

export const App = () => {
const [preparedMovies, setPreparedMovies] = useState(moviesFromServer);
// const addNewMovie

Choose a reason for hiding this comment

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

remove this comment line


return (
<div className="page">
<div className="page-content">
<MoviesList movies={moviesFromServer} />
<MoviesList movies={preparedMovies} />
</div>
<div className="sidebar">
<NewMovie /* onAdd={(movie) => {}} */ />
<NewMovie onAdd={(movie) => {
setPreparedMovies(currentList => [...currentList, movie]);
}}
Copy link

@DanilWeda DanilWeda Jan 22, 2024

Choose a reason for hiding this comment

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

you can write a handler for it ( not critical )

/>
</div>
</div>
);
Expand Down
67 changes: 58 additions & 9 deletions src/components/NewMovie/NewMovie.tsx
Original file line number Diff line number Diff line change
@@ -1,45 +1,93 @@
import { useState } from 'react';
import { TextField } from '../TextField';
import { Movie } from '../../types/Movie';

export const NewMovie = () => {
export interface Props {
onAdd: (movie: Movie) => void;
}

export const NewMovie = ({ onAdd }: Props) => {
// Increase the count after successful form submission
// to reset touched status of all the `Field`s
Copy link

@DanilWeda DanilWeda Jan 22, 2024

Choose a reason for hiding this comment

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

Think you can remove it ( not critical )

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

Choose a reason for hiding this comment

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

you can use object with type Movie and create only one state for all fields.

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

// your code

const [fieldsOfMovie, setFieldsOfMovie] = React.useState(InitialFieldsOfMovie);

I think it's easier to use because you don't have to create a lot of states for each field, but your solution is also suitable (It is not necessary to change)

Copy link
Author

Choose a reason for hiding this comment

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

We ask about this our teacher Misha and he said that it is not better.


const isAbleToAdd = title && imgUrl && imdbUrl && imdbId;

Choose a reason for hiding this comment

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

Don't forget to trim() value, because now I able to add "empty" movie
image


const createNewMovie = (): Movie => {
return {
title: title.trim(),
description: description.trim(),
imgUrl: imgUrl.trim(),
imdbUrl: imdbUrl.trim(),
imdbId: imdbId.trim(),
};
};

function addNewMovie(event: React.FormEvent<HTMLFormElement>) {
event.preventDefault();
const newMovie = createNewMovie();

setTitle('');
setDescription('');
setImgUrl('');
setImdbUrl('');
setImdbId('');
Comment on lines +30 to +34

Choose a reason for hiding this comment

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

You can write function reset for it


setCount(current => current + 1);

onAdd(newMovie);
}

return (
<form className="NewMovie" key={count}>
<form
className="NewMovie"
key={count}

Choose a reason for hiding this comment

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

Why you need to add this key attribute? Why you need to reset "touched status"? Can you explain please?

Copy link
Author

@Manankin Manankin Jan 19, 2024

Choose a reason for hiding this comment

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

We use this key to rerender component with clear field
I use touched status to controll if field was used then clear and trying to switch to another field (I show error in this case)

onSubmit={addNewMovie} // (event) => {}, f(event)

Choose a reason for hiding this comment

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

remove comment line

>
<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=""
value={description}
onChange={setDescription}
/>

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

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

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

<div className="field is-grouped">
Expand All @@ -48,6 +96,7 @@ export const NewMovie = () => {
type="submit"
data-cy="submit-button"
className="button is-link"
disabled={!isAbleToAdd}
>
Add
</button>
Expand Down
2 changes: 1 addition & 1 deletion src/components/TextField/TextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const TextField: React.FC<Props> = ({
label = name,
placeholder = `Enter ${label}`,
required = false,
onChange = () => {},
onChange = () => { },
}) => {
// generage a unique id once on component load
const [id] = useState(() => `${name}-${getRandomDigits()}`);
Expand Down