Skip to content

Commit

Permalink
solution
Browse files Browse the repository at this point in the history
  • Loading branch information
zvir91 committed Oct 19, 2023
1 parent 9bacc9b commit 62be060
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 14 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://zvir91.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 { useState } from 'react';

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);
const addMovie = (newMovie: Movie) => {
setMovies(currentMovies => [...currentMovies, newMovie]);
};

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
85 changes: 75 additions & 10 deletions src/components/NewMovie/NewMovie.tsx
Original file line number Diff line number Diff line change
@@ -1,45 +1,109 @@
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);
// to reset touched status of all the `Field`
const [count, setCount] = useState(0);
const [newTitle, setNewTitle] = useState('');
const [newDescription, setNewDescription] = useState('');
const [newImageURL, setNewImageURL] = useState('');
const [newURL, setNewURL] = useState('');
const [newId, setNewId] = useState('');

const reset = () => {
setNewTitle('');
setNewDescription('');
setNewImageURL('');
setNewURL('');
setNewId('');
};

const disable = !newTitle || !newImageURL || !newId || !newURL || !newId;

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

let updCaunt = count;

setCount(updCaunt += 1);

if (disable) {
return;
}

onAdd({
title: newTitle,
description: newDescription,
imgUrl: newImageURL,
imdbUrl: newURL,
imdbId: newId,
});

reset();
};

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

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

<TextField
name="description"
label="Description"
value=""
value={newDescription}
onChange={(value) => {
setNewDescription(value);
}}
/>

<TextField
name="imgUrl"
label="Image URL"
value=""
value={newImageURL}
onChange={(value) => {
setNewImageURL(value);
}}
required
/>

<TextField
name="imdbUrl"
label="Imdb URL"
value=""
value={newURL}
onChange={(value) => {
setNewURL(value);
}}
required
/>

<TextField
name="imdbId"
label="Imdb ID"
value=""
value={newId}
onChange={(value) => {
setNewId(value);
}}
required
/>

<div className="field is-grouped">
Expand All @@ -48,6 +112,7 @@ export const NewMovie = () => {
type="submit"
data-cy="submit-button"
className="button is-link"
disabled={disable}
>
Add
</button>
Expand Down
4 changes: 3 additions & 1 deletion src/components/TextField/TextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ export const TextField: React.FC<Props> = ({
})}
placeholder={placeholder}
value={value}
onChange={event => onChange(event.target.value)}
onChange={(event) => {
onChange(event.target.value);
}}
onBlur={() => setTouched(true)}
/>
</div>
Expand Down

0 comments on commit 62be060

Please sign in to comment.