-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
94 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,112 +1,110 @@ | ||
import About from 'components/Pages/About'; | ||
import Contact from 'components/Pages/Contact'; | ||
import Join from 'components/Pages/Join'; | ||
import React, { createContext, useEffect, useState } from 'react'; | ||
import { createContext, useCallback, useEffect, useState } from 'react'; | ||
import { useSelector } from 'react-redux'; | ||
|
||
const HeaderContext = createContext({}); | ||
|
||
// Create a HeaderProvider component | ||
const HeaderProvider = ({ children }) => { | ||
// Define the state for the header context | ||
const [anchorEl, setAnchorEl] = useState(null); | ||
const [sidebarOpen, setSidebarOpen] = useState(false); | ||
const [showMapControls, setShowMapControls] = useState(false); | ||
const [menuExpand, setMenuExpand] = useState(false); | ||
const [pageExpand, setPageExpand] = useState(false); | ||
const [verticalAnimFinished1, setVerticalAnimFinished1] = useState(false); | ||
const [verticalAnimFinished2, setVerticalAnimFinished2] = useState(false); | ||
const [shownPage, setShownPage] = useState(null); | ||
const isSearchShown = useSelector(state => state.filterMarkers.isSearchShown); | ||
const isFilterShown = useSelector(state => state.filterMarkers.isFilterShown); | ||
// Define the state for the header context | ||
const [anchorEl, setAnchorEl] = useState(null); | ||
const [sidebarOpen, setSidebarOpen] = useState(false); | ||
const [showMapControls, setShowMapControls] = useState(false); | ||
const [menuExpand, setMenuExpand] = useState(false); | ||
const [pageExpand, setPageExpand] = useState(false); | ||
const [verticalAnimFinished1, setVerticalAnimFinished1] = useState(false); | ||
const [verticalAnimFinished2, setVerticalAnimFinished2] = useState(false); | ||
const [shownPage, setShownPage] = useState(null); | ||
const isSearchShown = useSelector(state => state.filterMarkers.isSearchShown); | ||
const isFilterShown = useSelector(state => state.filterMarkers.isFilterShown); | ||
|
||
// Define any functions or values you want to expose to consumers of the context | ||
const open = Boolean(anchorEl); | ||
const toggleMenuExpand = event => { | ||
if (menuExpand) { | ||
setVerticalAnimFinished1(false); | ||
setVerticalAnimFinished2(false); | ||
setPageExpand(false); | ||
setShownPage(null); | ||
} | ||
setMenuExpand(!menuExpand); | ||
}; | ||
// Define any functions or values you want to expose to consumers of the context | ||
const open = Boolean(anchorEl); | ||
const toggleMenuExpand = event => { | ||
if (menuExpand) { | ||
setVerticalAnimFinished1(false); | ||
setVerticalAnimFinished2(false); | ||
setPageExpand(false); | ||
setShownPage(null); | ||
} | ||
setMenuExpand(!menuExpand); | ||
}; | ||
|
||
const showSidebar = () => { | ||
setSidebarOpen(true); | ||
}; | ||
const showSidebar = () => { | ||
setSidebarOpen(true); | ||
}; | ||
|
||
const menuClicked = page => { | ||
if (page == shownPage) { | ||
setVerticalAnimFinished1(false); | ||
setVerticalAnimFinished2(false); | ||
setPageExpand(false); | ||
setShownPage(null); | ||
} else { | ||
setPageExpand(true); | ||
switch (page) { | ||
case 'about': | ||
page = <About />; | ||
break; | ||
case 'join': | ||
page = <Join />; | ||
break; | ||
case 'contact': | ||
page = <Contact />; | ||
break; | ||
default: | ||
break; | ||
} | ||
setShownPage(page); | ||
} | ||
}; | ||
const menuClicked = page => { | ||
if (page == shownPage) { | ||
setVerticalAnimFinished1(false); | ||
setVerticalAnimFinished2(false); | ||
setPageExpand(false); | ||
setShownPage(null); | ||
} else { | ||
setPageExpand(true); | ||
switch (page) { | ||
case 'about': | ||
page = <About />; | ||
break; | ||
case 'join': | ||
page = <Join />; | ||
break; | ||
case 'contact': | ||
page = <Contact />; | ||
break; | ||
default: | ||
break; | ||
} | ||
setShownPage(page); | ||
} | ||
}; | ||
|
||
const isNotMapPage = useCallback(() => { | ||
const pagePaths = /(\/mission)|(\/share)|(\/project)|(\/contribute)/; | ||
const isNotMapPage = () => { | ||
return window.location.pathname.match(pagePaths); | ||
}; | ||
return window.location.pathname.match(pagePaths); | ||
}, []); | ||
|
||
//On render, check if on map page to show or hide map controls | ||
useEffect(() => { | ||
if (isNotMapPage()) { | ||
setShowMapControls(false); | ||
} else { | ||
setShowMapControls(true); | ||
} | ||
}, [isNotMapPage, setShowMapControls]); | ||
//On render, check if on map page to show or hide map controls | ||
useEffect(() => { | ||
if (isNotMapPage()) { | ||
setShowMapControls(false); | ||
} else { | ||
setShowMapControls(true); | ||
} | ||
}, [isNotMapPage, setShowMapControls]); | ||
|
||
const stateVal = { | ||
anchorEl, | ||
setAnchorEl, | ||
sidebarOpen, | ||
setSidebarOpen, | ||
showMapControls, | ||
setShowMapControls, | ||
menuExpand, | ||
setMenuExpand, | ||
pageExpand, | ||
setPageExpand, | ||
verticalAnimFinished1, | ||
setVerticalAnimFinished1, | ||
verticalAnimFinished2, | ||
setVerticalAnimFinished2, | ||
shownPage, | ||
setShownPage, | ||
isSearchShown, | ||
isFilterShown, | ||
open, | ||
toggleMenuExpand, | ||
showSidebar, | ||
menuClicked, | ||
isNotMapPage, | ||
}; | ||
const stateVal = { | ||
anchorEl, | ||
setAnchorEl, | ||
sidebarOpen, | ||
setSidebarOpen, | ||
showMapControls, | ||
setShowMapControls, | ||
menuExpand, | ||
setMenuExpand, | ||
pageExpand, | ||
setPageExpand, | ||
verticalAnimFinished1, | ||
setVerticalAnimFinished1, | ||
verticalAnimFinished2, | ||
setVerticalAnimFinished2, | ||
shownPage, | ||
setShownPage, | ||
isSearchShown, | ||
isFilterShown, | ||
open, | ||
toggleMenuExpand, | ||
showSidebar, | ||
menuClicked, | ||
isNotMapPage | ||
}; | ||
|
||
// Return the HeaderContext.Provider with the headerState and any other values/functions | ||
return ( | ||
<HeaderContext.Provider value={stateVal} > | ||
{children} | ||
</HeaderContext.Provider> | ||
); | ||
// Return the HeaderContext.Provider with the headerState and any other values/functions | ||
return ( | ||
<HeaderContext.Provider value={stateVal}>{children}</HeaderContext.Provider> | ||
); | ||
}; | ||
|
||
export { HeaderContext, HeaderProvider }; | ||
export { HeaderContext, HeaderProvider }; |