diff --git a/src/app.js b/src/app.js new file mode 100644 index 0000000..ed40b1b --- /dev/null +++ b/src/app.js @@ -0,0 +1,63 @@ +import React, {Component} from 'react' +import Main from './components' +import Navbar from './components/navbar' +import Footer from './components/footer' +import Routes from './routes' + +import {connect} from 'react-redux' +import {fetchAllEpisodes, fetchAllCaptions} from './store' + +class App extends Component { + + constructor() { + super() + this.state = { loading: true } + } + + componentDidMount() { + console.log('App Loaded. Now Fetching Data...') + this.fetchData() + } + + async fetchData() { + await this.props.loadEpisodes() + await this.props.loadCaptions() + await this.setState({loading: false}) + } + + renderError() { + return
{this.state.error}
+ } + + renderApp() { + return ( +
+ + +
+ ) + } + + render() { + if (this.state.loading) return
+ return this.renderApp() + } +} + +const mapState = state => { + return { + episodes: state.episodes + } +} + +const mapDispatch = dispatch => { + return { + loadEpisodes: () => dispatch(fetchAllEpisodes()), + loadCaptions: () => dispatch(fetchAllCaptions()) + } +} + + + +export default connect(mapState, mapDispatch)(App) diff --git a/src/components/index.js b/src/components/index.js index 00b96c1..12f2ea6 100644 --- a/src/components/index.js +++ b/src/components/index.js @@ -1,101 +1,23 @@ -import React, { Component } from 'react' -import axios from 'axios' -import { Episodes } from './episodes' -import { Captions } from './captions' -import fetchEpisodes from './fetchEpisodes' -import { Form, Button } from 'react-bootstrap' -class App extends Component { - constructor() { - super() - this.state = { savedComments: [], loading: true } - this.fetchEpisodeComments = this.fetchEpisodeComments.bind(this) - this.handleClick = this.handleClick.bind(this) - this.handleSubmit = this.handleSubmit.bind(this) - } +import React from 'react' +import {connect} from 'react-redux' +import {Container, Row, Col, Image} from 'react-bootstrap' - componentDidMount() { - this.fetchAPI() - } +const Main = () => { + return ( + + + + + + + ) +} - async fetchAPI() { - try { - const { data } = await axios.get('/api') - const { ascii } = await data - await console.log(ascii) - const episodes = await fetchEpisodes() - await this.setState({ loading: false, episodes, ascii }) - } catch (e) { - console.log(e) - } - } - - async fetchEpisodeComments(slug) { - try { - const { data } = await axios.get(`/api/v2/episodes/${slug}`) - console.log(`FETCH COMMENTS FOR EPISODE ${data.slug}`, data.captions) - const captions = data.captions - this.setState({ loading: false, captions }) - } catch (e) { - console.log(e) - } - } - handleClick(msg) { - const comments = this.state.savedComments.concat([msg]) - this.setState({ savedComments: comments }) - console.log(this.state.savedComments) - } - - handleSubmit(e) { - e.preventDefault() - console.log(this.state.savedComments) - } - - renderEpisodes() { - return ( -
- -
- ) - } - - renderCaptions() { - return ( -
-
- -
-
- - Saved Comments - - - {this.state.savedComments.map(comment => `${comment.text}\n`)} - - - - - -
- ) - } - renderError() { - return
{this.state.error}
- } - - render() { - console.log(this.state) - if (this.state.loading) return
- if (this.state.captions) return this.renderCaptions() - if (this.state.episodes) return this.renderEpisodes() - // if (this.state.error) return this.renderError() - else return
+const mapState = state => { + return { + episodes: state.episodes } } -export default App + +export default connect(mapState)(Main) diff --git a/src/history.js b/src/history.js new file mode 100644 index 0000000..1a47cc8 --- /dev/null +++ b/src/history.js @@ -0,0 +1,7 @@ +const createHistory = require('history').createBrowserHistory +const createMemoryHistory = require('history').createMemoryHistory + +const history = + process.env.NODE_ENV === 'test' ? createMemoryHistory() : createHistory() + +export default history diff --git a/src/index.js b/src/index.js index cee5aa2..29dce88 100644 --- a/src/index.js +++ b/src/index.js @@ -1,16 +1,19 @@ import React from 'react' import ReactDOM from 'react-dom' -import Main from './components' -import Navbar from './components/navbar' -import Footer from './components/footer' +import {Provider} from 'react-redux' +import {Router} from 'react-router-dom' +import history from './history' +import store from './store' +import App from './app' + ReactDOM.render( -
- -
-
-
, + + + + + , document.getElementById('app') ) -module.hot.accept() +//module.hot.accept() diff --git a/src/routes.js b/src/routes.js new file mode 100644 index 0000000..e19a66b --- /dev/null +++ b/src/routes.js @@ -0,0 +1,28 @@ +import React, {Component} from 'react' +import {withRouter, Route} from 'react-router-dom' +import {connect} from 'react-redux' +import {Episodes} from './components/episodes' +import {Captions} from './components/captions' +import Main from './components' +class Routes extends Component { + componentDidMount() { + } + render() { + return ( +
+ + + +
+ ) + } +} + +const mapState = state => { + return { + episodes: state.episodes, + captions: state.captions + } +} + +export default connect(mapState)(withRouter(Routes))