Skip to content

Commit

Permalink
Fixed #17 started on error handling #23
Browse files Browse the repository at this point in the history
  • Loading branch information
larsmhar committed Oct 31, 2018
1 parent 4272439 commit 29726da
Show file tree
Hide file tree
Showing 16 changed files with 216 additions and 29 deletions.
1 change: 1 addition & 0 deletions backend/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ const getFilms = function( args ) {
};

const getUser = function( args ) {
console.log( 'Getting user', args );
return new Promise( ( resolve, reject ) => {
db.get( 'SELECT * FROM user WHERE username = $username', args.username ).then( function( result ) {
if ( result ) {
Expand Down
Binary file modified database.db
Binary file not shown.
31 changes: 27 additions & 4 deletions frontend/src/App.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,50 @@
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import { connect } from 'react-redux';

import Filmspage from './screens/Filmspage';
import Frontpage from './screens/Frontpage';
import Film from './screens/Film';
import { BrowserRouter as Router, Route, Link } from 'react-router-dom';
import { BrowserRouter as Router, Route, Link, Redirect } from 'react-router-dom';


class App extends Component {

checkLogin( component, destination ) {
return this.props.user.user.data.user ? <Redirect to={ destination }/> : component;
}
render() {
//localStorage.setItem("thing", "datum")
console.log(localStorage.getItem("thing"))
return (
<Router>
<div className="App">
<header className="App-header">
<div> <Link to='/'> Filmlr</Link></div>
</header>
{/* Had to set margin-top here, because setting in css didn't work?*/}
<Route exact path="/" component={Frontpage} />
<Route path="/film/:id" component={Film} />
{/*
<Route exact path="/" render={() =>
!!this.props.user.user.data ?
(<Redirect to='films'/>)
:
<Login/>
} />
*/}
{console.log( this.props.user.user.data.user )}
<Route exact path='/' render={() => this.checkLogin( <Frontpage/>, 'films' ) } />
<Route exact path="/films" component={Filmspage}/>
<Route path="/films/:id" component={Film} />
</div>
</Router>
);
}
}

export default App;
const mapStateToProps = state => ( {
'user': state.user
} );

export default connect( mapStateToProps )( App );

9 changes: 5 additions & 4 deletions frontend/src/actions/filmActions.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { FETCH_FILMS, FETCH_FILM, LOADING, LOADED, UPDATE_LIKED, UPDATE_WATCHED} from './types';
import { FETCH_FILMS, FETCH_FILM, LOADING, LOADED, UPDATE_LIKED, UPDATE_WATCHED } from './types';

export const fetchFilms = () => dispatch => {
//console.log( 'fetching all films' );
export const fetchFilms = (uid) => dispatch => {
console.log( 'fetching all films' );
console.log("uid", uid)
dispatch( { 'type': LOADING} );
fetch( 'http://localhost:4000/graphql', {
'method': 'POST',
'headers': { 'Content-Type': 'application/json' },
'body': JSON.stringify( { 'query': '{ films { id title poster } userWatched(uid:1) {id watched} userLiked(uid:1) {id liked} }' } ),
'body': JSON.stringify( { 'query': '{ films { id title poster } userWatched(uid: ' + uid + ') {id watched} userLiked(uid: ' + uid + ') {id liked} }' } ),
} )
.then( res => {
return res.json();
Expand Down
1 change: 1 addition & 0 deletions frontend/src/actions/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export const LOADING = 'LOADING';
export const LOADED = 'LOADED';
export const UPDATE_LIKED = 'UPDATE_LIKED';
export const UPDATE_WATCHED = 'UPDATE_WATCHED';
export const GET_USER = 'GET_USER';
19 changes: 19 additions & 0 deletions frontend/src/actions/userActions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { LOADING, GET_USER } from './types';

export const getUser = ( username ) => dispatch => {
//console.log( JSON.stringify( { 'query': '{ films (id: ' + '"' + id + '"' + ') { id title poster } }' } ) );
//console.log("Typeof", typeof id)
//console.log("fetching single film with id", id)
console.log( 'Gotting users' );
dispatch( { 'type': LOADING} );
fetch( 'http://localhost:4000/graphql', {
'method': 'POST',
'headers': { 'Content-Type': 'application/json' },
'body': JSON.stringify( { 'query': '{ user (username: "' + username + '") { uid username } }' } ),
} )
.then( res => res.json() )
.then( user => dispatch( {
'type': GET_USER,
'payload': user
} ) );
};
20 changes: 20 additions & 0 deletions frontend/src/components/Errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from 'react';
import { Component } from 'react';
import { connect } from 'react-redux';

class Errors extends Component {
render() {
return (
<div>
{this.props.user.user.errors.map( error =>
<div>{error.message}</div> )}
</div>
);
}
}

const mapStateToProps = state => ( {
'user': state.user
} );

export default connect( mapStateToProps )( Errors );
9 changes: 5 additions & 4 deletions frontend/src/components/Items.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { BrowserRouter as Router, Route, Link } from 'react-router-dom';
class Items extends Component {

componentWillMount() {
this.props.fetchFilms();
this.props.fetchFilms( this.props.user.user.data.user.uid );
}

generateClass( id ) {
Expand All @@ -19,9 +19,9 @@ class Items extends Component {

render() {
const films = this.props.films.data.films.map( film =>
<Link to={'/film/' + film.id}>
<Link to={'/films/' + film.id}>
<div id={film.id} className={this.generateClass( film.id )} >
<div class="hiddenTitle">{film.title}</div>
<div className="hiddenTitle">{film.title}</div>
<img src={film.poster} alt="poster"/>
</div>
</Link> );
Expand All @@ -39,7 +39,8 @@ Items.propTypes = {
};

const mapStateToProps = state => ( {
'films': state.films.items
'films': state.films.items,
'user': state.user,
} );

export default connect( mapStateToProps, { fetchFilms } )( Items );
15 changes: 15 additions & 0 deletions frontend/src/components/Login.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.userField {
background-color: #485563;
color: #f5f5f5;
border: 0.09em solid hsl(150, 8%, 55%);
margin-right: 0.5em;
width: 20vw;
padding: 0.5em;
}

.loginBtn {
padding: 0.5em;
border: 0.09em solid #e4cc0c;
color: #f5f5f5;
background-color: #102027;
}
49 changes: 49 additions & 0 deletions frontend/src/components/Login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import React from 'react';
import { Component } from 'react';
import { connect } from 'react-redux';

import { getUser } from '../actions/userActions';
import Errors from './Errors';

import './Login.css';

class Login extends Component {

constructor( props ) {
super( props );
this.state = {
'username': '',
};
this.onHandleSubmit = this.onHandleSubmit.bind( this );
this.onUsernameChange = this.onUsernameChange.bind( this );
}

onHandleSubmit( e ) {
e.preventDefault();
this.props.getUser( this.state.username );
}

onUsernameChange( e ) {
this.setState( { 'username': e.target.value } );
}

render() {
let error;
if ( 'errors' in this.props.user.user ) {
error = ( <div><Errors errors={this.props.user.user.errors} /><p>PPP</p> </div> );
}
return (
<form onSubmit={ this.onHandleSubmit }>
<input className="userField" type="text" value={ this.state.username } onChange={ this.onUsernameChange }></input>
<input className="loginBtn" type="submit"></input>
{error}
</form>
);
}
}

const mapStateToProps = state => ( {
'user': state.user
} );

export default connect( mapStateToProps, { getUser } )( Login );
4 changes: 3 additions & 1 deletion frontend/src/reducers/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { combineReducers } from 'redux';
import filmReducer from './filmReducer';
import userReducer from './userReducer';

export default combineReducers( {
'films': filmReducer
'films': filmReducer,
'user': userReducer,
} );
28 changes: 28 additions & 0 deletions frontend/src/reducers/userReducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { LOADING, LOADED, GET_USER } from '../actions/types';

const initialState = {
'items': [],
};

export default function( state = initialState, action ) {
switch ( action.type ) {
case GET_USER:
return {
...state,
'user': action.payload,
'loaded': true,
};
case LOADING:
return {
...state,
'loaded': false,
};
case LOADED:
return {
...state,
'loaded': true,
};
default:
return state;
}
}
31 changes: 18 additions & 13 deletions frontend/src/screens/Film.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class Film extends Component {

componentWillMount() {
//console.log( this.props.match.params.id );
this.props.fetchFilms();
this.props.fetchFilm( this.props.match.params.id, 1 );
this.props.fetchFilms( this.props.user.user.data.user.uid );
this.props.fetchFilm( this.props.match.params.id, this.props.user.user.data.user.uid );
}

constructor( props ) {
Expand All @@ -23,11 +23,12 @@ class Film extends Component {
}

handleClick( e ) {
console.log( this.props.user.user.data )
if ( e.target.id === 'liked' ) {
this.props.updateLiked( this.props.match.params.id, 1 );
this.setState( {'liked': !this.state.liked} );
this.props.updateLiked( this.props.match.params.id, this.props.user.user.data.user.uid );
//this.setState( {'liked': !this.state.liked} );
} else if ( e.target.id === 'watched' ) {
this.props.updateWatched( this.props.match.params.id, 1 );
this.props.updateWatched( this.props.match.params.id, this.props.user.user.data.user.uid );
this.setState( {'watched':!this.state.watched} );

}
Expand All @@ -42,12 +43,16 @@ class Film extends Component {
//console.log( this.props.film.data.films );
console.log( this.props.film );
let film;
if ( this.props.loaded) {
console.log(this.props)
if ( this.props.loaded ) {
console.log( this.props );

console.log( this.state.liked, !!this.props.film[0]['liked'] )
if ( this.state.liked != !!this.props.film[0]['liked'] ) this.setState( { 'liked': !!this.props.film[0]['liked'] } )
if ( this.state.watched != !!this.props.film[0]['watched'] ) this.setState( { 'watched': !!this.props.film[0]['watched'] } )
console.log( this.state.liked, !!this.props.film[0]['liked'] );
if ( this.state.liked != !!this.props.film[0]['liked'] ) {
this.setState( { 'liked': !!this.props.film[0]['liked'] } );
}
if ( this.state.watched != !!this.props.film[0]['watched'] ) {
this.setState( { 'watched': !!this.props.film[0]['watched'] } );
}
/*
this.props.userLiked.forEach( x => {
if ( x['id'] == this.props.film[0]['id'] & this.state.favorite != true ) this.setState( { 'favorite': true }, console.log( 'Liked' ) );
Expand All @@ -71,9 +76,8 @@ class Film extends Component {
<i id="watched" onClick={this.handleClick} style={{'color':this.state.watched ? '#8bc34a' : '#bebebe'}} className="material-icons md-48">visibility</i>
</div>
</div> );
}
else {
film = <div className=''>loading</div>;
} else {
film = <div></div>;
}
return (
[film]
Expand All @@ -94,6 +98,7 @@ const mapStateToProps = state => ( {
'userLiked': state.films.items.data.userLiked,
'userWatched': state.films.items.data.userWatched,
'loaded': state.films.loaded,
'user': state.user,
} );

export default connect( mapStateToProps, { fetchFilm, fetchFilms, updateLiked, updateWatched } )( Film );
15 changes: 15 additions & 0 deletions frontend/src/screens/Filmspage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React, { Component } from 'react';

import Items from '../components/Items';

class Filmspage extends Component {
render() {
return (
<div className="App-container" style={{'marginTop':'1em'}}>
<Items/>
</div>
);
}
}

export default Filmspage;
4 changes: 2 additions & 2 deletions frontend/src/screens/Frontpage.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React, { Component } from 'react';

import Items from '../components/Items';
import Login from '../components/Login';

class Frontpage extends Component {
render() {
return (
<div className="App-container" style={{'marginTop':'1em'}}>
<Items/>
<Login/>
</div>
);
}
Expand Down
9 changes: 8 additions & 1 deletion frontend/src/store/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ const initialState = {
}
},
'loaded': false
},
'user': {
'user': {
'data': {
'user': null
},
}
}
};

Expand All @@ -41,7 +48,7 @@ const store = createStore(
rootReducer,
initialState,
compose(
applyMiddleware(...middleware),
applyMiddleware( ...middleware ),
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
)
);
Expand Down

0 comments on commit 29726da

Please sign in to comment.