Add react new error handeling with componentDidCatch

This commit is contained in:
Qolzam
2017-10-10 17:09:43 +07:00
parent 0c9a0bfd38
commit c82826bdd4

View File

@@ -7,21 +7,18 @@ import { push } from 'react-router-redux'
import Snackbar from 'material-ui/Snackbar' import Snackbar from 'material-ui/Snackbar'
import LinearProgress from 'material-ui/LinearProgress' import LinearProgress from 'material-ui/LinearProgress'
// - Import components // - Import components
import Home from 'components/Home' import Home from 'components/Home'
import Signup from 'components/Signup' import Signup from 'components/Signup'
import Login from 'components/Login' import Login from 'components/Login'
import Settings from 'components/Settings' import Settings from 'components/Settings'
import MasterLoading from 'components/MasterLoading' import MasterLoading from 'components/MasterLoading'
import { IMasterProps } from "./IMasterProps" import { IMasterProps } from './IMasterProps'
import { IMasterState } from "./IMasterState" import { IMasterState } from './IMasterState'
// - Import API // - Import API
import { PrivateRoute, PublicRoute } from 'api/AuthRouterAPI' import { PrivateRoute, PublicRoute } from 'api/AuthRouterAPI'
// - Import actions // - Import actions
import * as authorizeActions from 'actions/authorizeActions' import * as authorizeActions from 'actions/authorizeActions'
import * as imageGalleryActions from 'actions/imageGalleryActions' import * as imageGalleryActions from 'actions/imageGalleryActions'
@@ -33,18 +30,14 @@ import * as globalActions from 'actions/globalActions'
import * as circleActions from 'actions/circleActions' import * as circleActions from 'actions/circleActions'
import * as notifyActions from 'actions/notifyActions' import * as notifyActions from 'actions/notifyActions'
/* ------------------------------------ */ /* ------------------------------------ */
// - Create Master component class // - Create Master component class
export class Master extends Component<IMasterProps,IMasterState>{ export class Master extends Component<IMasterProps,IMasterState> {
static isPrivate = true static isPrivate = true
// Constructor // Constructor
constructor(props : IMasterProps) { constructor (props: IMasterProps) {
super(props) super(props)
this.state = { this.state = {
loading: true, loading: true,
@@ -56,7 +49,6 @@ export class Master extends Component<IMasterProps,IMasterState>{
this.handleLoading = this.handleLoading.bind(this) this.handleLoading = this.handleLoading.bind(this)
this.handleMessage = this.handleMessage.bind(this) this.handleMessage = this.handleMessage.bind(this)
} }
// Handle click on message // Handle click on message
@@ -72,16 +64,21 @@ export class Master extends Component<IMasterProps,IMasterState>{
}) })
} }
componentWillMount (){ componentDidCatch (error: any, info: any) {
console.log('====================================')
console.log(error, info)
console.log('====================================')
}
firebaseAuth().onAuthStateChanged((user:any) => { componentWillMount () {
firebaseAuth().onAuthStateChanged((user: any) => {
if (user) { if (user) {
this.props.login(user) this.props.login(user)
this.setState({ this.setState({
loading: false loading: false
}) })
if (!this.props.global.defaultLoadDataStatus) { if (!this.props.global.defaultLoadDataStatus) {
this.props.clearData() this.props.clearData()
this.props.loadData() this.props.loadData()
@@ -102,44 +99,42 @@ export class Master extends Component<IMasterProps,IMasterState>{
} }
/** /**
* Render app DOM component * Render app DOM component
* *
* @returns * @returns
* *
* @memberof Master * @memberof Master
*/ */
public render() : React.ReactElement<{}> { public render (): React.ReactElement<{}> {
const {progress, global} = this.props const {progress, global} = this.props
return ( return (
<div id="master"> <div id='master'>
<div className='master__progress' style={{display: (progress.visible ? 'block' : 'none' )}}> <div className='master__progress' style={{display: (progress.visible ? 'block' : 'none' )}}>
<LinearProgress mode="determinate" value={progress.percent} /> <LinearProgress mode='determinate' value={progress.percent} />
</div> </div>
<div className='master__loading animate-fading2' style={{display: ( global.showTopLoading ? 'flex' : 'none' )}}> <div className='master__loading animate-fading2' style={{display: ( global.showTopLoading ? 'flex' : 'none' )}}>
<div className='title'> Loading ... </div> <div className='title'>Loading ... </div>
</div> </div>
<MasterLoading activeLoading={this.state.loading || !(this.props.loaded || this.props.guest)} handleLoading={this.handleLoading} /> <MasterLoading activeLoading={this.state.loading || !(this.props.loaded || this.props.guest)} handleLoading={this.handleLoading} />
{(!this.state.loading && (this.props.loaded || this.props.guest)) {(!this.state.loading && (this.props.loaded || this.props.guest))
?(<Switch> ? (<Switch>
<Route path="/signup" component={Signup} /> <Route path='/signup' component={Signup} />
<Route path="/settings" component={Settings} /> <Route path='/settings' component={Settings} />
<Route path="/login" render={() => { <Route path='/login' render={() => {
console.log('this.props.authed: ', this.props.authed, "this.props: ", this.props) console.log('this.props.authed: ', this.props.authed, 'this.props: ', this.props)
return ( return (
this.props.authed this.props.authed
? <Redirect to="/" /> ? <Redirect to='/' />
: <Login /> : <Login />
) )
} }
} /> } />
<Route render={() =><Home uid={this.props.uid}/>} /> <Route render={() => <Home uid={this.props.uid}/>} />
</Switch>) : '' </Switch>) : ''
} }
@@ -157,7 +152,7 @@ export class Master extends Component<IMasterProps,IMasterState>{
} }
// - Map dispatch to props // - Map dispatch to props
const mapDispatchToProps = (dispatch : any, ownProps : any) => { const mapDispatchToProps = (dispatch: any, ownProps: any) => {
return { return {
loadData: () => { loadData: () => {
@@ -178,9 +173,9 @@ const mapDispatchToProps = (dispatch : any, ownProps : any) => {
dispatch(voteActions.clearAllvotes()) dispatch(voteActions.clearAllvotes())
dispatch(notifyActions.clearAllNotifications()) dispatch(notifyActions.clearAllNotifications())
dispatch(circleActions.clearAllCircles()) dispatch(circleActions.clearAllCircles())
}, },
login: (user : any) => { login: (user: any) => {
dispatch(authorizeActions.login(user.uid)) dispatch(authorizeActions.login(user.uid))
}, },
logout: () => { logout: () => {
@@ -206,8 +201,8 @@ const mapDispatchToProps = (dispatch : any, ownProps : any) => {
* Map state to props * Map state to props
* @param {object} state * @param {object} state
*/ */
const mapStateToProps = (state : any) => { const mapStateToProps = (state: any) => {
const {authorize, global, user, post, comment, imageGallery , vote, notify,circle } = state const {authorize, global, user, post, comment, imageGallery , vote, notify,circle } = state
return { return {
guest: authorize.guest, guest: authorize.guest,
uid: authorize.uid, uid: authorize.uid,