Skip to content

Commit

Permalink
aasdasd
Browse files Browse the repository at this point in the history
  • Loading branch information
sinner1993 committed Dec 17, 2024
1 parent f27ae36 commit 2ece445
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 17 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://sinner1993.github.io/react_movies-list-add-form/) and add it to the PR description.
18 changes: 16 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,29 @@ import './App.scss';
import { MoviesList } from './components/MoviesList';
import { NewMovie } from './components/NewMovie';
import moviesFromServer from './api/movies.json';
import { useState } from 'react';

type Movie = {
title: string;
description: string;
imgUrl: string;
imdbUrl: string;
imdbId: string;
};

export const App = () => {
const [films, setFilms] = useState<Movie[]>(moviesFromServer);
const handleAdd = (newMovie: Movie): void => {
setFilms(prevMovies => [...prevMovies, newMovie]);
};

return (
<div className="page">
<div className="page-content">
<MoviesList movies={moviesFromServer} />
<MoviesList movies={films} />
</div>
<div className="sidebar">
<NewMovie /* onAdd={(movie) => {}} */ />
<NewMovie onAdd={handleAdd} />
</div>
</div>
);
Expand Down
96 changes: 88 additions & 8 deletions src/components/NewMovie/NewMovie.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,117 @@
import { useState } from 'react';
import { FormEvent, useState } from 'react';
import { TextField } from '../TextField';

export const NewMovie = () => {
type Movie = {
title: string;
description: string;
imgUrl: string;
imdbUrl: string;
imdbId: string;
};

type NewMovieProps = {
onAdd: (newMovie: Movie) => void;
};

export const NewMovie: React.FC<NewMovieProps> = ({ onAdd }) => {
// Increase the count after successful form submission
// to reset touched status of all the `Field`s
const [count] = useState(0);
const [title, setTitle] = useState<string>('');
const [description, setDescription] = useState<string>('');
const [imgUrl, setImgUrl] = useState<string>('');
const [imdbUrl, setImdbUrl] = useState<string>('');
const [imdbId, setImdbId] = useState<string>('');
const buttonEnabled = title || imgUrl || imdbUrl || imdbId;

const handleSubmit = (event: FormEvent<HTMLFormElement>): void => {
event.preventDefault();

const form = event.target as HTMLFormElement;
const formData = new FormData(form);

const data = Object.fromEntries(formData.entries()) as {
[key: string]: string;
};

const newMovie: Movie = {
title: data.title,
description: data.description,
imgUrl: data.imgUrl,
imdbUrl: data.imdbUrl,
imdbId: data.imdbId,
};

onAdd(newMovie);

form.reset();

setTitle('');
setDescription('');
setImgUrl('');
setImdbUrl('');
setImdbId('');
};

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

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

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

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

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

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

<div className="field is-grouped">
<div className="control">
<button
type="submit"
data-cy="submit-button"
className="button is-link"
disabled={!buttonEnabled}
>
Add
</button>
Expand Down
19 changes: 13 additions & 6 deletions src/components/TextField/TextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ type Props = {
label?: string;
placeholder?: string;
required?: boolean;
onChange?: (newValue: string) => void;
data: string;
onChange: (newValue: string) => void;
};

function getRandomDigits() {
Expand All @@ -16,18 +17,22 @@ function getRandomDigits() {

export const TextField: React.FC<Props> = ({
name,
value,
label = name,
placeholder = `Enter ${label}`,
required = false,
onChange = () => {},
data,
onChange,
}) => {
// generate a unique id once on component load
const [id] = useState(() => `${name}-${getRandomDigits()}`);

// To show errors only if the field was touched (onBlur)
const [touched, setTouched] = useState(false);
const hasError = touched && required && !value;
const hasError = touched && required && !data;

const handleOnChange = (event: React.ChangeEvent<HTMLInputElement>) => {
onChange(event.target.value);
};

return (
<div className="field">
Expand All @@ -37,16 +42,18 @@ export const TextField: React.FC<Props> = ({

<div className="control">
<input
name={name}
type="text"
id={id}
data-cy={`movie-${name}`}
className={classNames('input', {
'is-danger': hasError,
})}
placeholder={placeholder}
value={value}
onChange={event => onChange(event.target.value)}
value={data}
onChange={event => handleOnChange(event)}
onBlur={() => setTouched(true)}
required={name === 'description' ? false : data ? false : true}
/>
</div>

Expand Down

0 comments on commit 2ece445

Please sign in to comment.