Skip to content

Commit

Permalink
add react form
Browse files Browse the repository at this point in the history
  • Loading branch information
AnoriFrell committed Jan 23, 2024
1 parent c9e7a5e commit c783f01
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 12 deletions.
12 changes: 10 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import './App.scss';
import { useState } from 'react';
import { MoviesList } from './components/MoviesList';
import { NewMovie } from './components/NewMovie';
import moviesFromServer from './api/movies.json';
import { Movie } from './types/Movie';

export const App = () => {
const [movies, setMovies] = useState<Movie[]>(moviesFromServer);

const addMovie = (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={addMovie} />
</div>
</div>
);
Expand Down
116 changes: 106 additions & 10 deletions src/components/NewMovie/NewMovie.tsx
Original file line number Diff line number Diff line change
@@ -1,45 +1,140 @@
import { useState } from 'react';
import React, { useState } from 'react';
import { TextField } from '../TextField';
import { Movie } from '../../types/Movie';

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

export const NewMovie: React.FC<Props> = ({ 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 [movieData, setMovieData] = useState({
count: 0,
title: '',
description: '',
imgUrl: '',
imdbUrl: '',
imdbId: '',
});

const {
count,
title,
description,
imgUrl,
imdbUrl,
imdbId,
} = movieData;

const reset = () => {
setMovieData((prevData) => ({
...prevData,
count: prevData.count + 1,
title: '',
description: '',
imgUrl: '',
imdbUrl: '',
imdbId: '',
}));
};

const handleAdd = (event: React.FormEvent) => {
event.preventDefault();
onAdd({
title,
description,
imgUrl,
imdbUrl,
imdbId,
});
reset();
};

const checkCorrect = !title.trim()
|| !imgUrl.trim()
|| !imdbUrl.trim()
|| !imdbId.trim();

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

<TextField
name="title"
label="Title"
value=""
onChange={() => {}}
value={title}
onChange={(newValue) => setMovieData(
{
...movieData,
title: newValue,
},
)}
required
/>

<TextField
name="description"
label="Description"
value=""
value={description}
onChange={(newValue) => setMovieData(
{
...movieData,
description: newValue,
},
)}
/>

<TextField
name="imgUrl"
label="Image URL"
value=""
value={imgUrl}
onChange={(newValue) => setMovieData(
{
...movieData,
imgUrl: newValue,
},
)}
required
/>

<TextField
name="imdbUrl"
label="Imdb URL"
value=""
value={imdbUrl}
onChange={(newValue) => setMovieData(
{
...movieData,
imdbUrl: newValue,
},
)}
required
/>

<TextField
name="imdbId"
label="Imdb ID"
value=""
value={imdbId}
onChange={(newValue) => setMovieData(
{
...movieData,
imdbId: newValue,
},
)}
required
/>

<div className="field is-grouped">
Expand All @@ -48,6 +143,7 @@ export const NewMovie = () => {
type="submit"
data-cy="submit-button"
className="button is-link"
disabled={checkCorrect}
>
Add
</button>
Expand Down

0 comments on commit c783f01

Please sign in to comment.