Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. import React, { Component, Fragment } from 'react'
  2. import {
  3. BrowserRouter as Router,
  4. Route,
  5. Switch,
  6. } from "react-router-dom"
  7. import { CssBaseline, withStyles, Button } from '@material-ui/core'
  8. import ReactLoading from 'react-loading'
  9. import { withSnackbar } from 'notistack'
  10.  
  11. import { Header } from './Layouts'
  12. import RecipeDrawer from './Recipe/RecipeDrawer'
  13. import PrivateRoute from './Router/PrivateRoute'
  14. import Auth from './Auth/Auth'
  15. import Register from './Auth/Register'
  16. import { isAuthenticate, logout } from '../Lib/API/api'
  17.  
  18. import 'typeface-roboto'
  19.  
  20. // import { recipes } from '../Recipes.js'
  21.  
  22. const styles = theme => ({
  23. flex: {
  24. display: 'flex',
  25. },
  26. spinner: {
  27. flexGrow: 0.25,
  28. margin: 'auto',
  29. marginTop: theme.spacing.unit * 20,
  30. color: theme.palette.primary
  31. },
  32. toolbar: theme.mixins.toolbar,
  33. })
  34.  
  35. class App extends Component {
  36. state = {
  37. recipes: null,
  38. isDrawerOpen: !this.isMobileDevice(),
  39. isDrawerLocked: !this.isMobileDevice(),
  40. isAuth: undefined,
  41. }
  42.  
  43. async componentDidMount() {
  44. try {
  45. const { successful } = await isAuthenticate()
  46. this.setState({ isAuth: successful })
  47. } catch(err) {
  48. this.props.enqueueSnackbar(err.message, {
  49. variant: 'error',
  50. persist: true,
  51. action: (
  52. <Button size="small">{'Dismiss'}</Button>
  53. ),
  54. })
  55.  
  56. console.log(err)
  57. }
  58. }
  59.  
  60. logoutUser = async () => {
  61. try {
  62. const { successful } = await logout()
  63. this.setState({ isAuth: !successful })
  64. } catch(err) {
  65. this.props.enqueueSnackbar(err.message, {
  66. variant: 'error',
  67. persist: true,
  68. action: (
  69. <Button size="small">{'Dismiss'}</Button>
  70. ),
  71. })
  72.  
  73. console.log(`App.js logout err: ${err}`)
  74. }
  75. }
  76.  
  77. updateAppStates = (stateObjects) => {
  78. this.setState({ ...stateObjects })
  79. }
  80.  
  81. updateAuthState = (newAuthValue) => {
  82. this.setState({ isAuth: newAuthValue })
  83. }
  84.  
  85. setRecipes = (recipes) => {
  86. this.setState({recipes: recipes})
  87. }
  88.  
  89. isMobileDevice() {
  90. return (typeof window.orientation !== "undefined") || (navigator.userAgent.indexOf('IEMobile') !== -1);
  91. }
  92.  
  93. handleDrawer = () => {
  94. this.setState({ isDrawerOpen: !this.state.isDrawerOpen })
  95. }
  96.  
  97. handleDrawerLock = () => {
  98. this.setState({ isDrawerLocked: !this.state.isDrawerLocked })
  99. }
  100.  
  101. getRecipesBySiteName() {
  102. return Object.entries(this.state.recipes.reduce((recipes, recipe) => {
  103. const { websiteName } = recipe
  104.  
  105. recipes[websiteName] = recipes[websiteName]
  106. ? [...recipes[websiteName], recipe]
  107. : [recipe]
  108.  
  109. return recipes
  110. }, {}))
  111. }
  112.  
  113. hideDrawerButton = () => {
  114. // return window.location.pathname === '/login'
  115. return !this.state.isAuth
  116. }
  117.  
  118. render() {
  119. const { classes } = this.props
  120. const recipesData = this.state.recipes ? this.getRecipesBySiteName() : null
  121. const { isDrawerOpen, isDrawerLocked, isAuth } = this.state
  122.  
  123. let recipeDrawerProps = {
  124. recipesData: recipesData,
  125. isDrawerOpen: isDrawerOpen,
  126. isDrawerLocked: isDrawerLocked,
  127. handleDrawerLock: this.handleDrawerLock,
  128. handleDrawerOpen: this.handleDrawer
  129. }
  130.  
  131. return (
  132. <div className={classes.root}>
  133. <CssBaseline />
  134. <Router >
  135. <Header
  136. isDrawerOpen={isDrawerOpen}
  137. hideDrawerButton={this.hideDrawerButton()}
  138. handleDrawerOpen={this.handleDrawer}
  139. isAuth={isAuth}
  140. logout={this.logoutUser}
  141. />
  142. <div className={classes.flex}>
  143. <Switch>
  144. {/*Recipe rendered in the Drawer*/}
  145. {
  146. isAuth === undefined ? (
  147. <Fragment>
  148. <div className={classes.toolbar} />
  149. <ReactLoading type="bars" color='#3f51b5' className={classes.spinner} />
  150. </Fragment>
  151. ) : (
  152. <Fragment>
  153. <PrivateRoute exact path="/" component={RecipeDrawer} recipeDrawerProps={recipeDrawerProps} isAuth={isAuth} redirectTo="/login" />
  154. <Route exact path="/login" render={ props => <Auth {...props} isAuth={isAuth} updateAuthState={this.updateAuthState} updateAppStates={this.updateAppStates} /> } />
  155. <Route exact path="/register" render={ props => <Register {...props} isAuth={isAuth} updateAuthState={this.updateAuthState} /> } />
  156. </Fragment>
  157. )
  158. }
  159. </Switch>
  160. </div>
  161. </Router>
  162. </div>
  163. )
  164. }
  165. }
  166.  
  167.  
  168. export default withSnackbar(withStyles(styles)(App))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement