Skip to content
This repository has been archived by the owner on Oct 9, 2024. It is now read-only.

Commit

Permalink
Bug/hyp 177 navbar link fix (#85)
Browse files Browse the repository at this point in the history
* Fixes NavBar links by saving context to local storage and then using the AppBar link. Should also make the demonstrator less fragile to refreshes

* 0.3.9

* Linting fixes
  • Loading branch information
mattdean-digicatapult authored Mar 12, 2024
1 parent 05a8c21 commit df9acf1
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 27 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@digicatapult/sqnc-hyproof-client",
"version": "0.3.8",
"version": "0.3.9",
"description": "User interface for HyProof",
"homepage": "https://github.com/digicatapult/sqnc-hyproof-client",
"main": "src/index.js",
Expand Down
2 changes: 2 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export const personas = [

export default function App() {
const {
reset,
update,
current,
currentId,
Expand All @@ -65,6 +66,7 @@ export default function App() {

const resetDemonstrator = (e) => {
e.preventDefault()
reset()
window.location.replace('/')
}

Expand Down
17 changes: 4 additions & 13 deletions src/pages/components/Nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export default function Nav() {
const path = window.location.pathname

const dialogRef = useRef(null)
const inactive = (e) => e.preventDefault()
const showPopup = (e) => {
e.preventDefault()
dialogRef.current?.showModal()
Expand All @@ -33,14 +32,14 @@ export default function Nav() {
accent: '#FFF',
}}
>
<AppBar.Item onClick={inactive}>
<WhiteLink onClick={showPopup}>what we do</WhiteLink>
<AppBar.Item onClick={showPopup} active={false}>
what we do
</AppBar.Item>
<AppBar.Item
onClick={inactive}
href="/certificate"
active={path.startsWith('/certificate')}
>
<WhiteLink to="/certificate">certificates</WhiteLink>
certificates
</AppBar.Item>
</AppBar>
</Grid.Panel>
Expand Down Expand Up @@ -71,11 +70,3 @@ const Home = styled(Grid.Panel)`
padding-left: 20px;
padding-right: 20px;
`

const WhiteLink = styled(Link)`
color: white;
text-decoration: none;
&:hover {
color: #dbe9e8;
}
`
41 changes: 30 additions & 11 deletions src/utils/Context.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
import React, { createContext, useState } from 'react'
import React, { createContext, useEffect, useState } from 'react'

export const Context = createContext({})

const stateKey = 'demo-state'
const defaultState = {
current: 'heidi',
currentId: '',
currentCommitment: '',
currentCommitmentSalt: '',
currentEnergyConsumedWh: 0,
currentProductionStartTime: '',
currentProductionEndTime: '',
emma: {},
heidi: {},
reginald: {},
}
let initState = defaultState
try {
initState = JSON.parse(localStorage.getItem(stateKey)) || initState
} catch (e) {
localStorage.removeItem(stateKey)
}

// this is a provider for initial and state updates
export const ContextProvider = ({ children }) => {
const [state, setState] = useState({
current: 'heidi',
currentId: '',
currentCommitment: '',
currentCommitmentSalt: '',
currentEnergyConsumedWh: 0,
currentProductionStartTime: '',
currentProductionEndTime: '',
emma: {},
heidi: {},
reginald: {},
...initState,
update: (val, key) => {
if (!val) return state
if (!key) return setState({ ...state, ...val })
Expand All @@ -27,7 +38,15 @@ export const ContextProvider = ({ children }) => {
},
})
},
reset: () => {
localStorage.removeItem(stateKey)
},
})

useEffect(() => {
const { update, ...storedState } = state
localStorage.setItem(stateKey, JSON.stringify(storedState))
}, [state])

return <Context.Provider value={state}>{children}</Context.Provider>
}

0 comments on commit df9acf1

Please sign in to comment.