Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexLiashenko19 committed Dec 22, 2024
1 parent 9663976 commit 7b16463
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ const pattern = /^((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=+$,\w]+@)?[A-Za-z0-9.-]+|(
- Implement a solution following the [React task guideline](https://github.com/mate-academy/react_task-guideline#react-tasks-guideline).
- Use the [React TypeScript cheat sheet](https://mate-academy.github.io/fe-program/js/extra/react-typescript).
- Open one more terminal and run tests with `npm test` to ensure your solution is correct.
- Replace `<your_account>` with your Github username in the [DEMO LINK](https://<your_account>.github.io/react_movies-list-add-form/) and add it to the PR description.
- Replace `<your_account>` with your Github username in the [DEMO LINK](https://AlexLiashenko19.github.io/react_movies-list-add-form/) and add it to the PR description.
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 addMovies = (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={addMovies} />
</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}
onReset={reset}
>
<h2 className="title">Add a movie</h2>

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

<TextField name="description" label="Description" value="" />
<TextField
name="description"
label={Fields.description}
value={description}
onChange={(newValue: string) => setDescription(newValue)}
/>

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

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

<TextField name="imdbId" label="Imdb ID" value="" />
<TextField
name="imdbId"
label={Fields.imdbId}
value={imdbId}
onChange={(newValue: string) => setImdbId(newValue)}
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)
}
>
Add
</button>
Expand Down

0 comments on commit 7b16463

Please sign in to comment.