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

add task solution #2716

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://DianaKadimbekova.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
Expand Up @@ -2,15 +2,22 @@ import './App.scss';
import { MoviesList } from './components/MoviesList';
import { NewMovie } from './components/NewMovie';
import moviesFromServer from './api/movies.json';
import { useState } from 'react';
import { Movie } from '././types/Movie';

Choose a reason for hiding this comment

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

The import path '././types/Movie' seems incorrect. It should likely be './types/Movie' without the double './'. Please verify the correct path to the Movie type.


export const App = () => {
const [movies, setMovies] = useState(moviesFromServer);
const handleMovies = (newMovie: Movie) => {
setMovies(precMovies => [...precMovies, newMovie]);
};

return (
<div className="page">
<div className="page-content">
<MoviesList movies={moviesFromServer} />
<MoviesList movies={movies} />
</div>
<div className="sidebar">
<NewMovie /* onAdd={(movie) => {}} */ />
<NewMovie onAdd={handleMovies} />
</div>
</div>
);
Expand Down
105 changes: 96 additions & 9 deletions src/components/NewMovie/NewMovie.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,124 @@
import { useState } from 'react';
import { TextField } from '../TextField';
import { Movie } from '../../types/Movie';
interface Props {
onAdd: (movie: Movie) => void;
}

export const NewMovie = () => {
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 [title, setTitle] = useState('');
const [description, setDescription] = useState('');
const [imgUrl, setImgUrl] = useState('');
const [imdbUrl, setImdbUrl] = useState('');
const [imdbId, setImdbId] = useState('');

const trimmedTitle = title.trim();
const trimmedDescription = description.trim();
const trimmedImgUrl = imgUrl.trim();
const trimmedImdbUrl = imdbUrl.trim();
const trimmedImdbId = imdbId.trim();

const handleTitle = (currentValue: string) => {
setTitle(currentValue);
};

const handleDescription = (currentValue: string) => {
setDescription(currentValue);
};

const handleImgUrl = (currentValue: string) => {
setImgUrl(currentValue);
};

const handleImdbUrl = (currentValue: string) => {
setImdbUrl(currentValue);
};

const handleImdbId = (currentValue: string) => {
setImdbId(currentValue);
};

const newMovie = {
title: trimmedTitle,
description: trimmedDescription,
imgUrl: trimmedImgUrl,
imdbUrl: trimmedImdbUrl,
imdbId: trimmedImdbId,
};

const handleSubmit = (event: React.FormEvent) => {
event.preventDefault();

if (trimmedTitle && trimmedImgUrl && trimmedImdbUrl && trimmedImdbId) {
onAdd(newMovie);
}

setTitle('');
setDescription('');
setImgUrl('');
setImdbUrl('');
setImdbId('');
setCount(prevCount => prevCount + 1);
};

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

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

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

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

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

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

<div className="field is-grouped">
<div className="control">
<button
type="submit"
data-cy="submit-button"
className="button is-link"
disabled={
!trimmedTitle ||
!trimmedImgUrl ||
!trimmedImdbUrl ||
!trimmedImdbId
}
>
Add
</button>
Expand Down
Loading