Skip to content

Initial Commit #102

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

Open
wants to merge 3 commits into
base: main
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
17,043 changes: 17,021 additions & 22 deletions client/package-lock.json

Large diffs are not rendered by default.

12 changes: 10 additions & 2 deletions client/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import EditMovieForm from './components/EditMovieForm';
import FavoriteMovieList from './components/FavoriteMovieList';

import axios from 'axios';
import AddMovieForm from "./components/AddMovieForm";

const App = (props) => {

const App = () => {
const [movies, setMovies] = useState([]);
const [favoriteMovies, setFavoriteMovies] = useState([]);

Expand All @@ -26,6 +28,7 @@ const App = (props) => {
}, []);

const deleteMovie = (id)=> {
setMovies(movies.filter(movie => movie.id !== id))
}

const addToFavorites = (movie) => {
Expand All @@ -45,16 +48,21 @@ const App = (props) => {

<Switch>
<Route path="/movies/edit/:id">
<EditMovieForm setMovies={setMovies}/>
</Route>

<Route path="/movies/:id">
<Movie/>
<Movie deleteMovie={deleteMovie} />
</Route>

<Route path="/movies">
<MovieList movies={movies}/>
</Route>

<Route path='/add-movie'>
<AddMovieForm setMovies={setMovies} />
</Route>

<Route path="/">
<Redirect to="/movies"/>
</Route>
Expand Down
81 changes: 81 additions & 0 deletions client/src/components/AddMovieForm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import React, { useState } from 'react';
import { useHistory } from 'react-router-dom';
import { Link } from 'react-router-dom';
import axios from 'axios';

const AddMovieForm = (props) => {
const { push } = useHistory();

const { setMovies } = props;

const initialValues = {
title:"",
director: "",
genre: "",
metascore: 0,
description: ""
}

const [movie, setMovie] = useState(initialValues);

const handleChange = (e) => {
setMovie({
...movie,
[e.target.name]: e.target.value
});
}

const handleSubmit = (e) => {
e.preventDefault();
axios.post(`http://localhost:5000/api/movies/`, movie)
.then(resp => {
setMovies(resp.data)
push('/movies/')
})
.catch(err => {
console.log(err);
})
}

const { title, director, genre, metascore, description } = movie;
return (
<div className="col">
<div className="modal-content">
<form onSubmit={handleSubmit}>
<div className="modal-header">
<h4 className="modal-title">Add<strong>{movie.title}</strong></h4>
</div>
<div className="modal-body">
<div className="form-group">
<label>Title</label>
<input value={title} onChange={handleChange} name="title" type="text" className="form-control"/>
</div>
<div className="form-group">
<label>Director</label>
<input value={director} onChange={handleChange} name="director" type="text" className="form-control"/>
</div>
<div className="form-group">
<label>Genre</label>
<input value={genre} onChange={handleChange} name="genre" type="text" className="form-control"/>
</div>
<div className="form-group">
<label>Metascore</label>
<input value={metascore} onChange={handleChange} name="metascore" type="number" className="form-control"/>
</div>
<div className="form-group">
<label>Description</label>
<textarea value={description} onChange={handleChange} name="description" className="form-control"></textarea>
</div>

</div>
<div className="modal-footer">
<input type="submit" className="btn btn-info" value="Add"/>
<Link to={`/movies/`}><input type="button" className="btn btn-default" value="Cancel"/></Link>
</div>
</form>
</div>
</div>
);
}

export default AddMovieForm;
25 changes: 22 additions & 3 deletions client/src/components/EditMovieForm.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React, { useState, useEffect } from 'react';
import { useParams, useHistory } from 'react-router-dom';
import { Link } from 'react-router-dom';

import axios from 'axios';

const EditMovieForm = (props) => {
const { push } = useHistory();
const { id } = useParams();

const [movie, setMovie] = useState({
title:"",
Expand All @@ -14,7 +14,17 @@ const EditMovieForm = (props) => {
metascore: 0,
description: ""
});


useEffect(() => {
axios.get(`http://localhost:5000/api/movies/${id}`)
.then(resp => {
setMovie(resp.data)
})
.catch(err => {
console.log(err);
})
}, [])

const handleChange = (e) => {
setMovie({
...movie,
Expand All @@ -25,6 +35,15 @@ const EditMovieForm = (props) => {
const handleSubmit = (e) => {
e.preventDefault();
}

axios.put(`http://localhost:5000/api/movies/${id}`, movie)
.then(resp => {
push(`/movies/${id}`)
props.setMovie(resp.data)
})
.catch(err => {
console.log(err);
})

const { title, director, genre, metascore, description } = movie;

Expand Down Expand Up @@ -60,7 +79,7 @@ const EditMovieForm = (props) => {
</div>
<div className="modal-footer">
<input type="submit" className="btn btn-info" value="Save"/>
<Link to={`/movies/1`}><input type="button" className="btn btn-default" value="Cancel"/></Link>
<Link to={`/movies/${id}`}><input type="button" className="btn btn-default" value="Cancel"/></Link>
</div>
</form>
</div>
Expand Down
16 changes: 13 additions & 3 deletions client/src/components/Movie.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Link, useParams, useHistory } from 'react-router-dom';
import axios from 'axios';

const Movie = (props) => {
const { addToFavorites } = props;
const { addToFavorites, deleteMovie } = props;

const [movie, setMovie] = useState('');

Expand All @@ -21,6 +21,16 @@ const Movie = (props) => {
})
}, [id]);

const handleDelete = () => {
axios.delete(`http://localhost:5000/api/movies/${id}`)
.then(resp => {
deleteMovie(resp.data)
push('/movies')
})
.catch(err => {
console.log(err);
})
}
return(<div className="modal-page col">
<div className="modal-dialog">
<div className="modal-content">
Expand Down Expand Up @@ -51,8 +61,8 @@ const Movie = (props) => {

<section>
<span className="m-2 btn btn-dark">Favorite</span>
<Link to={`/movies/edit/${movie.id}`} className="m-2 btn btn-success">Edit</Link>
<span className="delete"><input type="button" className="m-2 btn btn-danger" value="Delete"/></span>
<Link to={`/movies/edit/${id}`} className="m-2 btn btn-success">Edit</Link>
<span onClick={handleDelete} className="delete"><input type="button" className="m-2 btn btn-danger" value="Delete"/></span>
</section>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/MovieHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const MovieHeader = ()=> {
<h2>IMDB Movie Database</h2>
</div>
<div className="col-sm-6">
<Link className="btn btn-success"><i className="material-icons">&#xE147;</i> <span>Add New Movie</span></Link>
<Link to='/movies/add/' className="btn btn-success"><i className="material-icons">&#xE147;</i> <span>Add New Movie</span></Link>
<Link to="/movies" className="btn btn-primary">View All Movies</Link>
</div>
</div>
Expand Down
Loading