Skip to content

Commit

Permalink
Add extra error checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-Griffiths committed Feb 19, 2018
1 parent 2bc55a7 commit 07a58e8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
17 changes: 11 additions & 6 deletions src/components/App.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
import React, { Component } from 'react'
import { Link, Redirect } from 'react-router-dom'

import Alert from './Alert'
import GameList from './GameList'
import GameLoader from './GameLoader'
import { Redirect } from 'react-router-dom'

import './../assets/css/App.css'

export default class App extends Component {
state = {
filter: '',
games: [],
errorMessage: ''
steamID: '',
steamSecret: '',
errorMessage: '',
}

componentDidMount() {
const id = localStorage.getItem('steamId')
const secret = localStorage.getItem('steamSecret')

if(!id || !secret){
window.location.replace('/settings')
}

fetch(`http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=${secret}&steamid=${id}&format=json&include_appinfo=1&include_played_free_games=1`)
.then(response => response.json())
.then(response => response.response.games)
Expand All @@ -42,10 +40,17 @@ export default class App extends Component {

render() {

// Redirect if the keys are blank
if(!localStorage.getItem('steamId') || !localStorage.getItem('steamSecret')){
return <Redirect to="/settings" />
}

// Looks like there was an error getting the games
if(this.state.errorMessage){
return <Alert>{ this.state.errorMessage }</Alert>
}

// All good yo
return (
<div>
{ !this.state.games.length
Expand Down
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import ReactDOM from 'react-dom'
import { Route, BrowserRouter as Router, Switch, Link } from 'react-router-dom'
import { Route, HashRouter as Router, Switch, Link } from 'react-router-dom'

import App from './components/App'
import Nav from './components/Nav'
Expand Down Expand Up @@ -32,8 +32,9 @@ ReactDOM.render(
</NavItem>
</Nav>
<Switch>
<Route exact path="/" component={App}/>
<Route path="/settings" component={Settings}/>
<Route path="*" component={App} />
<Route path="*" component={NotFound} />
</Switch>
</div>
</Router>
Expand Down

0 comments on commit 07a58e8

Please sign in to comment.