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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,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://Blervin1.github.io/react_movies-list-add-form/) and add it to the PR description.
14 changes: 11 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import { useState } from 'react';
import moviesFromServer from './api/movies.json';
import './App.scss';
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 [movies, setMovies] = useState<Movie[]>([...moviesFromServer]);

Choose a reason for hiding this comment

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

You are using the spread operator to create a copy of moviesFromServer when initializing the movies state. According to the checklist, if you are using a non-mutating array method, you don't need to create a copy of the array. Consider removing the spread operator: useState<Movie[]>(moviesFromServer);.


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

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
92 changes: 80 additions & 12 deletions src/components/NewMovie/NewMovie.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,105 @@
import { useState } from 'react';
import { FC, useState } from 'react';
import { Movie } from '../../types/Movie';
import { TextField } from '../TextField';

export const NewMovie = () => {
// Increase the count after successful form submission
// to reset touched status of all the `Field`s
const [count] = useState(0);
interface Props {
onAdd: (movie: Movie) => void;
}

type InputTypes = 'title' | 'description' | 'imgUrl' | 'imdbUrl' | 'imdbId';

export const NewMovie: FC<Props> = ({ onAdd }) => {
const [count, setCount] = useState(0);
const [movies, setMovies] = useState({

Choose a reason for hiding this comment

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

The count state is used to reset the form by changing the key, which is a good approach to ensure the form is reinitialized. Ensure that the TextField component handles the onBlur event to validate inputs as per the task description.

title: '',

Choose a reason for hiding this comment

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

Suggested change
const [movies, setMovies] = useState({
const [movie, setMovie] = useState({

description: '',
imgUrl: '',
imdbUrl: '',
imdbId: '',
});

Comment on lines +13 to +19

Choose a reason for hiding this comment

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

Consider renaming the movies state to something more descriptive like movieDetails to avoid confusion, as it represents a single movie's details rather than multiple movies.

const validInputs = () => {
const { title, imgUrl, imdbUrl, imdbId } = movies;

return title.trim() && imgUrl.trim() && imdbUrl.trim() && imdbId.trim();
};

function handleInputChange(name: InputTypes, value: string) {
setMovies(prevMovie => ({
...prevMovie,
[name]: value,
}));
}

const reset = () => {
setMovies({
title: '',
description: '',
imgUrl: '',
imdbUrl: '',
imdbId: '',
});
};

const handleSubmit = (event: React.FormEvent) => {
event.preventDefault();
onAdd(movies);
reset();
setCount(count + 1);
};

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

<TextField
name="title"
label="Title"
value=""
onChange={() => {}}
value={movies.title}
onChange={value => {
handleInputChange('title', value);
}}
required
/>

<TextField name="description" label="Description" value="" />
<TextField
name="description"
label="Description"
value={movies.description}
onChange={value => handleInputChange('description', value)}
/>

<TextField name="imgUrl" label="Image URL" value="" />
<TextField
name="imgUrl"
label="Image URL"
required
value={movies.imgUrl}
onChange={value => handleInputChange('imgUrl', value)}
/>

<TextField name="imdbUrl" label="Imdb URL" value="" />
<TextField
name="imdbUrl"
label="Imdb URL"
required
value={movies.imdbUrl}
onChange={value => handleInputChange('imdbUrl', value)}
/>

<TextField name="imdbId" label="Imdb ID" value="" />
<TextField
name="imdbId"
label="Imdb ID"
value={movies.imdbId}
required
onChange={value => handleInputChange('imdbId', value)}
/>

<div className="field is-grouped">
<div className="control">
<button
type="submit"
data-cy="submit-button"
className="button is-link"
disabled={!validInputs()}
>
Add
</button>
Expand Down
4 changes: 1 addition & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
{
"extends": "@mate-academy/students-ts-config",
"include": [
"src"
],
"include": ["src"],
"compilerOptions": {
"sourceMap": false
}
Expand Down
Loading