Skip to content
This repository has been archived by the owner on Aug 16, 2019. It is now read-only.

Commit

Permalink
Add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jrawsthorne committed Jun 4, 2018
1 parent f47e26f commit 77bcf12
Show file tree
Hide file tree
Showing 37 changed files with 189 additions and 30 deletions.
6 changes: 6 additions & 0 deletions src/actions/authActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import { LOGIN, LOGOUT, FETCH_USER_RATINGS, FETCH_USER_SUBSCRIPTIONS } from './t

export const login = () => ({
type: LOGIN,
/* server side login */
payload: axios.post(`${process.env.API_URL}/users/login`, { accessToken: Cookie.get('access_token') }).then((res) => {
/* all future api requests send the authorization token */
axios.defaults.headers.common.Authorization = `Bearer ${res.data.token}`;
return res.data;
}).catch(() => {
/* remove the tokens if there's an error logging in */
Cookie.remove('access_token');
Cookie.remove('token');
throw new Error('Login failed');
Expand All @@ -19,6 +22,7 @@ export const login = () => ({
});

export const logout = () => (dispatch) => {
/* remove both tokens used to authenticate */
steemConnectAPI.revokeToken();
Cookie.remove('access_token');
Cookie.remove('token');
Expand All @@ -27,12 +31,14 @@ export const logout = () => (dispatch) => {
});
};

/* fetch all the ratings for the current user */
export const fetchUserRatings = () => ({
type: FETCH_USER_RATINGS,
payload: axios.get(`${process.env.API_URL}/users/ratings`).then(res => res.data),
meta: { globalError: 'Error fetching ratings' },
});

/* fetch all the shows and movies the user has subscribed to */
export const fetchUserSubscriptions = () => ({
type: FETCH_USER_SUBSCRIPTIONS,
payload: axios.get(`${process.env.API_URL}/users/subscriptions`).then(res => res.data),
Expand Down
39 changes: 20 additions & 19 deletions src/actions/mediaActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ export const fetchMovie = id => ({
title: movie.title,
overview: movie.overview,
year: movie.release_date && new Date(movie.release_date).getFullYear(),
actors: movie.credits.cast.slice(0, 3),
genres: movie.genres.slice(0, 3),
similar: _.orderBy(movie.similar.results, 'popularity', 'desc').slice(0, 10),
actors: movie.credits.cast.slice(0, 3), /* Top 3 actors */
genres: movie.genres.slice(0, 3), /* Top 3 genres */
similar: _.orderBy(movie.similar.results, 'popularity', 'desc').slice(0, 10), /* Top 10 highest rated similar movies */
})),
meta: {
globalError: "Sorry, we couln't find that movie",
Expand All @@ -37,8 +37,8 @@ export const fetchMovie = id => ({
export const fetchActor = id => ({
type: FETCH_ACTOR,
payload: theMovieDBAPI.personInfo({ id, append_to_response: 'combined_credits' }).then(person => ({
...person,
combined_credits: _.orderBy(_.uniqBy(person.combined_credits.cast, 'id'), 'popularity', 'desc').slice(0, 10),
...person, /* The person's general info (name, bio) */
combined_credits: _.orderBy(_.uniqBy(person.combined_credits.cast, 'id'), 'popularity', 'desc').slice(0, 10), /* Top 10 highest rated shows/movies they've been in */
})),
meta: {
globalError: "Sorry, we couln't find that preson",
Expand All @@ -61,9 +61,9 @@ export const fetchShow = id => ({
overview: show.overview,
year: show.first_air_date && new Date(show.first_air_date).getFullYear(),
seasons: arrayToObject(show.seasons, 'season_number'),
actors: show.credits.cast.slice(0, 3),
genres: show.genres.slice(0, 3),
similar: _.orderBy(show.similar.results, 'popularity', 'desc').slice(0, 10),
actors: show.credits.cast.slice(0, 3), /* Top 3 actors */
genres: show.genres.slice(0, 3), /* Top 3 genres */
similar: _.orderBy(show.similar.results, 'popularity', 'desc').slice(0, 10), /* Top 10 highest rated similar shows */
})),
meta: {
globalError: "Sorry, we couln't find that show",
Expand Down Expand Up @@ -92,14 +92,14 @@ export const fetchSeason = (id, seasonNum) => ({
backdrop_path: show.backdrop_path,
title: show.name,
overview: show.overview,
actors: show.credits.cast.slice(0, 3),
genres: show.genres.slice(0, 3),
similar: _.orderBy(show.similar.results, 'popularity', 'desc').slice(0, 10),
actors: show.credits.cast.slice(0, 3), /* Top 3 actors */
genres: show.genres.slice(0, 3), /* Top 3 genres */
similar: _.orderBy(show.similar.results, 'popularity', 'desc').slice(0, 10), /* Top 10 highest rated similar shows */
seasons: {
...arrayToObject(show.seasons, 'season_number'),
...arrayToObject(show.seasons, 'season_number'), /* each season with number as key */
[seasonNum]: {
...arrayToObject(show.seasons, 'season_number')[seasonNum],
episodes: arrayToObject(show[`season/${seasonNum}`].episodes, 'episode_number'),
episodes: arrayToObject(show[`season/${seasonNum}`].episodes, 'episode_number'), /* each episode with number as key */
},
},
});
Expand Down Expand Up @@ -132,14 +132,14 @@ export const fetchEpisode = (id, seasonNum, episodeNum) => ({
backdrop_path: show.backdrop_path,
title: show.name,
overview: show.overview,
actors: show.credits.cast.slice(0, 3),
genres: show.genres.slice(0, 3),
similar: _.orderBy(show.similar.results, 'popularity', 'desc').slice(0, 10),
actors: show.credits.cast.slice(0, 3), /* Top 3 actors */
genres: show.genres.slice(0, 3), /* Top 3 genres */
similar: _.orderBy(show.similar.results, 'popularity', 'desc').slice(0, 10), /* Top 10 highest rated similar shows */
seasons: {
...arrayToObject(show.seasons, 'season_number'),
...arrayToObject(show.seasons, 'season_number'), /* each season with number as key */
[seasonNum]: {
...arrayToObject(show.seasons, 'season_number')[seasonNum],
episodes: arrayToObject(show[`season/${seasonNum}`].episodes, 'episode_number'),
episodes: arrayToObject(show[`season/${seasonNum}`].episodes, 'episode_number'), /* each episode with number as key */
},
},
});
Expand All @@ -152,6 +152,7 @@ export const fetchEpisode = (id, seasonNum, episodeNum) => ({
},
});

/* change the rating of a show or movie */
export const userRateChange = (
mediaType,
tmdbid,
Expand All @@ -177,7 +178,7 @@ export const userRateChange = (
},
});


/* subscribe or unsubscribe from a show or movie */
export const subscribeChange = (type, tmdbid, subscribed) => ({
type: SUBSCRIBE_CHANGE,
payload: axios.post(`${process.env.API_URL}/subscriptions/change`, {
Expand Down
11 changes: 7 additions & 4 deletions src/actions/postActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const fetchPost = (author, permlink) => ({
.then(post =>
steemAPI.getContentAsync(post.author, post.permlink)
.then(steemPost => getPostData(steemPost, post)))
.then(steemPost => steemPost.id !== 0 && steemPost),
.then(steemPost => steemPost.id !== 0 && steemPost), /* only return post if it exists */
meta: {
author,
permlink,
Expand All @@ -52,6 +52,7 @@ export const fetchPosts = ({
subscriptions = false,
}) => (dispatch, getState) => {
let query;
/* change api request based on type of feed */
if (subscriptions) {
query = 'subscriptions';
} else {
Expand All @@ -73,16 +74,17 @@ export const fetchPosts = ({
},
})
.then(res =>
Promise.all(res.data.results.map(post =>
Promise.all(res.data.results.map(post => /* loop through each post from the db */
(!_.get(posts, `@${post.author}/${post.permlink}`) ?
/* get the post from steem if not stored */
steemAPI.getContentAsync(post.author, post.permlink)
.then(steemPost => getPostData(steemPost, post))
.catch(() => Promise.resolve(null))
.catch(() => Promise.resolve(null)) /* carry on if error finding post */
: _.get(posts, `@${post.author}/${post.permlink}`)
)))
.then(p => ({
count: res.data.count,
posts: p.filter(post => post !== null),
posts: p.filter(post => post !== null), /* eliminate posts that errored */
}))),
meta: {
globalError: 'Sorry, there was an error fetching posts',
Expand All @@ -95,6 +97,7 @@ export const fetchPosts = ({
});
};

/* Update new post form data */
export const newPostInfo = data => ({
type: NEW_POST_INFO,
payload: data,
Expand Down
1 change: 1 addition & 0 deletions src/actions/userActions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import steemAPI from '../apis/steemAPI';
import { FETCH_ACCOUNT } from './types';

/* fetch account details by username */
export const fetchAccount = name => ({
type: FETCH_ACCOUNT,
payload: steemAPI.getAccountsAsync([name]).then((result) => {
Expand Down
2 changes: 2 additions & 0 deletions src/apis/config/passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ module.exports = (passport) => {
User.findById(payload.id)
.then((user) => {
if (user) {
/* user found, send user object to route */
return done(null, user);
}
/* else throw an error */
return done(null, false);
});
}));
Expand Down
1 change: 1 addition & 0 deletions src/apis/models/Post.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,6 @@ const schema = new mongoose.Schema(
},
);

/* Each author and permlink combination must be unique */
schema.index({ author: 1, permlink: 1 }, { unique: true });
module.exports = mongoose.model('posts', schema);
1 change: 1 addition & 0 deletions src/apis/models/Rating.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const schema = new Schema(
},
);

/* A user can only have 1 rating per item */
schema.index({
user: 1, mediaType: 1, tmdbid: 1, seasonNum: 1, episodeNum: 1,
}, { unique: true });
Expand Down
1 change: 1 addition & 0 deletions src/apis/models/Subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ const schema = new Schema(
},
);

/* A user can only subscribe to 1 item */
schema.index({ user: 1, tmdbid: 1, type: 1 }, { unique: true });
module.exports = mongoose.model('subscriptions', schema);
7 changes: 6 additions & 1 deletion src/components/media/Links.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ import { Link } from 'react-router-dom';
import { Icon } from 'antd';
import * as actions from '../../actions/postActions';

/* Links to go back to the main item or the season page */
const Links = ({
mediaType, tmdbid, seasonNum, mediaItem, isPostPage, isNewPostPage, newPostInfo,
}) => {
if (mediaType === 'season' || mediaType === 'episode') {
if (!isNewPostPage) {
return (
/* Link to the main show page */
<div className="MediaHeader__info__links">
<Link to={`/show/${tmdbid}`} >
<Icon type="arrow-left" /> {mediaItem.title}
</Link>
{mediaType === 'episode' && (
/* Link to the season page */
<Link to={`/show/${tmdbid}/season/${seasonNum}`} >
<Icon type="arrow-left" /> {mediaItem.seasons[seasonNum].name}
</Link>
Expand All @@ -25,11 +28,12 @@ const Links = ({
}
return (
<div className="MediaHeader__info__links">
{/* Change the new post info to the main show */}
<p onClick={() => newPostInfo({ seasonNum: null, episodeNum: null, mediaType: 'show' })} role="presentation" >
<Icon type="arrow-left" /> {mediaItem.title}
</p>
{mediaType === 'episode' && (
<p onClick={() => newPostInfo({ episodeNum: null, mediaType: 'season' })} role="presentation" >
<p onClick={() => newPostInfo({ episodeNum: null, mediaType: 'season' })} role="presentation" > {/* Change the new post info to the season */}
<Icon type="arrow-left" /> {mediaItem.seasons[seasonNum].name}
</p>
)}
Expand All @@ -38,6 +42,7 @@ const Links = ({
} else if (isPostPage) {
return (
<div className="MediaHeader__info__links">
{/* Link to the movie or show page from a post */}
<Link to={`/${mediaType}/${tmdbid}`} >
<Icon type="arrow-left" /> {mediaItem.title}
</Link>
Expand Down
12 changes: 12 additions & 0 deletions src/components/media/Media.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const Links = Loadable({
loading: (() => null),
});

/* Only available when logged in */
const AuthActions = ({
tmdbid, mediaType, seasonNum, episodeNum, isAuthenticated,
}) => {
Expand Down Expand Up @@ -60,11 +61,13 @@ const AuthActions = ({
<React.Fragment>
<div className="MediaHeader__info__rating">
<h4>Rating</h4>
{/* Link to log in if not authenticated */}
<LoginLink linkText="Log in to rate" />
</div>
</React.Fragment>);
};

/* Show a list of genres */
const Genres = ({ genres }) => (
<div className="MediaHeader__info__genres">
<h4>Genres</h4>
Expand All @@ -73,6 +76,7 @@ const Genres = ({ genres }) => (
</div>
);

/* Show a list of actors */
const Actors = ({ actors }) => (
<div className="MediaHeader__info__actors">
<h4>Actors</h4>
Expand All @@ -83,6 +87,7 @@ const Actors = ({ actors }) => (
</div>
);

/* Go to the next or previous episode or season */
const Switcher = ({ link, direction }) => {
if (link) {
return (
Expand All @@ -103,20 +108,24 @@ const backgroundImage = (backdropPath, opacity) => ({

const Media = props => (
<div className="MediaItem">
{/* Only show on larger screens */}
<MediaQuery query="(min-width: 768px)">
<div className="MediaItem__backdrop" style={backgroundImage(props.backdropPath, 0.5)} />
</MediaQuery>
<div className="MediaHeader">
<Switcher link={props.prev} direction="left" />
{/* Only show on larger screens */}
<MediaQuery query="(min-width: 768px)">
<div className="MediaHeader__poster">
<img alt="" src={props.poster} />
{/* Don't show auth actions on new post page */}
{!props.isNewPostPage && <AuthActions {...props} />}
</div>
</MediaQuery>
<div className="MediaHeader__info" style={backgroundImage(props.backdropPath, 0.8)}>
<div className="MediaHeader__info__title">
<h1>{props.title}</h1>
{/* Show new post button if authenticated */}
{props.isAuthenticated && !props.isNewPostPage &&
<NewPostButton
mediaType={props.mediaType}
Expand All @@ -127,16 +136,19 @@ const Media = props => (
/>
}
</div>
{/* Show links back if season, show or post page */}
{(props.mediaType === 'season' || props.mediaType === 'episode' || props.isPostPage) && <Links {...props} />}
<Row gutter={32} type="flex">
<Col xs={24} sm={24} lg={14}>
<div className="MediaHeader__info__overview">
<h4>Overview</h4>
<BodyShort body={props.overview} />
</div>
{/* popovers for episodes and seasons */}
<SelectorPopover type="season" list={props.seasons} tmdbid={props.tmdbid} seasonNum={props.seasonNum} />
<SelectorPopover type="episode" list={props.episodes} tmdbid={props.tmdbid} seasonNum={props.seasonNum} />
{!props.isNewPostPage &&
/* Show auth actions below if on mobile/tablet */
<MediaQuery query="(max-width: 768px)">
<AuthActions {...props} />
</MediaQuery>
Expand Down
6 changes: 6 additions & 0 deletions src/components/media/MediaContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class MediaContainer extends React.Component {
fetching,
loaded,
} = this.props;
/* Fetch metadata based on mediaType */
if (!mediaItem || (!fetching && !loaded)) {
if (mediaType === 'movie') fetchMovie(tmdbid);
if (mediaType === 'show') fetchShow(tmdbid);
Expand All @@ -46,6 +47,7 @@ class MediaContainer extends React.Component {
fetching,
loaded,
} = nextProps;
/* Fetch metadata based on mediaType */
if ((!mediaItem && !loaded && !fetching) || (!loaded && !fetching)) {
if (mediaType === 'movie') fetchMovie(tmdbid);
if (mediaType === 'show') fetchShow(tmdbid);
Expand All @@ -70,11 +72,13 @@ class MediaContainer extends React.Component {
} = this.props;
if (failed) return <div className="main-content"><p>Sorry, there was an error loading the metadata</p></div>;
if (fetching || !loaded) return <Loading />;
/* get details based on type */
const {
backdropPath, posterPath, title, overview,
} = getMediaItemDetails(mediaItem, mediaType, seasonNum, episodeNum);
let prev;
let next;
/* add next and previous buttons if episode or season */
if (seasonNum) {
const nextPrev = getNextPrev(mediaItem, seasonNum, episodeNum);
prev = _.get(nextPrev, 'prev');
Expand Down Expand Up @@ -138,6 +142,7 @@ const mapStateToProps = (state, ownProps) => {
} = ownProps;
let query = '';
let type;
/* match store structure */
if (mediaType === 'movie') {
type = 'movies';
} else if (mediaType === 'show' || mediaType === 'episode' || mediaType === 'season') {
Expand All @@ -148,6 +153,7 @@ const mapStateToProps = (state, ownProps) => {
if (seasonNum) query += `.seasons.${seasonNum}`;
const fetching = _.get(state.media.itemStates, `${query}.fetching`);
if (episodeNum) query += `.episodes.${episodeNum}`;
/* use season/episode failed and loaded states */
const failed = _.get(state.media.itemStates, `${query}.failed`);
const loaded = _.get(state.media.itemStates, `${query}.loaded`);
return {
Expand Down
Loading

0 comments on commit 77bcf12

Please sign in to comment.