Skip to content

Commit 5abb5d9

Browse files
author
bottas
committed
Fixed NavBar behaviour
Using use-react-router Hook to update the navigation bar title whenever location changes.
1 parent 7f5c433 commit 5abb5d9

File tree

4 files changed

+73
-62
lines changed

4 files changed

+73
-62
lines changed

package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"react-router-dom": "^5.0.1",
1919
"react-scripts": "3.0.1",
2020
"sass": "^1.21.0",
21-
"typescript": "3.5.2"
21+
"typescript": "3.5.2",
22+
"use-react-router": "^1.0.7"
2223
},
2324
"scripts": {
2425
"start": "react-scripts start",

src/App.tsx

Lines changed: 57 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import React from 'react';
1+
import React, { useState } from 'react';
22
import { BrowserRouter as Router, Route, Link, Switch } from "react-router-dom";
33
import { Redirect } from 'react-router-dom';
44

5+
import useReactRouter from 'use-react-router';
6+
57
import { SidebarComponent } from './components/Sidebar';
68
import { AppContainer } from './components/AppContainer';
79

@@ -20,40 +22,43 @@ import './styles/AppStyle.scss';
2022
import AddAreaForm from './components/AddAreaForm';
2123
import 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

5863
function Landing() {
5964
return (
@@ -67,38 +72,33 @@ function Landing() {
6772
}
6873

6974
function Areas() {
70-
const [isVisible, setVisible] = React.useState(true);
7175
return (
72-
<AppContainer isVisible={isVisible}><AreaList /></AppContainer>
76+
<AreaList />
7377
);
7478
}
7579

7680
function AreasAdd() {
77-
const [isVisible, setVisible] = React.useState(true);
7881
return (
79-
<AppContainer isVisible={isVisible}><AddAreaForm /></AppContainer>
82+
<AddAreaForm />
8083
);
8184
}
8285

8386
function AreasEdit() {
84-
const [isVisible, setVisible] = React.useState(true);
8587
return (
86-
<AppContainer isVisible={isVisible}><EditAreaForm /></AppContainer>
88+
<EditAreaForm />
8789
);
8890
}
8991

9092
function Sensores() {
91-
const [isVisible, setVisible] = React.useState(true);
9293
return (
93-
<AppContainer isVisible={isVisible}><SensorList /></AppContainer>
94+
<SensorList />
9495
);
9596
}
9697

9798
function 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">&#9776;</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">&#9776;</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
);

src/components/SensorView.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import React, { useEffect, useState } from 'react';
2-
import { Line, defaults } from 'react-chartjs-2';
2+
import { Line } from 'react-chartjs-2';
33

44
import '../styles/SensorView.scss'
55
import { fetchMoisture } from '../deviceAPI';
6-
import { number, bool } from 'prop-types';
76

87
var timeInMs = Date.now(); //(ms) since 1 de janeiro de 1970 00:00:00 UTC
98
const weekInMs = 518400000; //7 days 604800000 gives 8 results, using 6 days;
@@ -12,8 +11,6 @@ const agregation = "day"
1211
const beginDate = timeInMs - weekInMs;
1312
const endDate = timeInMs;
1413

15-
var disabledLegend: number;
16-
1714
function SensorView(props: any) {
1815
type ExpectedResponse = {
1916
S1T: number;
@@ -31,7 +28,6 @@ function SensorView(props: any) {
3128
deviceID = window.location.pathname.substring(15);
3229

3330
const [response, setResponse] = useState<Array<ExpectedResponse> | undefined>();
34-
3531
const [isVisible, setVisible] = useState([true, true, true, true]);
3632
const [disabledLegend, setDisabled] = useState(-1)
3733

0 commit comments

Comments
 (0)