From b50990b4a8a8e154fa71f008fe65d13e20ee8009 Mon Sep 17 00:00:00 2001 From: mjrode Date: Tue, 9 Apr 2019 21:57:16 -0500 Subject: [PATCH] Refactor MediaList to use redux instead of local state --- client/src/actions/index.js | 7 ++- client/src/components/MediaCard.js | 26 +++++---- client/src/components/MediaList.js | 53 +++++++++++-------- .../src/components/plex/ImportPlexLibrary.js | 18 +++---- client/src/reducers/plexReducer.js | 10 ++-- 5 files changed, 68 insertions(+), 46 deletions(-) diff --git a/client/src/actions/index.js b/client/src/actions/index.js index ac7d66f..f4dcde2 100644 --- a/client/src/actions/index.js +++ b/client/src/actions/index.js @@ -3,6 +3,7 @@ export const types = { SET_LOADING: 'set_loading', FETCH_USER: 'fetch_user', FETCH_MEDIA_RESPONSE: 'fetch_media_response', + GET_MOST_WATCHED: 'get_most_watched', }; export const setLoading = loading => dispatch => { @@ -18,10 +19,14 @@ export const fetchUser = () => async dispatch => { export const fetchMedia = () => async dispatch => { dispatch({type: types.SET_LOADING, payload: true}); const res = await axios.get('/api/plex/import/all'); - console.log('action res', res); dispatch({type: types.FETCH_MEDIA_RESPONSE, payload: res.data}); }; +export const getMostWatched = params => async dispatch => { + const res = await axios.get('/api/recommend/most-watched'); + dispatch({type: types.GET_MOST_WATCHED, payload: res.data}); +}; + // export const fetchPlexToken = ( // username, // password, diff --git a/client/src/components/MediaCard.js b/client/src/components/MediaCard.js index 9ad8818..8085203 100644 --- a/client/src/components/MediaCard.js +++ b/client/src/components/MediaCard.js @@ -1,10 +1,10 @@ -import React, { Component } from 'react'; +import React, {Component} from 'react'; import PropTypes from 'prop-types'; -import { withStyles } from '@material-ui/core/styles'; -import { connect } from 'react-redux'; +import {withStyles} from '@material-ui/core/styles'; +import {connect} from 'react-redux'; import Header from './helpers/Header'; import styles from '../css/materialize.css'; -import { Link } from 'react-router-dom'; +import {Link} from 'react-router-dom'; class MediaCard extends Component { render() { const show = this.props.media; @@ -19,7 +19,7 @@ class MediaCard extends Component { className="card-image" style={{ boxShadow: - '0 2px 2px 0 rgba(0,0,0,0.14), 0 3px 1px -2px rgba(0,0,0,0.12), 0 1px 5px 0 rgba(0,0,0,0.2)' + '0 2px 2px 0 rgba(0,0,0,0.14), 0 3px 1px -2px rgba(0,0,0,0.12), 0 1px 5px 0 rgba(0,0,0,0.2)', }} > live_tvSimilar Shows @@ -68,7 +68,13 @@ class MediaCard extends Component {

{show.summary}

- This is a link + + live_tvSimilar Shows +
@@ -78,11 +84,11 @@ class MediaCard extends Component { } MediaCard.propTypes = { - classes: PropTypes.object.isRequired + classes: PropTypes.object.isRequired, }; -function mapStateToProps({ auth }) { - return { auth }; +function mapStateToProps({auth}) { + return {auth}; } export default connect(mapStateToProps)(withStyles(styles)(MediaCard)); diff --git a/client/src/components/MediaList.js b/client/src/components/MediaList.js index 831fabd..f835634 100644 --- a/client/src/components/MediaList.js +++ b/client/src/components/MediaList.js @@ -10,29 +10,34 @@ import {BrowserRouter, Route} from 'react-router-dom'; import Similar from './plex/Similar'; class MediaList extends Component { - state = {tvShowList: [], loading: false}; - componentDidMount() { - this.getMostWatched(); + this.props.getMostWatched(); + console.log(this.state); + console.log('props', this.props.tvShowList); } - getMostWatched = async params => { - const res = await axios.get('/api/recommend/most-watched'); - this.setState({tvShowList: res.data}); - }; + // getMostWatched = async params => { + // const res = await axios.get('/api/recommend/most-watched'); + // this.setState({tvShowList: res.data}); + // }; render() { - const mediaList = this.state.tvShowList.map(show => { - return ( -
- } - /> - -
- ); - }); - return
{mediaList}
; + console.log('RENDER---', this.props.tvShowList); + if (this.props.tvShowList) { + const mediaList = this.props.tvShowList.map(show => { + console.log('show-------', show); + return ( +
+ } + /> + +
+ ); + }); + return
{mediaList}
; + } + return
Loading
; } } @@ -40,8 +45,14 @@ MediaList.propTypes = { classes: PropTypes.object.isRequired, }; -function mapStateToProps({plex}) { - return {loading: plex.loading}; +function mapStateToProps(state) { + console.log('WHAT AMI!', state); + return { + loading: state.plex.loading, + plexToken: state.plex.plexToken, + tvShowList: state.plex.tvShowList, + mediaResponse: state.plex.mediaResponse, + }; } export default connect( diff --git a/client/src/components/plex/ImportPlexLibrary.js b/client/src/components/plex/ImportPlexLibrary.js index 4e26e03..ac8054e 100644 --- a/client/src/components/plex/ImportPlexLibrary.js +++ b/client/src/components/plex/ImportPlexLibrary.js @@ -1,9 +1,7 @@ -import React, { Component } from 'react'; +import React, {Component} from 'react'; import PropTypes from 'prop-types'; -import CssBaseline from '@material-ui/core/CssBaseline'; -import { withStyles } from '@material-ui/core/styles'; -import { connect } from 'react-redux'; -import TextHeader from '../helpers/Header'; +import {withStyles} from '@material-ui/core/styles'; +import {connect} from 'react-redux'; import styles from '../../css/materialize.css.js'; import '../../css/materialize.css'; import * as actions from '../../actions'; @@ -13,7 +11,7 @@ class ImportPlexLibrary extends Component { this.props.fetchMedia(); } render() { - const { classes } = this.props; + const {classes} = this.props; return (