From d301b499c28b1bbb636d518f0b3a7d8edbd0925d Mon Sep 17 00:00:00 2001 From: mkbeefcake Date: Mon, 5 Dec 2022 18:43:08 -0500 Subject: [PATCH] removed debugger --- .../ui/src/app/pages/Election/Election.tsx | 12 +-------- .../ui/src/common/hooks/useCustomEvent.ts | 25 ------------------- .../ui/src/common/hooks/useLocalStorage.ts | 12 +++++++++ .../modals/RestoreVotes/RestoreVotesModal.tsx | 3 --- 4 files changed, 13 insertions(+), 39 deletions(-) delete mode 100644 packages/ui/src/common/hooks/useCustomEvent.ts diff --git a/packages/ui/src/app/pages/Election/Election.tsx b/packages/ui/src/app/pages/Election/Election.tsx index c052e1eefe..2d8be6f183 100644 --- a/packages/ui/src/app/pages/Election/Election.tsx +++ b/packages/ui/src/app/pages/Election/Election.tsx @@ -4,14 +4,12 @@ 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 { useCustomEvent } from '@/common/hooks/useCustomEvent' import { useRefetchQueries } from '@/common/hooks/useRefetchQueries' import { MILLISECONDS_PER_BLOCK } from '@/common/model/formatters' import { getUrl } from '@/common/utils/getUrl' @@ -58,14 +56,6 @@ export const Election = () => { } }, [electionStage]) - const { onceEvent } = useCustomEvent() - - useEffect(() => { - onceEvent('RefreshElectionPageEvent', () => { - history.go(0) - }) - }, []) - if (isLoadingElectionStage) { return } /> } diff --git a/packages/ui/src/common/hooks/useCustomEvent.ts b/packages/ui/src/common/hooks/useCustomEvent.ts deleted file mode 100644 index 5cc44d583f..0000000000 --- a/packages/ui/src/common/hooks/useCustomEvent.ts +++ /dev/null @@ -1,25 +0,0 @@ -export const useCustomEvent = () => { - const onEvent = (eventType: string, listener: any) => { - document.addEventListener(eventType, listener) - } - - const offEvent = (eventType: string, listener: any) => { - document.removeEventListener(eventType, listener) - } - - const onceEvent = (eventType: string, listener: any) => { - const handleEventOnce = (event: any) => { - listener(event) - offEvent(eventType, handleEventOnce) - } - - onEvent(eventType, handleEventOnce) - } - - const triggerEvent = (eventType: string, data: any) => { - const event = new CustomEvent(eventType, { detail: data }) - document.dispatchEvent(event) - } - - return { onEvent, offEvent, onceEvent, triggerEvent } -} diff --git a/packages/ui/src/common/hooks/useLocalStorage.ts b/packages/ui/src/common/hooks/useLocalStorage.ts index 20b33e85be..0f4b135695 100644 --- a/packages/ui/src/common/hooks/useLocalStorage.ts +++ b/packages/ui/src/common/hooks/useLocalStorage.ts @@ -41,11 +41,23 @@ export const useLocalStorage = (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) + } + }, []) + const dispatch = useCallback( (setStateAction: T | ((prevState?: T) => T)) => { const value = isFunction(setStateAction) ? setStateAction(getItem(key)) : setStateAction setState(value) setItem(key, value) + document.dispatchEvent(new CustomEvent(`storage_event_${key}`, {})) }, [key] ) diff --git a/packages/ui/src/council/modals/RestoreVotes/RestoreVotesModal.tsx b/packages/ui/src/council/modals/RestoreVotes/RestoreVotesModal.tsx index 0609d530e8..868fba08e8 100644 --- a/packages/ui/src/council/modals/RestoreVotes/RestoreVotesModal.tsx +++ b/packages/ui/src/council/modals/RestoreVotes/RestoreVotesModal.tsx @@ -4,7 +4,6 @@ import * as Yup from 'yup' import { ButtonPrimary } from '@/common/components/buttons' import { FileEntry, FileInput } from '@/common/components/forms/FileInput' import { Modal, ModalBody, ModalHeader, ModalFooter } from '@/common/components/Modal' -import { useCustomEvent } from '@/common/hooks/useCustomEvent' import { useLocalStorage } from '@/common/hooks/useLocalStorage' import { useModal } from '@/common/hooks/useModal' import { dedupeObjects } from '@/common/utils' @@ -68,7 +67,6 @@ export const RestoreVotesModal = () => { } = useModal() const [votingAttempts, setVotingAttempts] = useLocalStorage(`votes:${cycleId}`) const [value, dispatch] = useReducer(valueReducer(cycleId), undefined) - const { triggerEvent } = useCustomEvent() const onUpload = useCallback(async ([file]: File[]) => { if (!file) return @@ -84,7 +82,6 @@ export const RestoreVotesModal = () => { try { VotingAttemptsSchema.validateSync(votingAttempts) setVotingAttempts(dedupeObjects([...(votingAttempts ?? []), ...content.votingAttempts])) - triggerEvent('RefreshElectionPageEvent', {}) } catch (error) { if (error instanceof Yup.ValidationError) { setVotingAttempts(content.votingAttempts)