Handle not having hetwork better
This commit is contained in:
parent
163472ad2a
commit
d17f252fd2
|
@ -7,15 +7,21 @@ import { Link, Route } from 'react-router-dom';
|
||||||
import io from 'socket.io-client';
|
import io from 'socket.io-client';
|
||||||
|
|
||||||
// Move to env var
|
// Move to env var
|
||||||
const SOCKET_SERVER_URL = 'http://localhost:8080';
|
const SOCKET_SERVER_URL = 'https://tools-socket.protospace.ca';
|
||||||
const AUTH_SERVER_URL = 'http://localhost:8000';
|
const AUTH_SERVER_URL = 'https://tools-auth.protospace.ca';
|
||||||
|
|
||||||
class App extends Component {
|
class App extends Component {
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
this.socket = io(SOCKET_SERVER_URL);
|
this.socket = io(SOCKET_SERVER_URL);
|
||||||
this.storage = typeof localStorage !== 'undefined';
|
|
||||||
|
try {
|
||||||
|
localStorage.setItem('test', 'test');
|
||||||
|
this.storage = true;
|
||||||
|
} catch (e) {
|
||||||
|
this.storage = false;
|
||||||
|
}
|
||||||
|
|
||||||
let token = this.storage ? localStorage.getItem('token') : null;
|
let token = this.storage ? localStorage.getItem('token') : null;
|
||||||
|
|
||||||
|
@ -29,6 +35,11 @@ class App extends Component {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
noNetwork = () => {
|
||||||
|
this.setState({ network: false });
|
||||||
|
this.socket.disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
getUser = () => {
|
getUser = () => {
|
||||||
fetch(AUTH_SERVER_URL + '/user/', {
|
fetch(AUTH_SERVER_URL + '/user/', {
|
||||||
headers: {'Authorization': 'Token ' + this.state.login.token},
|
headers: {'Authorization': 'Token ' + this.state.login.token},
|
||||||
|
@ -47,11 +58,16 @@ class App extends Component {
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
fetch(AUTH_SERVER_URL + '/tooldata/')
|
fetch(AUTH_SERVER_URL + '/tooldata/')
|
||||||
.then(response => response.json())
|
.then(response => {
|
||||||
.then(data => this.setState({ toolData: data }))
|
if (response.ok) {
|
||||||
|
response.json().then(data => this.setState({ toolData: data }));
|
||||||
|
} else {
|
||||||
|
this.noNetwork();
|
||||||
|
}
|
||||||
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
console.log(error)
|
console.log(error)
|
||||||
this.setState({network: false});
|
this.noNetwork();
|
||||||
});
|
});
|
||||||
|
|
||||||
if (this.state.login.token) this.getUser();
|
if (this.state.login.token) this.getUser();
|
||||||
|
@ -85,7 +101,7 @@ class App extends Component {
|
||||||
const value = event.target.value;
|
const value = event.target.value;
|
||||||
|
|
||||||
this.setState({
|
this.setState({
|
||||||
login: {...this.state.login, [name]: value}
|
login: {...this.state.login, [name]: value, error: false}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -99,17 +115,17 @@ class App extends Component {
|
||||||
.then(response => {
|
.then(response => {
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
response.json().then(data => {
|
response.json().then(data => {
|
||||||
this.setState({login: {...login, ...data, error: false}});
|
this.setState({ login: {...login, ...data, error: false} });
|
||||||
if (this.storage) localStorage.setItem('token', data.token);
|
if (this.storage) localStorage.setItem('token', data.token);
|
||||||
this.getUser();
|
this.getUser();
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
this.setState({login: {...login, error: true}});
|
this.setState({ login: {...login, error: true} });
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
console.log(error)
|
console.log(error)
|
||||||
this.setState({network: false});
|
this.setState({ network: false });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -200,11 +216,7 @@ class App extends Component {
|
||||||
:
|
:
|
||||||
<Container text>
|
<Container text>
|
||||||
<Form size='large' onSubmit={this.handleSubmit}>
|
<Form size='large' onSubmit={this.handleSubmit}>
|
||||||
{login.error ?
|
|
||||||
<Message visible error header='Invalid username / password!' />
|
|
||||||
:
|
|
||||||
<Message visible warning header='Please log in to access the tools!' />
|
<Message visible warning header='Please log in to access the tools!' />
|
||||||
}
|
|
||||||
<Form.Field>
|
<Form.Field>
|
||||||
<label>Protospace Username</label>
|
<label>Protospace Username</label>
|
||||||
<input name='username' placeholder='Username' value={login.username} onChange={this.handleChange} />
|
<input name='username' placeholder='Username' value={login.username} onChange={this.handleChange} />
|
||||||
|
@ -214,6 +226,9 @@ class App extends Component {
|
||||||
<input name='password' type='password' value={login.password} onChange={this.handleChange} />
|
<input name='password' type='password' value={login.password} onChange={this.handleChange} />
|
||||||
</Form.Field>
|
</Form.Field>
|
||||||
<Button type='submit'>Submit</Button>
|
<Button type='submit'>Submit</Button>
|
||||||
|
{login.error && <Label basic color='red' pointing='left'>
|
||||||
|
Invalid username / password!
|
||||||
|
</Label>}
|
||||||
</Form>
|
</Form>
|
||||||
</Container>
|
</Container>
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ import React from 'react';
|
||||||
import ReactDOM from 'react-dom';
|
import ReactDOM from 'react-dom';
|
||||||
import './index.css';
|
import './index.css';
|
||||||
import App from './App';
|
import App from './App';
|
||||||
import registerServiceWorker from './registerServiceWorker';
|
|
||||||
import { BrowserRouter } from 'react-router-dom';
|
import { BrowserRouter } from 'react-router-dom';
|
||||||
|
|
||||||
ReactDOM.render((
|
ReactDOM.render((
|
||||||
|
@ -10,5 +9,3 @@ ReactDOM.render((
|
||||||
<App />
|
<App />
|
||||||
</BrowserRouter>
|
</BrowserRouter>
|
||||||
), document.getElementById('root'));
|
), document.getElementById('root'));
|
||||||
|
|
||||||
registerServiceWorker();
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user