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

Solution #2683

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 10 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@ 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 addNewMovie = (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={addNewMovie} />
</div>
</div>
);
Expand Down
106 changes: 93 additions & 13 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 React, { useState } from 'react';
import { TextField } from '../TextField';
import { Movie } from '../../types/Movie';

export const NewMovie = () => {
// Increase the count after successful form submission
// to reset touched status of all the `Field`s
const [count] = useState(0);
interface Props {
onAdd: (movie: Movie) => void;
}

enum Fields {
title = 'Title',
description = 'Description',
imgUrl = 'Image URL',
imdbUrl = 'Imdb URL',
imdbId = 'Imdb ID',
}

const isValid = (value: string): boolean => {
return value.trim().length > 0;
};

export const NewMovie: React.FC<Props> = ({ onAdd }) => {
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 reset = () => {
setCount(0);
setTitle('');
setDescription('');
setImgUrl('');
setImdbUrl('');
setImdbId('');
};

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

const newMovie = {
title: title.trim(),
description: description.trim(),
imgUrl: imgUrl.trim(),
imdbUrl: imdbUrl.trim(),
imdbId: imdbId.trim(),
};

onAdd(newMovie);

setCount(count + 1);
reset();
};

return (
<form className="NewMovie" key={count}>
<form
className="NewMovie"
key={count}
onSubmit={handleFormSubmit}

Choose a reason for hiding this comment

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

Change the form's key to reinitialize it after resetting to clear errors. This is necessary to ensure errors are not shown after clearing the form, as per the task requirements.

onReset={reset}
>
<h2 className="title">Add a movie</h2>
Comment on lines +56 to +61

Choose a reason for hiding this comment

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

Consider implementing onBlur validation for required fields to show errors and a red border if the input is invalid. This is part of the task requirements to ensure fields are validated when they lose focus.


<TextField
name="title"
label="Title"
value=""
onChange={() => {}}
label={Fields.title}
value={title}
onChange={event => setTitle(event.target.value)}
required
/>

<TextField name="description" label="Description" value="" />
<TextField
name="description"
label={Fields.description}
value={description}
onChange={event => setDescription(event.target.value)}
/>

<TextField name="imgUrl" label="Image URL" value="" />
<TextField
name="imgUrl"
label={Fields.imgUrl}
value={imgUrl}
onChange={event => setImgUrl(event.target.value)}
required
/>

<TextField name="imdbUrl" label="Imdb URL" value="" />
<TextField
name="imdbUrl"
label={Fields.imdbUrl}
value={imdbUrl}
onChange={event => setImdbUrl(event.target.value)}
required
/>

<TextField name="imdbId" label="Imdb ID" value="" />
<TextField
name="imdbId"
label={Fields.imdbId}
value={imdbId}
onChange={event => setImdbId(event.target.value)}
required
/>

<div className="field is-grouped">
<div className="control">
<button
type="submit"
data-cy="submit-button"
className="button is-link"
disabled={
!isValid(title) ||
!isValid(imgUrl) ||
!isValid(imdbUrl) ||
!isValid(imdbId)
}
>
Comment on lines +109 to +114

Choose a reason for hiding this comment

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

Ensure that the submit button is disabled until all required fields are filled with valid, trimmed values. This is a task requirement to prevent submission of incomplete data.

Add
</button>
Expand Down
6 changes: 3 additions & 3 deletions src/components/TextField/TextField.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import classNames from 'classnames';
import React, { useState } from 'react';
import React, { ChangeEvent, useState } from 'react';

type Props = {
name: string;
value: string;
label?: string;
placeholder?: string;
required?: boolean;
onChange?: (newValue: string) => void;
onChange?: (newValue: ChangeEvent<HTMLInputElement>) => void;
};

function getRandomDigits() {
Expand Down Expand Up @@ -45,7 +45,7 @@ export const TextField: React.FC<Props> = ({
})}
placeholder={placeholder}
value={value}
onChange={event => onChange(event.target.value)}
onChange={event => onChange(event)}
onBlur={() => setTouched(true)}
/>
</div>
Expand Down
Loading