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

aasdasd #2682

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

aasdasd #2682

Show file tree
Hide file tree
Changes from 1 commit
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 @@ -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);

Choose a reason for hiding this comment

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

Ensure that moviesFromServer is an array of objects that conform to the Movie type. If the structure of moviesFromServer does not match the Movie type, it could lead to runtime errors.

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;

Choose a reason for hiding this comment

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

The buttonEnabled logic should ensure that all required fields are filled before enabling the submit button. Currently, it checks if any of the fields are filled, which might not be the intended behavior.

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={() => {}}

Choose a reason for hiding this comment

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

The value prop should be set to the corresponding state variable (e.g., title, description, etc.) to reflect the current input value. Currently, it is set to an empty string, which will not update with user input.

required
data={title}
onChange={setTitle}
/>

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

Choose a reason for hiding this comment

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

The value prop should be set to the description state variable to reflect the current input value.

onChange={setDescription}
/>

<TextField name="imgUrl" label="Image URL" value="" />
<TextField
name="imgUrl"
label="Image URL"
value=""
required

Choose a reason for hiding this comment

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

The value prop should be set to the imgUrl state variable to reflect the current input value.

data={imgUrl}
onChange={setImgUrl}
/>

<TextField name="imdbUrl" label="Imdb URL" value="" />
<TextField
name="imdbUrl"
label="Imdb URL"
value=""
required

Choose a reason for hiding this comment

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

The value prop should be set to the imdbUrl state variable to reflect the current input value.

data={imdbUrl}
onChange={setImdbUrl}
/>

<TextField name="imdbId" label="Imdb ID" value="" />
<TextField
name="imdbId"
label="Imdb ID"
value=""
required

Choose a reason for hiding this comment

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

The value prop should be set to the imdbId state variable to reflect the current input value.

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}

Choose a reason for hiding this comment

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

The logic for the required attribute is a bit complex and might not behave as expected. It sets required to false for the 'description' field, which is fine, but for other fields, it checks if data is present to determine if the field is required. This logic might lead to unexpected behavior if data is not correctly initialized or updated. Consider simplifying this logic to ensure it aligns with the intended validation requirements.

/>
</div>

Expand Down
Loading