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

add task solution #2401

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
17 changes: 13 additions & 4 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import { useState } from 'react';

import './App.scss';
import moviesFromServer from './api/movies.json';
import { MoviesList } from './components/MoviesList';
import { NewMovie } from './components/NewMovie';
import moviesFromServer from './api/movies.json';
import { Movie } from './types/Movie';

export const App: React.FC = () => {
const [movies, setMovies] = useState<Movie[]>(moviesFromServer);

const handleAddMovie = (movie: Movie) => {
setMovies([...movies, movie]);
};

export const App = () => {
return (
<div className="page">
<div className="page-content">
<MoviesList movies={moviesFromServer} />
<MoviesList movies={movies} />
</div>
<div className="sidebar">
<NewMovie /* onAdd={(movie) => {}} */ />
<NewMovie onAdd={handleAddMovie} />
</div>
</div>
);
Expand Down
6 changes: 2 additions & 4 deletions src/components/MoviesList/MoviesList.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import React from 'react';

import './MoviesList.scss';
import { MovieCard } from '../MovieCard';
import { Movie } from '../../types/Movie';
import { MovieCard } from '../MovieCard';
import './MoviesList.scss';

interface Props {
movies: Movie[];
Expand Down
100 changes: 89 additions & 11 deletions src/components/NewMovie/NewMovie.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,115 @@
import { useState } from 'react';
import { Movie } from '../../types/Movie';
import { TextField } from '../TextField';

export const NewMovie = () => {
// Increase the count after successful form submission
// to reset touched status of all the `Field`s
const [count] = useState(0);
interface MovieForm {
title: string;
imgUrl: string;
imdbUrl: string;
imdbId: string;
description: string;
}

export const NewMovie = ({ onAdd }: { onAdd: (movie: Movie) => void }) => {
const [form, setForm] = useState<Movie>({
title: '',
imgUrl: '',
imdbUrl: '',
imdbId: '',
description: '',
});

const [touched, setTouched] = useState<{ [key: string]: boolean }>({});

const handleBlur = (name: string) => {
setTouched(prev => ({ ...prev, [name]: true }));
};

const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const { name, value } = event.target;
setForm(prevForm => ({
...prevForm,
[name]: value,

Choose a reason for hiding this comment

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

Suggested change
[name]: value,
[event.target.name]: event.target.value,

}));
};

const isFormValid = () => {
const requiredFields = ['title', 'imgUrl', 'imdbUrl', 'imdbId'];

return requiredFields.every(field => !!form[field as keyof MovieForm]);
};

const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();
onAdd(form);
setForm({
title: '',
imgUrl: '',
imdbUrl: '',
imdbId: '',
description: '',
});
setTouched({});
};

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

<TextField
name="title"
label="Title"
value=""
onChange={() => {}}
value={form.title}
onChange={handleChange}
onBlur={() => handleBlur('title')}
required
touched={!!touched.title}
/>

<TextField name="description" label="Description" value="" />
<TextField
name="description"
label="Description"
value={form.description}
onChange={handleChange}
/>

<TextField name="imgUrl" label="Image URL" value="" />
<TextField
name="imgUrl"
label="Image URL"
value={form.imgUrl}
onChange={handleChange}
onBlur={() => handleBlur('imgUrl')}
required
touched={!!touched.imgUrl}
/>

<TextField name="imdbUrl" label="Imdb URL" value="" />
<TextField
name="imdbUrl"
label="Imdb URL"
value={form.imdbUrl}
onChange={handleChange}
onBlur={() => handleBlur('imdbUrl')}
required
touched={!!touched.imdbUrl}
/>

<TextField name="imdbId" label="Imdb ID" value="" />
<TextField
name="imdbId"
label="Imdb ID"
value={form.imdbId}
onChange={handleChange}
onBlur={() => handleBlur('imdbId')}
required
touched={!!touched.imdbId}
/>

<div className="field is-grouped">
<div className="control">
<button
type="submit"
data-cy="submit-button"
className="button is-link"
disabled={!isFormValid()}
>
Add
</button>
Expand Down
12 changes: 9 additions & 3 deletions src/components/TextField/TextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ type Props = {
placeholder?: string;
required?: boolean;
onChange?: (newValue: string) => void;
onBlur?: () => void; // Оновлено
touched?: boolean;
};

function getRandomDigits() {
Expand All @@ -21,12 +23,16 @@ export const TextField: React.FC<Props> = ({
placeholder = `Enter ${label}`,
required = false,
onChange = () => {},
onBlur = () => {}, // Оновлено
touched = false,
}) => {
// generage 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 handleBlur = () => {
onBlur(); // Викликаємо колбек onBlur
};

const hasError = touched && required && !value;

return (
Expand All @@ -46,7 +52,7 @@ export const TextField: React.FC<Props> = ({
placeholder={placeholder}
value={value}
onChange={event => onChange(event.target.value)}
onBlur={() => setTouched(true)}
onBlur={handleBlur} // Викликаємо локальну функцію handleBlur
/>
</div>

Expand Down
Loading