Skip to content

Commit

Permalink
Adding To Watchlist And WatchedList Implemented.
Browse files Browse the repository at this point in the history
  • Loading branch information
MohammadAkthamObeidat committed Jan 30, 2020
1 parent 6ad885b commit b0bce09
Show file tree
Hide file tree
Showing 11 changed files with 503 additions and 63 deletions.
3 changes: 0 additions & 3 deletions controllers/UsersController.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ app.use(express.json());

// Add new movie to watch list.
const addMovieToWatchList = (req, res, next) => {
// console.log('MOVIE :', movie);
console.log('REQ.BODY :', req.body);
const { userID } = req.params;
// console.log('MOVIE :', movie);
user.addMovieToWatchList(userID, req.body, result => {
res.status(200).json({
status: 'Success Adding Movie To Watchlist.',
Expand Down
2 changes: 0 additions & 2 deletions models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,6 @@ const addUser = (newUser, callback) => {

// Add new movie to watch list.
const addMovieToWatchList = (userID, newMovie, callback) => {
console.log('USERID :', userID);
console.log('NEWMOVIE :', newMovie);
User.findOneAndUpdate(
{
_id: userID
Expand Down
2 changes: 2 additions & 0 deletions view/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import AuthHelper from './Utils/AuthHelper';
import axios from 'axios';
import MovieDetails from './Layouts/MovieDetails';
import ShowDetails from './Layouts/ShowDetails';
import SearchResults from './Layouts/SearchResults';
class App extends Component {
// App State.
constructor(props) {
Expand Down Expand Up @@ -66,6 +67,7 @@ class App extends Component {
<Route path="/register" component={Register} />
<Route path="/login" component={Login} />
<Route path="/profile" component={Profile} />
<Route path="/search-results" component={SearchResults} />
<Route
path="/discover/movies/popular"
component={DiscoverMovies}
Expand Down
94 changes: 80 additions & 14 deletions view/src/Layouts/DiscoverMovies.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,40 @@ import AuthHelper from '../Utils/AuthHelper';

class DiscoverMovies extends Component {
state = {
loadedMovie: {},
user: {},
popularMovies: [],
trendingMovies: [],
popularity: 'popular'
popularity: 'popular',
isMovieExistInWatchlist: false,
isMovieExistInWatchedlist: false
};

handleAddToWatchlist = async id => {
// Utility Function To Add Movies To WatchList.
handleAddMovieToWatchlist = async id => {
// Use AuthHelper Class To Get User ID.
const Auth = new AuthHelper();
const userID = Auth.getConfirm().id;
const token = Auth.getToken();

// Get User Information Who Want To Add To Watchlist.
const loadedUser = await axios.post(`/get-user/${userID}`);
this.setState({ ...this.state.user, user: loadedUser.data.user });

// Get All Movie Details According To Movie ID.
const loadedMovie = await axios.get(`/movies/details/${id}`);
const { movieDetails } = loadedMovie.data.data;

const { movies_list } = this.state.user;
movies_list.watch_list.map(movie => {
if (movie.id === id) {
return this.setState({ isMovieExistInWatchlist: true });
} else {
return this.setState({ isMovieExistInWatchlist: false });
}
});

// Add Movie To Authenticated User Watchlist.
if (movieDetails) {
if (movieDetails && this.state.isMovieExistInWatchlist === false) {
const addedMovie = await axios.patch(
`/user/movie/add/watchlist/${userID}`,
movieDetails,
Expand All @@ -33,8 +49,55 @@ class DiscoverMovies extends Component {
authorization: `Bearer ${token}`,
'Content-Type': 'application/json'
}
},
}
);

console.log('ADDED MOVIE :', addedMovie);
} else {
console.log('Movie Is Already Exist In Your Movies WatchList !!!');
}
};

// Utility Function To Add Movies To WatchList.
handleAddMovieToWatchedlist = async id => {
// Use AuthHelper Class To Get User ID.
const Auth = new AuthHelper();
const userID = Auth.getConfirm().id;
const token = Auth.getToken();

// Get User Information Who Want To Add To Watchlist.
const loadedUser = await axios.post(`/get-user/${userID}`);
this.setState({ ...this.state.user, user: loadedUser.data.user });

// Get All Movie Details According To Movie ID.
const loadedMovie = await axios.get(`/movies/details/${id}`);
const { movieDetails } = loadedMovie.data.data;

const { movies_list } = this.state.user;
movies_list.watched_list.map(movie => {
if (movie.id === id) {
return this.setState({ isMovieExistInWatchedlist: true });
} else {
return this.setState({ isMovieExistInWatchedlist: false });
}
});

// Add Movie To Authenticated User Watchlist.
if (movieDetails && this.state.isMovieExistInWatchedlist === false) {
const addedMovie = await axios.patch(
`/user/movie/add/watchedlist/${userID}`,
movieDetails,
{
headers: {
authorization: `Bearer ${token}`,
'Content-Type': 'application/json'
}
}
);

console.log('ADDED MOVIE :', addedMovie);
} else {
console.log('Movie Is Already Exist In Your Movies WatchList !!!');
}
};

Expand All @@ -61,8 +124,6 @@ class DiscoverMovies extends Component {
this.getPopularMovies();
};

handleItemClick = id => {};

render() {
const { popularMovies, trendingMovies, popularity } = this.state;
return this.state ? (
Expand Down Expand Up @@ -97,24 +158,29 @@ class DiscoverMovies extends Component {
? popularMovies.map(movie => {
return (
<MovieItem
addToWatchList={this.handleAddToWatchlist}
addToWatchList={
this.handleAddMovieToWatchlist
}
addToWatchedList={
this.handleAddMovieToWatchedlist
}
key={movie.id}
movie={movie}
clicked={() =>
this.handleItemClick(movie.id)
}
/>
);
})
: popularity === 'trending' && trendingMovies.length > 0
? trendingMovies.map(movie => {
return (
<MovieItem
addToWatchList={
this.handleAddMovieToWatchlist
}
addToWatchedList={
this.handleAddMovieToWatchedlist
}
key={movie.id}
movie={movie}
clicked={() =>
this.handleItemClick(movie.id)
}
/>
);
})
Expand Down
137 changes: 113 additions & 24 deletions view/src/Layouts/DiscoverShows.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,102 @@ import React, { Component } from 'react';
import axios from 'axios';
import ShowItem from '../components/ShowItem';
import '../Assets/CSS/Discover.css';
import { Link, NavLink } from 'react-router-dom';
import { NavLink } from 'react-router-dom';
import AuthHelper from '../Utils/AuthHelper';
class DiscoverShows extends Component {
state = {
user: {},
trendingShows: [],
popularShows: [],
popularity: 'popular'
popularity: 'popular',
isShowExistInWatchlist: false,
isShowExistInWatchedlist: false
};

// Utility Function To Add shows To WatchList.
handleAddShowToWatchlist = async id => {
// Use AuthHelper Class To Get User ID.
const Auth = new AuthHelper();
const userID = Auth.getConfirm().id;
const token = Auth.getToken();

// Get User Information Who Want To Add To Watchlist.
const loadedUser = await axios.post(`/get-user/${userID}`);
this.setState({ ...this.state.user, user: loadedUser.data.user });

// Get All show Details According To show ID.
const loadedShow = await axios.get(`/shows/details/${id}`);
const { showDetails } = loadedShow.data.data;

const { shows_list } = this.state.user;
shows_list.watch_list.map(show => {
if (show.id === id) {
return this.setState({ isShowExistInWatchlist: true });
} else {
return this.setState({ isShowExistInWatchlist: false });
}
});

// Add show To Authenticated User Watchlist.
if (showDetails && this.state.isShowExistInWatchlist === false) {
const addedShow = await axios.patch(
`/user/show/add/watchlist/${userID}`,
showDetails,
{
headers: {
authorization: `Bearer ${token}`,
'Content-Type': 'application/json'
}
}
);

console.log('ADDED SHOW :', addedShow);
} else {
console.log('Show Is Already Exist In Your Shows WatchList !!!');
}
};

// Utility Function To Add shows To WatchList.
handleAddShowToWatchedlist = async id => {
// Use AuthHelper Class To Get User ID.
const Auth = new AuthHelper();
const userID = Auth.getConfirm().id;
const token = Auth.getToken();

// Get User Information Who Want To Add To Watchlist.
const loadedUser = await axios.post(`/get-user/${userID}`);
this.setState({ ...this.state.user, user: loadedUser.data.user });

// Get All show Details According To show ID.
const loadedShow = await axios.get(`/shows/details/${id}`);
const { showDetails } = loadedShow.data.data;

const { shows_list } = this.state.user;
shows_list.watched_list.map(show => {
if (show.id === id) {
return this.setState({ isShowExistInWatchedlist: true });
} else {
return this.setState({ isShowExistInWatchedlist: false });
}
});

// Add show To Authenticated User Watchlist.
if (showDetails && this.state.isShowExistInWatchedlist === false) {
const addedShow = await axios.patch(
`/user/show/add/watchedlist/${userID}`,
showDetails,
{
headers: {
authorization: `Bearer ${token}`,
'Content-Type': 'application/json'
}
}
);

console.log('ADDED SHOW :', addedShow);
} else {
console.log('Show Is Already Exist In Your Shows WatchList !!!');
}
};

//@GET
Expand All @@ -34,9 +124,6 @@ class DiscoverShows extends Component {
this.getPopularShows();
};

handleItemClick = (id) => {

}
render() {
const { popularShows, trendingShows, popularity } = this.state;

Expand All @@ -59,41 +146,43 @@ class DiscoverShows extends Component {
to={{
pathname: '/discover/shows/popular'
}}
onClick={this.getPopularMovies}
onClick={this.getPopularShows}
className="header-btns"
>
Popular
</NavLink>
</div>
<hr className="line" />
</div>
<div className="movies-show-container">
<div className="shows-show-container">
{popularity === 'popular'
? popularShows.map(show => {
return (
<Link
to={'/show-details/' + show.id}
<ShowItem
key={show.id}
>
<ShowItem
show={show}
clicked={() =>
this.handleItemClick(show.id)
}
/>
</Link>
addToWatchlist={
this.handleAddShowToWatchlist
}
addToWatchedlist={
this.handleAddShowToWatchedlist
}
show={show}
/>
);
})
: popularity === 'trending'
? trendingShows.map(show => {
return (

<ShowItem
show={show}
clicked={() =>
this.handleItemClick(show.id)
}
/>
<ShowItem
key={show.id}
addToWatchlist={
this.handleAddShowToWatchlist
}
addToWatchedlist={
this.handleAddShowToWatchedlist
}
show={show}
/>
);
})
: ''}
Expand Down
Loading

0 comments on commit b0bce09

Please sign in to comment.