-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feat/17/login' into 'master'
Fixed #17 started on error handling #23 Closes #17 See merge request it2810-h18/prosjekt3/gruppe42!11
- Loading branch information
Showing
16 changed files
with
216 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} ) ); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters