Browse Source

Fixed incorrect loading of the info to the not correct time so that the passed information was null (fix: #42)

dependabot/npm_and_yarn/web/prismjs-1.21.0
Max Schmitt 8 years ago
parent
commit
2fbe86adee
  1. 6
      static/src/About/About.js
  2. 43
      static/src/index.js

6
static/src/About/About.js

@ -6,9 +6,11 @@ export default class AboutComponent extends Component {
state = { state = {
info: null info: null
} }
componentWillReceiveProps = () => {
this.setState({ info: this.props.info }) componentDidMount() {
this.setState({ info: this.props.location.state.info })
} }
render() { render() {
const { info } = this.state const { info } = this.state
return ( return (

43
static/src/index.js

@ -1,6 +1,6 @@
import React, { Component } from 'react' import React, { Component } from 'react'
import ReactDOM from 'react-dom'; import ReactDOM from 'react-dom';
import { HashRouter, Route, Link } from 'react-router-dom' import { MemoryRouter, Route, Link } from 'react-router-dom'
import { Menu, Container, Modal, Button, Image, Icon } from 'semantic-ui-react' import { Menu, Container, Modal, Button, Image, Icon } from 'semantic-ui-react'
import toastr from 'toastr' import toastr from 'toastr'
import 'semantic-ui-css/semantic.min.css'; import 'semantic-ui-css/semantic.min.css';
@ -13,29 +13,24 @@ import Lookup from './Lookup/Lookup'
export default class BaseComponent extends Component { export default class BaseComponent extends Component {
state = { state = {
open: true, oAuthOpen: true,
userData: {}, userData: {},
authorized: false, authorized: false,
activeItem: "", activeItem: "",
info: null info: null
} }
onOAuthClose() {
this.setState({ open: true })
}
handleItemClick = (e, { name }) => this.setState({ activeItem: name }) handleItemClick = (e, { name }) => this.setState({ activeItem: name })
componentWillMount() { onOAuthClose() {
this.checkAuth() this.setState({ oAuthOpen: true })
this.loadInfo()
} }
loadInfo = () => { componentDidMount() {
fetch('/api/v1/info') fetch('/api/v1/info')
.then(d => d.json()) .then(d => d.json())
.then(info => this.setState({ info })) .then(info => this.setState({ info }))
.catch(e => toastr.error(e)) .then(() => this.checkAuth())
} }
checkAuth = () => { checkAuth = () => {
@ -53,8 +48,10 @@ export default class BaseComponent extends Component {
}) })
.then(res => res.ok ? res.json() : Promise.reject(`incorrect response status code: ${res.status}; text: ${res.statusText}`)) .then(res => res.ok ? res.json() : Promise.reject(`incorrect response status code: ${res.status}; text: ${res.statusText}`))
.then(d => { .then(d => {
that.setState({ userData: d }) that.setState({
that.setState({ authorized: true }) userData: d,
authorized: true
})
}) })
.catch(e => { .catch(e => {
toastr.error(`Could not fetch info: ${e}`) toastr.error(`Could not fetch info: ${e}`)
@ -67,12 +64,13 @@ export default class BaseComponent extends Component {
onOAuthCallback = data => { onOAuthCallback = data => {
if (data.isTrusted) { if (data.isTrusted) {
// clear the old event listener, so that the event can only emitted be once // clear the old event listener, so that the event can only emitted be once
window.removeEventListener('message', this.onAuthCallback); window.removeEventListener('message', this.onOAuthCallback);
window.localStorage.setItem('token', data.data); window.localStorage.setItem('token', data.data);
this.checkAuth(); this.checkAuth();
this._oAuthPopup = null; this._oAuthPopup = null;
} }
} }
onOAuthClick = provider => { onOAuthClick = provider => {
window.addEventListener('message', this.onOAuthCallback, false); window.addEventListener('message', this.onOAuthCallback, false);
var url = `${window.location.origin}/api/v1/auth/${provider}/login`; var url = `${window.location.origin}/api/v1/auth/${provider}/login`;
@ -94,10 +92,10 @@ export default class BaseComponent extends Component {
} }
render() { render() {
const { open, authorized, activeItem, userData, info } = this.state const { oAuthOpen, authorized, activeItem, userData, info } = this.state
if (!authorized) { if (!authorized) {
return ( return (
<Modal size='tiny' open={open} onClose={this.onOAuthClose}> <Modal size='tiny' open={oAuthOpen} onClose={this.onOAuthClose}>
<Modal.Header> <Modal.Header>
Authentication Authentication
</Modal.Header> </Modal.Header>
@ -128,7 +126,7 @@ export default class BaseComponent extends Component {
) )
} }
return ( return (
<HashRouter> <MemoryRouter>
<Container style={{ paddingTop: "15px" }}> <Container style={{ paddingTop: "15px" }}>
<Menu stackable> <Menu stackable>
<Menu.Item as={Link} to="/" name='shorten' onClick={this.handleItemClick} > <Menu.Item as={Link} to="/" name='shorten' onClick={this.handleItemClick} >
@ -146,7 +144,10 @@ export default class BaseComponent extends Component {
<Menu.Item name='lookup' active={activeItem === 'lookup'} onClick={this.handleItemClick} as={Link} to="/lookup"> <Menu.Item name='lookup' active={activeItem === 'lookup'} onClick={this.handleItemClick} as={Link} to="/lookup">
Lookup Lookup
</Menu.Item> </Menu.Item>
<Menu.Item name='about' active={activeItem === 'about'} onClick={this.handleItemClick} as={Link} to="/about"> <Menu.Item name='about' active={activeItem === 'about'} onClick={this.handleItemClick} as={Link} to={{
pathname: "/about",
state: { info }
}}>
About About
</Menu.Item> </Menu.Item>
<Menu.Menu position='right'> <Menu.Menu position='right'>
@ -154,13 +155,11 @@ export default class BaseComponent extends Component {
</Menu.Menu> </Menu.Menu>
</Menu> </Menu>
<Route exact path="/" component={Home} /> <Route exact path="/" component={Home} />
<Route path="/about" render={(props) => ( <Route path="/about" component={About} />
<About info={info} />
)} />
<Route path="/ShareX" component={ShareX} /> <Route path="/ShareX" component={ShareX} />
<Route path="/Lookup" component={Lookup} /> <Route path="/Lookup" component={Lookup} />
</Container> </Container>
</HashRouter> </MemoryRouter>
) )
} }
} }

Loading…
Cancel
Save