Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
AV1788 committed Jan 21, 2024
1 parent 555a0b8 commit b4800e7
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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://AV1788.github.io/react_movies-list-add-form/) and add it to the PR description.
12 changes: 10 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import './App.scss';
import { useState } from 'react';
import { Movie } from './types/Movie';
import { MoviesList } from './components/MoviesList';
import { NewMovie } from './components/NewMovie';
import moviesFromServer from './api/movies.json';

export const App = () => {
const [movies, setMovies] = useState<Movie[]>(moviesFromServer);

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

return (
<div className="page">
<div className="page-content">
<MoviesList movies={moviesFromServer} />
<MoviesList movies={movies} />
</div>
<div className="sidebar">
<NewMovie /* onAdd={(movie) => {}} */ />
<NewMovie onAdd={addMovie} />
</div>
</div>
);
Expand Down
110 changes: 91 additions & 19 deletions src/components/NewMovie/NewMovie.tsx
Original file line number Diff line number Diff line change
@@ -1,61 +1,133 @@
import { 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 [text, setText] = useState('');
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 addTitle = (textInput: string) => {
setTitle(textInput);
};

const addDescription = (textInput: string) => {
setDescription(textInput);
};

const addImgUrl = (textInput: string) => {
setImgUrl(textInput);
};

const addImdbUrl = (textInput: string) => {
setImdbUrl(textInput);
};

const addText = (textInput: string) => {
setText(textInput);
const addImdbId = (textInput: string) => {
setImdbId(textInput);
};

const reset = () => {
setTitle('');
setDescription('');
setImgUrl('');
setImdbUrl('');
setImdbId('');
};

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

onAdd({
title,
description,
imgUrl,
imdbUrl,
imdbId,
});

reset();
setCount(count + 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={text}
onChange={addText}
value={title}
onChange={addTitle}
required
/>

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

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

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

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

<div className="field is-grouped">
<div className="control">
<button
type="submit"
data-cy="submit-button"
className="button is-link"
>
Add
</button>
{title !== '' && imgUrl !== ''
&& imdbUrl !== '' && imdbId !== ''
? (
<button
type="submit"
data-cy="submit-button"
className="button is-link"
>
Add
</button>
) : (
<button
type="submit"
data-cy="submit-button"
className="button is-link"
disabled
>
Add
</button>
)}
</div>
</div>
</form>
Expand Down

0 comments on commit b4800e7

Please sign in to comment.