YorKnEz

Untitled

Aug 22nd, 2021
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. import { useEffect, useState } from "react"
  2. import { useDispatch, useSelector } from "react-redux"
  3. import { Route, Switch, useLocation } from "react-router-dom"
  4. import { CookieBanner, Footer, Loading } from "./components"
  5. import {
  6. About,
  7. AddArticle,
  8. Article,
  9. Blog,
  10. Contact,
  11. Home,
  12. PrivacyPolicy,
  13. Service,
  14. Error,
  15. } from "./pages"
  16. import {
  17. fetchProjects,
  18. fetchServices,
  19. getCookie,
  20. fetchText,
  21. fetchBlog,
  22. } from "./redux"
  23. import { servicesPaths } from "./utils"
  24.  
  25. function App() {
  26. const [loading, setLoading] = useState(true)
  27. const dispatch = useDispatch()
  28. const { pathname } = useLocation()
  29. const cookie = useSelector(getCookie)
  30.  
  31. useEffect(() => {
  32. const scrollToTop = () => window.scrollTo(0, 0)
  33. scrollToTop()
  34. }, [pathname])
  35.  
  36. useEffect(() => {
  37. fetchProjects().then(projects =>
  38. dispatch({ type: "SET_PROJECTS", projects })
  39. )
  40.  
  41. fetchServices().then(services =>
  42. dispatch({ type: "SET_SERVICES", services })
  43. )
  44.  
  45. fetchText().then(text => dispatch({ type: "SET_TEXT", text }))
  46.  
  47. fetchBlog().then(blog => dispatch({ type: "SET_BLOG", blog }))
  48.  
  49. // eslint-disable-next-line react-hooks/exhaustive-deps
  50. }, [])
  51.  
  52. useEffect(() => {
  53. const handleLoading = () => {
  54. setLoading(true)
  55. setTimeout(() => setLoading(false), 1300)
  56. }
  57. handleLoading()
  58. }, [pathname])
  59.  
  60. if (loading) return <Loading />
  61.  
  62. return (
  63. <>
  64. {loading && <Loading />}
  65. <Switch>
  66. <Route exact path="/" component={Home} />
  67. <Route path="/contact" component={Contact} />
  68.  
  69. <Route
  70. path="/politica-de-confidentialitate"
  71. component={PrivacyPolicy}
  72. />
  73.  
  74. <Route
  75. path={process.env.REACT_APP_DASHBOARD_PATH}
  76. component={AddArticle}
  77. />
  78.  
  79. <Route exact path="/despre-noi" component={About} />
  80.  
  81. <Route exact path="/blog" component={Blog} />
  82. <Route path="/blog/:articleId" component={Article} />
  83.  
  84. {servicesPaths.map(s => (
  85. <Route key={s} path={s} component={Service} />
  86. ))}
  87.  
  88. <Route component={Error} />
  89. </Switch>
  90. {pathname !== process.env.REACT_APP_DASHBOARD_PATH && <Footer />}
  91. {!cookie.includes("policy") && <CookieBanner />}
  92. </>
  93. )
  94. }
  95.  
  96. export default App
  97.  
Advertisement
Add Comment
Please, Sign In to add comment