-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathindex.jsx
100 lines (91 loc) · 3.17 KB
/
index.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import React, { useState, useEffect } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import MapLayout from '@/components/Layout/MapLayout';
import MenuIcon from '@/icons/map/menuIcon';
import AirQoMap from '@/components/Map/AirQoMap';
import { useRouter } from 'next/router';
import { AirQualityLegend } from '@/components/Map/components/Legend';
import Sidebar from '@/components/Map/components/Sidebar';
import { getSitesSummary } from '@/lib/store/services/deviceRegistry/GridsSlice';
import withAuth from '@/core/utils/protectedRoute';
const index = () => {
const dispatch = useDispatch();
const router = useRouter();
const [showSideBar, setShowSideBar] = useState(true);
const siteData = useSelector((state) => state.grids?.sitesSummary);
const isAdmin = true;
const preferenceData = useSelector((state) => state.defaults?.individual_preferences);
const [pollutant, setPollutant] = useState('pm2_5');
/**
* Selected sites
*/
const selectedSites = preferenceData
? preferenceData.map((pref) => pref.selected_sites).flat()
: [];
/**
* Site details
*/
const siteDetails = siteData?.sites || [];
/**
* Fetch site details
* @returns {void}
*/
useEffect(() => {
dispatch(getSitesSummary());
}, []);
/**
* Show/hide sidebar on window resize
*/
useEffect(() => {
const handleResize = () => {
if (
(window.innerWidth < 768 || (window.innerWidth >= 600 && window.innerWidth <= 800)) &&
router.pathname === '/map'
) {
setShowSideBar(false);
} else {
setShowSideBar(true);
}
};
window.addEventListener('resize', handleResize);
handleResize();
return () => window.removeEventListener('resize', handleResize);
}, [router.pathname]);
return (
<MapLayout noTopNav={false}>
<div className='relative flex w-full h-full'>
{showSideBar && (
<Sidebar
siteDetails={siteDetails}
selectedSites={selectedSites}
isAdmin={isAdmin}
showSideBar={showSideBar}
setShowSideBar={setShowSideBar}
/>
)}
<div className={`${showSideBar ? 'hidden' : ''} relative left-4 z-50 md:block`}>
<div className={`absolute bottom-2`} style={{ zIndex: 900 }}>
<AirQualityLegend pollutant={pollutant} />
</div>
<div className={`absolute top-4`}>
<div className='flex flex-col space-y-4'>
<button
className={`inline-flex items-center justify-center w-[50px] h-[50px] mr-2 text-white rounded-full bg-white focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 shadow-md lg:hidden`}
onClick={() => setShowSideBar(!showSideBar)}>
<MenuIcon />
</button>
</div>
</div>
</div>
<AirQoMap
showSideBar={showSideBar}
mapboxApiAccessToken={process.env.NEXT_PUBLIC_MAPBOX_ACCESS_TOKEN}
customStyle='flex-grow h-screen w-full relative bg-[#e6e4e0]'
pollutant={pollutant}
resizeMap={showSideBar}
/>
</div>
</MapLayout>
);
};
export default withAuth(index);