Skip to content

completed #97

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 1 commit 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
6 changes: 4 additions & 2 deletions client/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React, { useEffect, useState } from "react";
import { Route, Switch, Redirect } from "react-router-dom";
import MovieList from './components/MovieList';
import Movie from './components/Movie';

import AddMovieForm from './components/AddMovieForm';
import MovieHeader from './components/MovieHeader';

import EditMovieForm from './components/EditMovieForm';
Expand All @@ -26,6 +26,7 @@ const App = (props) => {
}, []);

const deleteMovie = (id)=> {
setMovies(movies.filter(item => (item.id !== Number(id))));
}

const addToFavorites = (movie) => {
Expand All @@ -45,10 +46,11 @@ 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">
Expand Down
78 changes: 78 additions & 0 deletions client/src/components/AddMovieForm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import React, {useState, useEffect} from 'react';
import {useParams, useHistory} from 'react-router-dom';
import axios from 'axios';
import {Link} from 'react-router-dom';

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

const [movie, setMovie] = useState({
title: '',
director: '',
genre: '',
metascore: 0,
description: '',
});

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

const handleSubmit = (event) => {
event.preventDefault();
axios.post(`https://localhost:5000/api/movies`, movie)
.then(res => {
console.log(res);
props.setMovies(res.data);
push(`/movies`);
})
.catch(err => {
console.log(err.response);
})
}

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

return (
<div>
<div>
<form onSubmit = {handleSubmit}>
<div>
<h4>Adding {movie.title}</h4>
</div>
<div>
<div>
<label>Title</label>
<input value = {title} onChange = {handleChange} name = 'title' type = 'text' />
</div>
<div>
<label>Director</label>
<input value = {director} onChange = {handleChange} name = 'director' type = 'text' />
</div>
<div>
<label>Genre</label>
<input value = {genre} onChange = {handleChange} name = 'genre' type = 'text' />
</div>
<div>
<label>Metascore</label>
<input value = {metascore} onChange = {handleChange} name = 'metascore' type = 'number' />
</div>
<div>
<label>Description</label>
<input value = {description} onChange = {handleChange} name = 'description' />
</div>
</div>
<div>
<input type = 'submit' value = 'Save' />
<Link to = {`/movies`}><input type = 'button' calue = 'Cancel' /></Link>
</div>
</form>
</div>
</div>
)
}

export default AddMovieForm;
23 changes: 22 additions & 1 deletion client/src/components/EditMovieForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import axios from 'axios';

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

const [movie, setMovie] = useState({
title:"",
Expand All @@ -22,8 +23,28 @@ const EditMovieForm = (props) => {
});
}

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

const handleSubmit = (e) => {
e.preventDefault();
axios.put(`https://localhost:5000/api/movies/${id}`, movie)
.then(resp => {
console.log(resp)
props.setMovies(resp.data);
push(`/movies/${id}`);
})
.catch(err => {
console.log(err.response);
})

}

const { title, director, genre, metascore, description } = movie;
Expand Down Expand Up @@ -60,7 +81,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`}><input type="button" className="btn btn-default" value="Cancel"/></Link>
</div>
</form>
</div>
Expand Down
11 changes: 10 additions & 1 deletion client/src/components/Movie.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ const Movie = (props) => {
})
}, [id]);

const handleDelete = () => {
axios.delete(`https://localhost:5000/api/movies/${id}`)
.then(res => {
props.deleteMovies(id);
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 @@ -52,7 +61,7 @@ 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>
<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