Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed restore_votes not update MyVotes #2991, #2986 #3868

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/ui/src/app/pages/Election/Election.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import { useHistory } from 'react-router-dom'
import { PageHeaderRow, PageHeaderWrapper, PageLayout } from '@/app/components/PageLayout'
import { ButtonsGroup, CopyButtonTemplate } from '@/common/components/buttons'
import { LinkIcon } from '@/common/components/icons'
import { LinkSymbol } from '@/common/components/icons/symbols'
import { Loading } from '@/common/components/Loading'
import { MainPanel } from '@/common/components/page/PageContent'
import { PageTitle } from '@/common/components/page/PageTitle'
import { BlockDurationStatistics, StatisticItem, Statistics } from '@/common/components/statistics'
import { TextHuge, TextMedium } from '@/common/components/typography'
import { TextHuge } from '@/common/components/typography'
import { camelCaseToText } from '@/common/helpers'
import { useRefetchQueries } from '@/common/hooks/useRefetchQueries'
import { MILLISECONDS_PER_BLOCK } from '@/common/model/formatters'
Expand Down
12 changes: 12 additions & 0 deletions packages/ui/src/common/hooks/useLocalStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,23 @@ export const useLocalStorage = <T>(key?: string) => {
setState(getItem(key))
}, [key])

useEffect(() => {
const handleEventOnce = (event: any) => {
setState(getItem(key))
}

document.addEventListener(`storage_event_${key}`, handleEventOnce)
return () => {
document.removeEventListener(`storage_event_${key}`, handleEventOnce)
}
}, [])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if the key is ever changed somewhere in the code base, but to at least be consistent with other hooks dependencies called here:

Suggested change
}, [])
}, [key])


const dispatch = useCallback(
(setStateAction: T | ((prevState?: T) => T)) => {
const value = isFunction(setStateAction) ? setStateAction(getItem(key)) : setStateAction
setState(value)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to prevent redundant re-renders:

Suggested change
setState(value)

setItem(key, value)
document.dispatchEvent(new CustomEvent(`storage_event_${key}`, {}))
},
[key]
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export const VoteForCouncilSuccessModal = ({ onClose, candidateId }: Props) => {
const { candidate } = useCandidate(candidateId)

const goToElection = useCallback(() => {
history.push(generatePath(ElectionRoutes.currentElection))
onClose()
}, [onClose])

Expand Down