Skip to content

Commit

Permalink
solved
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolas-hallwil committed Feb 1, 2025
1 parent 9663976 commit 950a685
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 15 deletions.
19 changes: 17 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,30 @@ import './App.scss';
import { MoviesList } from './components/MoviesList';
import { NewMovie } from './components/NewMovie';
import moviesFromServer from './api/movies.json';
import { useState } from 'react';

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

export const App = () => {
const [movies, setMovie] = useState([...moviesFromServer]);

const onAdd = (movie: Movie) => {
setMovie(currentMovies => [...currentMovies, movie]);
};

return (
<div className="page">
<div className="page-content">
<MoviesList movies={moviesFromServer} />
<MoviesList movies={movies} />
</div>
<div className="sidebar">
<NewMovie /* onAdd={(movie) => {}} */ />
<NewMovie onAdd={onAdd} />
</div>
</div>
);
Expand Down
74 changes: 62 additions & 12 deletions src/components/NewMovie/NewMovie.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,87 @@
import { useState } from 'react';
import React, { useState } from 'react';
import { TextField } from '../TextField';
import { Movie } from '../../App';

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

export const NewMovie: React.FC<Props> = ({ onAdd }) => {
const [count, setCount] = useState(0);
const [title, setTitle] = useState('');
const [descr, setDescr] = useState('');
const [img, setImg] = useState('');
const [url, setUrl] = useState('');
const [id, setId] = useState('');
const isValid = title.trim() && img.trim() && url.trim() && id.trim();

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

if (!isValid) {
return;
}

setCount(count + 1);
onAdd({
title,
description: descr,
imgUrl: img,
imdbUrl: url,
imdbId: id,
});
};

return (
<form className="NewMovie" key={count}>
<form className="NewMovie" key={count} onSubmit={handleSubmit}>
<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="" />
<TextField
name="description"
label="Description"
value={descr}
onChange={setDescr}
/>

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

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

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

<div className="field is-grouped">
<div className="control">
<button
type="submit"
data-cy="submit-button"
className="button is-link"
disabled={!isValid}
>
Add
</button>
Expand Down
18 changes: 17 additions & 1 deletion src/components/TextField/TextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ function getRandomDigits() {
return Math.random().toFixed(16).slice(2);
}

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

Check failure on line 18 in src/components/TextField/TextField.tsx

View workflow job for this annotation

GitHub Actions / run_linter (20.x)

This line has a length of 169. Maximum allowed is 80

export const TextField: React.FC<Props> = ({
name,
value,
Expand All @@ -28,6 +31,7 @@ export const TextField: React.FC<Props> = ({
// To show errors only if the field was touched (onBlur)
const [touched, setTouched] = useState(false);
const hasError = touched && required && !value;
const [validMessage, setValidMessage] = useState('');

return (
<div className="field">
Expand All @@ -46,11 +50,23 @@ export const TextField: React.FC<Props> = ({
placeholder={placeholder}
value={value}
onChange={event => onChange(event.target.value)}
onBlur={() => setTouched(true)}
onBlur={() => {
setTouched(true);
if (
(name === 'imgUrl' || name === 'imdbUrl') &&
!pattern.test(value)
) {
console.log(pattern.test(value));

Check failure on line 59 in src/components/TextField/TextField.tsx

View workflow job for this annotation

GitHub Actions / run_linter (20.x)

Unexpected console statement
setValidMessage('Not valid URL');
} else {
setValidMessage('');
}
}}
/>
</div>

{hasError && <p className="help is-danger">{`${label} is required`}</p>}
{validMessage && <p className="help is-danger">{`Not valid URL`}</p>}
</div>
);
};

0 comments on commit 950a685

Please sign in to comment.