From 3160d06630da14aee520d8c00b23189b933a8fe5 Mon Sep 17 00:00:00 2001 From: AsyaDev <142207257+AsyaDev14@users.noreply.github.com> Date: Thu, 5 Dec 2024 20:50:54 +0200 Subject: [PATCH 1/3] task solution --- src/App.tsx | 14 +++- src/components/NewMovie/NewMovie.tsx | 98 +++++++++++++++++++++++--- src/components/TextField/TextField.tsx | 31 ++++---- 3 files changed, 118 insertions(+), 25 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 34be670b0..38528efe6 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -2,15 +2,25 @@ 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'; export const App = () => { + + const [movies, setMovies] = useState(moviesFromServer); + + // const test = (movie:Movie) => { + // setMovies((prev: Movie[]):Movie[] => [...prev, movie]); + // }; + return (
- +
- {}} */ /> + {/* */} + setMovies([...movies, movie])} />
); diff --git a/src/components/NewMovie/NewMovie.tsx b/src/components/NewMovie/NewMovie.tsx index 85bace9dd..e0e031322 100644 --- a/src/components/NewMovie/NewMovie.tsx +++ b/src/components/NewMovie/NewMovie.tsx @@ -1,34 +1,112 @@ -import { useState } from 'react'; +import { FC, useState } from 'react'; import { TextField } from '../TextField'; +import { Movie } from '../../types/Movie'; -export const NewMovie = () => { +type Props = { + onAdd: (movie: Movie) => void; +}; + +export const NewMovie: FC = props => { + const { onAdd } = props; // Increase the count after successful form submission // to reset touched status of all the `Field`s - const [count] = useState(0); + + // const [count] = useState(0); + const [formKey, setFormKey] = useState(0); + + const [titleValue, setTitleValue] = useState(''); + const [descriptionValue, setDescriptionValue] = useState(''); + const [imgUrlValue, setImgUrlValue] = useState(''); + const [imdbUrlValue, setImdbUrlValue] = useState(''); + const [imdbIdValue, setImdbIdValue] = useState(''); + + const handleSubmit = (event: React.FormEvent): void => { + event.preventDefault(); + + const newFilm: Movie = { + title: titleValue.trim(), + description: descriptionValue.trim(), + imgUrl: imgUrlValue.trim(), + imdbUrl: imdbUrlValue.trim(), + imdbId: imdbIdValue.trim(), + }; + + // setMovies((prev: Movie[]):Movie[] => [...prev, newFilm]); + onAdd(newFilm); + + setTitleValue(''); + setDescriptionValue(''); + setImgUrlValue(''); + setImdbUrlValue(''); + setImdbIdValue(''); + + // form refresh + setFormKey(prev => prev + 1); + }; + + // check validation + const isFormValid = + titleValue.trim() && + imgUrlValue.trim() && + imdbUrlValue.trim() && + imdbIdValue.trim(); return ( -
+ ): void => + handleSubmit(event) + } + >

Add a movie

{}} + value={titleValue} + onChange={setTitleValue} required /> - + - + - + - +
From 9cb50273d9d506ff45e5d246c947d588fa802402 Mon Sep 17 00:00:00 2001 From: AsyaDev <142207257+AsyaDev14@users.noreply.github.com> Date: Mon, 9 Dec 2024 13:47:28 +0200 Subject: [PATCH 2/3] fix, change props --- src/App.tsx | 10 ++-- src/components/NewMovie/NewMovie.tsx | 74 +++++++++----------------- src/components/TextField/TextField.tsx | 6 +-- src/helpers/form.ts | 67 +++++++++++++++++++++++ 4 files changed, 98 insertions(+), 59 deletions(-) create mode 100644 src/helpers/form.ts diff --git a/src/App.tsx b/src/App.tsx index 38528efe6..3d90f8ab6 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -6,21 +6,17 @@ import { useState } from 'react'; import { Movie } from './types/Movie'; export const App = () => { - const [movies, setMovies] = useState(moviesFromServer); - // const test = (movie:Movie) => { - // setMovies((prev: Movie[]):Movie[] => [...prev, movie]); - // }; - return (
- {/* */} - setMovies([...movies, movie])} /> + setMovies([...movies, movie])} + />
); diff --git a/src/components/NewMovie/NewMovie.tsx b/src/components/NewMovie/NewMovie.tsx index e0e031322..8b3c13dd9 100644 --- a/src/components/NewMovie/NewMovie.tsx +++ b/src/components/NewMovie/NewMovie.tsx @@ -1,6 +1,7 @@ import { FC, useState } from 'react'; import { TextField } from '../TextField'; import { Movie } from '../../types/Movie'; +import { getFormInputs } from '../../helpers/form'; type Props = { onAdd: (movie: Movie) => void; @@ -8,10 +9,7 @@ type Props = { export const NewMovie: FC = props => { const { onAdd } = props; - // Increase the count after successful form submission - // to reset touched status of all the `Field`s - // const [count] = useState(0); const [formKey, setFormKey] = useState(0); const [titleValue, setTitleValue] = useState(''); @@ -31,7 +29,6 @@ export const NewMovie: FC = props => { imdbId: imdbIdValue.trim(), }; - // setMovies((prev: Movie[]):Movie[] => [...prev, newFilm]); onAdd(newFilm); setTitleValue(''); @@ -40,17 +37,28 @@ export const NewMovie: FC = props => { setImdbUrlValue(''); setImdbIdValue(''); - // form refresh setFormKey(prev => prev + 1); }; - // check validation const isFormValid = titleValue.trim() && imgUrlValue.trim() && imdbUrlValue.trim() && imdbIdValue.trim(); + const formInputs = getFormInputs({ + titleValue, + setTitleValue, + descriptionValue, + setDescriptionValue, + imgUrlValue, + setImgUrlValue, + imdbUrlValue, + setImdbUrlValue, + imdbIdValue, + setImdbIdValue, + }); + return ( = props => { } >

Add a movie

- - - - - - - - - - + {formInputs.map(item => { + return ( + + ); + })}
diff --git a/src/components/NewMovie/NewMovie.tsx b/src/components/NewMovie/NewMovie.tsx index 8b3c13dd9..7989a8bb0 100644 --- a/src/components/NewMovie/NewMovie.tsx +++ b/src/components/NewMovie/NewMovie.tsx @@ -7,56 +7,53 @@ type Props = { onAdd: (movie: Movie) => void; }; -export const NewMovie: FC = props => { - const { onAdd } = props; - +export const NewMovie: FC = ({ onAdd }) => { const [formKey, setFormKey] = useState(0); - const [titleValue, setTitleValue] = useState(''); - const [descriptionValue, setDescriptionValue] = useState(''); - const [imgUrlValue, setImgUrlValue] = useState(''); - const [imdbUrlValue, setImdbUrlValue] = useState(''); - const [imdbIdValue, setImdbIdValue] = useState(''); + const [title, setTitle] = useState(''); + const [description, setDescription] = useState(''); + const [imgUrl, setImgUrl] = useState(''); + const [imdbUrl, setImdbUrl] = useState(''); + const [imdbId, setImdbId] = useState(''); + + const resetFormFields = (): void => { + setTitle(''); + setDescription(''); + setImgUrl(''); + setImdbUrl(''); + setImdbId(''); + }; const handleSubmit = (event: React.FormEvent): void => { event.preventDefault(); const newFilm: Movie = { - title: titleValue.trim(), - description: descriptionValue.trim(), - imgUrl: imgUrlValue.trim(), - imdbUrl: imdbUrlValue.trim(), - imdbId: imdbIdValue.trim(), + title: title.trim(), + description: description.trim(), + imgUrl: imgUrl.trim(), + imdbUrl: imdbUrl.trim(), + imdbId: imdbId.trim(), }; onAdd(newFilm); - - setTitleValue(''); - setDescriptionValue(''); - setImgUrlValue(''); - setImdbUrlValue(''); - setImdbIdValue(''); - + resetFormFields(); setFormKey(prev => prev + 1); }; const isFormValid = - titleValue.trim() && - imgUrlValue.trim() && - imdbUrlValue.trim() && - imdbIdValue.trim(); + title.trim() && imgUrl.trim() && imdbUrl.trim() && imdbId.trim(); const formInputs = getFormInputs({ - titleValue, - setTitleValue, - descriptionValue, - setDescriptionValue, - imgUrlValue, - setImgUrlValue, - imdbUrlValue, - setImdbUrlValue, - imdbIdValue, - setImdbIdValue, + title, + setTitle, + description, + setDescription, + imgUrl, + setImgUrl, + imdbUrl, + setImdbUrl, + imdbId, + setImdbId, }); return ( diff --git a/src/helpers/form.ts b/src/helpers/form.ts index 039bea62e..c0b98d573 100644 --- a/src/helpers/form.ts +++ b/src/helpers/form.ts @@ -1,66 +1,66 @@ import { Props } from '../components/TextField/TextField'; interface FormInputsProps { - titleValue: string; - setTitleValue: (value: string) => void; - descriptionValue: string; - setDescriptionValue: (value: string) => void; - imgUrlValue: string; - setImgUrlValue: (value: string) => void; - imdbUrlValue: string; - setImdbUrlValue: (value: string) => void; - imdbIdValue: string; - setImdbIdValue: (value: string) => void; + title: string; + setTitle: (value: string) => void; + description: string; + setDescription: (value: string) => void; + imgUrl: string; + setImgUrl: (value: string) => void; + imdbUrl: string; + setImdbUrl: (value: string) => void; + imdbId: string; + setImdbId: (value: string) => void; } export const getFormInputs = (props: FormInputsProps): Props[] => { const { - titleValue, - setTitleValue, - descriptionValue, - setDescriptionValue, - imgUrlValue, - setImgUrlValue, - imdbUrlValue, - setImdbUrlValue, - imdbIdValue, - setImdbIdValue, + title, + setTitle, + description, + setDescription, + imgUrl, + setImgUrl, + imdbUrl, + setImdbUrl, + imdbId, + setImdbId, } = props; return [ { name: 'title', label: 'Title', - value: titleValue, - onChange: setTitleValue, + value: title, + onChange: setTitle, required: true, }, { name: 'description', label: 'Description', - value: descriptionValue, - onChange: setDescriptionValue, + value: description, + onChange: setDescription, required: true, }, { name: 'imgUrl', label: 'Image URL', - value: imgUrlValue, - onChange: setImgUrlValue, + value: imgUrl, + onChange: setImgUrl, required: true, }, { name: 'imdbUrl', label: 'Imdb URL', - value: imdbUrlValue, - onChange: setImdbUrlValue, + value: imdbUrl, + onChange: setImdbUrl, required: true, }, { name: 'imdbId', label: 'Imdb ID', - value: imdbIdValue, - onChange: setImdbIdValue, + value: imdbId, + onChange: setImdbId, required: true, }, ];