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

task solution #2654

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 12 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<Movie[]>(moviesFromServer);

// const test = (movie:Movie) => {
// setMovies((prev: Movie[]):Movie[] => [...prev, movie]);
// };
AsyaDev14 marked this conversation as resolved.
Show resolved Hide resolved

return (
<div className="page">
<div className="page-content">
<MoviesList movies={moviesFromServer} />
<MoviesList movies={movies} />
</div>
<div className="sidebar">
<NewMovie /* onAdd={(movie) => {}} */ />
{/* <NewMovie setMovies={setMovies} /> */}
<NewMovie onAdd={(movie:Movie):void => setMovies([...movies, movie])} />
AsyaDev14 marked this conversation as resolved.
Show resolved Hide resolved
</div>
</div>
);
Expand Down
98 changes: 88 additions & 10 deletions src/components/NewMovie/NewMovie.tsx
Original file line number Diff line number Diff line change
@@ -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> = 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<number>(0);
const [formKey, setFormKey] = useState<number>(0);
AsyaDev14 marked this conversation as resolved.
Show resolved Hide resolved

const [titleValue, setTitleValue] = useState<string>('');
const [descriptionValue, setDescriptionValue] = useState<string>('');
const [imgUrlValue, setImgUrlValue] = useState<string>('');
const [imdbUrlValue, setImdbUrlValue] = useState<string>('');
const [imdbIdValue, setImdbIdValue] = useState<string>('');

AsyaDev14 marked this conversation as resolved.
Show resolved Hide resolved
const handleSubmit = (event: React.FormEvent<HTMLFormElement>): 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);
AsyaDev14 marked this conversation as resolved.
Show resolved Hide resolved

setTitleValue('');
setDescriptionValue('');
setImgUrlValue('');
setImdbUrlValue('');
setImdbIdValue('');

AsyaDev14 marked this conversation as resolved.
Show resolved Hide resolved
// form refresh
setFormKey(prev => prev + 1);
};

// check validation
const isFormValid =
titleValue.trim() &&
imgUrlValue.trim() &&
imdbUrlValue.trim() &&
imdbIdValue.trim();

return (
<form className="NewMovie" key={count}>
<form
className="NewMovie"
key={formKey}
onSubmit={(event: React.FormEvent<HTMLFormElement>): void =>
handleSubmit(event)
}
>
Comment on lines +63 to +65

Choose a reason for hiding this comment

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

Suggested change
onSubmit={(event: React.FormEvent<HTMLFormElement>): void =>
handleSubmit(event)
}
onSubmit={handleSubmit}

<h2 className="title">Add a movie</h2>

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

<TextField name="description" label="Description" value="" />
<TextField
name="description"
label="Description"
value={descriptionValue}
onChange={setDescriptionValue}
/>

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

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

<TextField name="imdbId" label="Imdb ID" value="" />
<TextField
name="imdbId"
label="Imdb ID"
value={imdbIdValue}
AsyaDev14 marked this conversation as resolved.
Show resolved Hide resolved
onChange={setImdbIdValue}
required
/>

<div className="field is-grouped">
<div className="control">
<button
// disabled={
// !titleValue && !imgUrlValue && !imdbUrlValue && !imdbIdValue
// }
disabled={!isFormValid}
AsyaDev14 marked this conversation as resolved.
Show resolved Hide resolved
type="submit"
data-cy="submit-button"
className="button is-link"
Expand Down
31 changes: 18 additions & 13 deletions src/components/TextField/TextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,24 @@ type Props = {
onChange?: (newValue: string) => void;
};

function getRandomDigits() {
function getRandomDigits(): string {
return Math.random().toFixed(16).slice(2);
}

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

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

return (
Expand All @@ -45,8 +46,12 @@ export const TextField: React.FC<Props> = ({
})}
placeholder={placeholder}
value={value}
onChange={event => onChange(event.target.value)}
onBlur={() => setTouched(true)}
onChange={(event: React.ChangeEvent<HTMLInputElement>): void => {
if (onChange) {
onChange(event.target.value);
}
}}
onBlur={(): void => setTouched(true)}
/>
</div>

Expand Down
Loading