1- import React from 'react' ;
1+ import React , { useState } from 'react' ;
22import { BrowserRouter as Router , Route , Link , Switch } from "react-router-dom" ;
33import { Redirect } from 'react-router-dom' ;
44
5+ import useReactRouter from 'use-react-router' ;
6+
57import { SidebarComponent } from './components/Sidebar' ;
68import { AppContainer } from './components/AppContainer' ;
79
@@ -20,40 +22,43 @@ import './styles/AppStyle.scss';
2022import AddAreaForm from './components/AddAreaForm' ;
2123import EditAreaForm from './components/EditAreaForm' ;
2224
23- function TopNav ( ) {
24- let addPath , currHref = null ;
25- let navTitle ;
26-
27- currHref = window . location . pathname . substring ( 1 ) ;
28-
29- switch ( currHref ) {
30- case "areas" :
31- navTitle = < Link to = "/areas" className = "active" > Áreas</ Link >
32- addPath = < Link to = "/areas/add" id = "add" > Adicionar< img id = "add-img" src = { plus } className = "App-plus" alt = "plus" /> </ Link >
33- break ;
34- case "sensores" :
35- navTitle = < Link to = "/sensores" className = "active" > Sensores</ Link >
36- break ;
37- case "areas-add" :
38- navTitle = < Link to = "/areas" className = "active" > Áreas</ Link >
39- currHref = "" ;
40- break ;
41- default :
25+ const TopNav = ( ) => {
26+ let addPath ;
27+ let object = [ "" , "" ] ;
28+ const { location } = useReactRouter ( ) ;
29+
30+
31+ if ( location . pathname === "/areas" ) {
32+ object = [ "Áreas" , "/areas" ] ;
33+ addPath = < Link to = "/areas/add" id = "add" > Adicionar< img id = "add-img" src = { plus } className = "App-plus" alt = "plus" /> </ Link >
34+ }
35+ else if ( location . pathname === "/sensores" ) {
36+ object = [ "Sensores" , "/sensores" ] ;
37+ addPath = undefined ;
38+ }
39+ else if ( location . pathname === "/areas/add" ) {
40+ object = [ "Criar Área" , "/areas" ] ;
41+ addPath = undefined ;
4242 }
4343
44+ else if ( location . pathname . slice ( 0 , 11 ) === "/areas/edit" ) {
45+ object = [ "Editar Área" , "/areas" ] ;
46+ addPath = undefined ;
47+ }
48+
49+ else if ( location . pathname . slice ( 0 , 14 ) === "/sensores/view" ) {
50+ object = [ location . pathname . slice ( 15 ) , "/sensores" ] ;
51+ addPath = undefined ;
52+ }
53+
4454 return (
4555 < div >
46- { navTitle }
56+ < Link to = { object [ 1 ] } className = "active" > { object [ 0 ] } </ Link >
4757 { addPath }
4858 </ div >
4959 )
5060}
5161
52- const TopbarComponent : React . FunctionComponent = props => (
53- < div className = "topmennav" >
54- { props . children }
55- </ div >
56- ) ;
5762
5863function Landing ( ) {
5964 return (
@@ -67,38 +72,33 @@ function Landing() {
6772}
6873
6974function Areas ( ) {
70- const [ isVisible , setVisible ] = React . useState ( true ) ;
7175 return (
72- < AppContainer isVisible = { isVisible } > < AreaList /> </ AppContainer >
76+ < AreaList />
7377 ) ;
7478}
7579
7680function AreasAdd ( ) {
77- const [ isVisible , setVisible ] = React . useState ( true ) ;
7881 return (
79- < AppContainer isVisible = { isVisible } > < AddAreaForm /> </ AppContainer >
82+ < AddAreaForm />
8083 ) ;
8184}
8285
8386function AreasEdit ( ) {
84- const [ isVisible , setVisible ] = React . useState ( true ) ;
8587 return (
86- < AppContainer isVisible = { isVisible } > < EditAreaForm /> </ AppContainer >
88+ < EditAreaForm />
8789 ) ;
8890}
8991
9092function Sensores ( ) {
91- const [ isVisible , setVisible ] = React . useState ( true ) ;
9293 return (
93- < AppContainer isVisible = { isVisible } > < SensorList /> </ AppContainer >
94+ < SensorList />
9495 ) ;
9596}
9697
9798function SensoresView ( ) {
9899
99- const [ isVisible , setVisible ] = React . useState ( true ) ;
100100 return (
101- < AppContainer isVisible = { isVisible } > < SensorView /> </ AppContainer >
101+ < SensorView />
102102 ) ;
103103}
104104
@@ -113,35 +113,36 @@ function Page404() {
113113 )
114114}
115115
116- const App : React . FC = ( ) => {
116+ const App = ( ) => {
117117
118- const [ isVisible , setVisible ] = React . useState ( true ) ;
118+ const [ isVisible , setVisible ] = useState ( true ) ;
119119
120120 return (
121121 < div className = "App" >
122122 < Router >
123- < TopbarComponent >
124- < div className = "topnav" >
125- < span onClick = { ( ) => setVisible ( ! isVisible ) } id = "menu-span" > ☰</ span >
126- < Link to = "/" id = "brand" > < img src = { brand } className = "App-brand" alt = "brand" /> </ Link >
127- < span > < img src = { spacer } alt = "" /> </ span >
128- < TopNav />
129- </ div >
130- </ TopbarComponent >
123+ < div className = "topnav" >
124+ < span onClick = { ( ) => setVisible ( ! isVisible ) } id = "menu-span" > ☰</ span >
125+ < Link to = "/" id = "brand" > < img src = { brand } className = "App-brand" alt = "brand" /> </ Link >
126+ < span > < img src = { spacer } alt = "" /> </ span >
127+ < TopNav />
128+ </ div >
131129 < SidebarComponent isVisible = { isVisible } >
132130 < Link to = "/areas" > < img src = { area } alt = "area" /> Áreas</ Link >
133131 < Link to = "/sensores" > < img src = { sensor } alt = "sensor" /> Sensores</ Link >
134132 </ SidebarComponent >
135- < Switch >
136- < Route exact path = "/" component = { Landing } />
137- < Route path = "/404" component = { Page404 } />
138- < Route exact path = "/areas" component = { Areas } />
139- < Route path = "/areas/add" component = { AreasAdd } />
140- < Route path = "/areas/edit" component = { AreasEdit } />
141- < Route exact path = "/sensores" component = { Sensores } />
142- < Route path = "/sensores/view" component = { SensoresView } />
143- < Redirect from = "*" to = "/404" />
144- </ Switch >
133+
134+ < AppContainer isVisible = { isVisible } >
135+ < Switch >
136+ < Route exact path = "/" component = { Landing } />
137+ < Route path = "/404" component = { Page404 } />
138+ < Route exact path = "/areas" component = { Areas } />
139+ < Route path = "/areas/add" component = { AreasAdd } />
140+ < Route path = "/areas/edit" component = { AreasEdit } />
141+ < Route exact path = "/sensores" component = { Sensores } />
142+ < Route path = "/sensores/view" component = { SensoresView } />
143+ < Redirect from = "*" to = "/404" />
144+ </ Switch >
145+ </ AppContainer >
145146 </ Router >
146147 </ div >
147148 ) ;
0 commit comments