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

init commit #2740

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
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://nex1994.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 onAdd = (movie: Movie) => {
setMovies([...movies, 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
84 changes: 74 additions & 10 deletions src/components/NewMovie/NewMovie.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,98 @@
import { useState } from 'react';
import { FormEventHandler, useState } from 'react';
import { TextField } from '../TextField';
import { Movie } from '../../types/Movie';

export const NewMovie = () => {
type Props = {
onAdd: (movie: Movie) => void;
};

export const NewMovie = ({ onAdd }: Props) => {
// 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 [titleInput, setTitleInput] = useState('');
const [descriptionInput, setDescriptionInput] = useState('');
const [imgUrlInput, setImgUrlInput] = useState('');
const [imdbUrlInput, setImdbUrlInput] = useState('');
const [imdbIdInput, setimdbIdInput] = useState('');
const increaseCount = () => {
setCount(count + 1);
};

const successfulSubmission =
titleInput !== '' &&
imgUrlInput !== '' &&
imdbUrlInput !== '' &&
imdbIdInput !== '';

const handleSubmit: FormEventHandler = event => {
event.preventDefault();
increaseCount();
const movie: Movie = {
title: titleInput.trimEnd(),
description: descriptionInput.trimEnd(),
imgUrl: imgUrlInput.trimEnd(),
imdbUrl: imdbUrlInput.trimEnd(),
imdbId: imdbIdInput.trimEnd(),
};

if (successfulSubmission) {
onAdd(movie);
setTitleInput('');
setDescriptionInput('');
setImgUrlInput('');
setImdbUrlInput('');
setimdbIdInput('');
}
};

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={titleInput}
onChange={setTitleInput}

Choose a reason for hiding this comment

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

The onChange prop should be a function that takes an event and updates the state. Make sure that setTitleInput and other similar functions are correctly handling the event parameter. Typically, it should be something like onChange={(e) => setTitleInput(e.target.value)}.

required
/>

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

Choose a reason for hiding this comment

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

Similar to the previous comment, ensure that setDescriptionInput correctly handles the event parameter to update the state.

name="description"
label="Description"
value={descriptionInput}
/>

<TextField name="imgUrl" label="Image URL" value="" />
<TextField
onChange={setImgUrlInput}

Choose a reason for hiding this comment

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

Ensure that setImgUrlInput correctly handles the event parameter to update the state.

name="imgUrl"
label="Image URL"
value={imgUrlInput}
required
/>

<TextField name="imdbUrl" label="Imdb URL" value="" />
<TextField
onChange={setImdbUrlInput}

Choose a reason for hiding this comment

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

Ensure that setImdbUrlInput correctly handles the event parameter to update the state.

name="imdbUrl"
label="Imdb URL"
value={imdbUrlInput}
required
/>

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

Choose a reason for hiding this comment

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

Ensure that setimdbIdInput correctly handles the event parameter to update the state.

name="imdbId"
label="Imdb ID"
value={imdbIdInput}
required
/>

<div className="field is-grouped">
<div className="control">
<button
disabled={!successfulSubmission}
type="submit"
data-cy="submit-button"
className="button is-link"
Expand Down
6 changes: 3 additions & 3 deletions src/components/TextField/TextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type Props = {
label?: string;
placeholder?: string;
required?: boolean;
onChange?: (newValue: string) => void;
onChange: (input: string) => void;
};

function getRandomDigits() {
Expand All @@ -20,7 +20,7 @@ export const TextField: React.FC<Props> = ({
label = name,
placeholder = `Enter ${label}`,
required = false,
onChange = () => {},
onChange,
}) => {
// generate a unique id once on component load
const [id] = useState(() => `${name}-${getRandomDigits()}`);
Expand All @@ -45,7 +45,7 @@ export const TextField: React.FC<Props> = ({
})}
placeholder={placeholder}
value={value}
onChange={event => onChange(event.target.value)}
onChange={event => onChange(event.target.value.trimStart())}
Copy link

Choose a reason for hiding this comment

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

Use trim method, to trim from both sides

onBlur={() => setTouched(true)}
/>
</div>
Expand Down