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 #2111

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
19 changes: 17 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
import './App.scss';
import { useState } from 'react';
import { MoviesList } from './components/MoviesList';
import { NewMovie } from './components/NewMovie';
import moviesFromServer from './api/movies.json';

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

export const App = () => {
const [movieList, setMovieList] = useState<State[]>([...moviesFromServer]);

const handleAddMovie = (newMovie: State) => {
setMovieList((prevMovies) => [...prevMovies, newMovie]);
};

return (
<div className="page">
<div className="page-content">
<MoviesList movies={moviesFromServer} />
<MoviesList movies={movieList} />
</div>
<div className="sidebar">
<NewMovie /* onAdd={(movie) => {}} */ />
<NewMovie onAdd={handleAddMovie} />
</div>
</div>
);
Expand Down
78 changes: 69 additions & 9 deletions src/components/NewMovie/NewMovie.tsx
Original file line number Diff line number Diff line change
@@ -1,45 +1,101 @@
import { useState } from 'react';
import { TextField } from '../TextField';

export const NewMovie = () => {
interface NewMovieProps {
onAdd: (
movie: {
title: string,
description: string,
imgUrl: string,
imdbUrl: string,
imdbId: string,
}

Choose a reason for hiding this comment

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

You create type State and can use it as type for movie. Actually we already have the same type in 'types' folder, so you may use it instead of creating you own.

Choose a reason for hiding this comment

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

agree)

) => void
Copy link

@olya-shyian olya-shyian Jan 22, 2024

Choose a reason for hiding this comment

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

If you don't understand what Oleksandr meant, then look at this

 onAdd: (movie: Movie) => void

you already have Movie type

dont write

 onAdd: (
   movie: {
     title: string,
     description: string,
     imgUrl: string,
     imdbUrl: string,
     imdbId: string,
   }

Copy link
Author

Choose a reason for hiding this comment

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

i fixed only in App. here I scroll and missed it))

}

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 [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 urlValidator = (urlString: string): boolean => {
// eslint-disable-next-line max-len

Choose a reason for hiding this comment

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

put this function inside utils folder

const pattern = /^((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=+$,\w]+@)?[A-Za-z0-9.-]+|(?:www\.|[-;:&=+$,\w]+@)[A-Za-z0-9.-]+)((?:\/[+~%/.\w-_]*)?\??(?:[-+=&;%@,.\w_]*)#?(?:[,.!/\\\w]*))?)$/;

if (!urlString.match(pattern)) {
return false;
}

return true;
};

function addMovie(event: React.FormEvent) {
event.preventDefault();
onAdd({
title: title.trim(),
description: description.trim(),
imgUrl: imgUrl.trim(),
imdbUrl: imdbUrl.trim(),
imdbId: imdbId.trim(),
});

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

Choose a reason for hiding this comment

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

you can create a reset function for this


setCount(prev => prev + 1);
}

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

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

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

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

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

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

<div className="field is-grouped">
Expand All @@ -48,6 +104,10 @@ export const NewMovie = () => {
type="submit"
data-cy="submit-button"
className="button is-link"
disabled={!title
|| !urlValidator(imgUrl)
|| !urlValidator(imdbUrl)
|| !imdbId}

Choose a reason for hiding this comment

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

maybe put this check in const

>
Add
</button>
Expand Down
4 changes: 4 additions & 0 deletions src/components/TextField/TextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type Props = {
placeholder?: string,
required?: boolean,
onChange?: (newValue: string) => void,
checkUrl?: (urlString: string) => boolean,
};

function getRandomDigits() {
Expand All @@ -23,6 +24,7 @@ export const TextField: React.FC<Props> = ({
placeholder = `Enter ${label}`,
required = false,
onChange = () => {},
checkUrl = undefined,

Choose a reason for hiding this comment

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

please don't write undefined as a default value

you have already done this by adding ? here:
checkUrl?: (urlString: string) => boolean,

}) => {
// generage a unique id once on component load
const [id] = useState(() => `${name}-${getRandomDigits()}`);
Expand Down Expand Up @@ -55,6 +57,8 @@ export const TextField: React.FC<Props> = ({
{hasError && (
<p className="help is-danger">{`${label} is required`}</p>
)}

{((touched && value) && (checkUrl && !checkUrl(value))) && (<p className="help is-danger">{`${label} incorrect URL`}</p>)}

Choose a reason for hiding this comment

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

You can write this condition in constant ( too long )

</div>
);
};