Skip to content

Commit

Permalink
Merge pull request #26 from oasisprotocol/ml/reset-home-page-state-sw…
Browse files Browse the repository at this point in the history
…itch-accounts

Reset home page state upon switching accounts
  • Loading branch information
lubej authored Mar 14, 2024
2 parents 6be7ca8 + 416db49 commit d2705af
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 18 deletions.
9 changes: 7 additions & 2 deletions frontend/src/pages/HomePage/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FC, useState } from 'react'
import { FC, useEffect, useState } from 'react'
import { CaretRightIcon } from '../../components/icons/CaretRightIcon.tsx'
import { Button } from '../../components/Button'
import { Card } from '../../components/Card'
Expand All @@ -16,7 +16,7 @@ type MascotChoices = 0 | 1 | 2

export const HomePage: FC = () => {
const {
state: { isConnected },
state: { isConnected, account },
vote,
canVoteOnPoll,
} = useWeb3()
Expand All @@ -31,6 +31,11 @@ export const HomePage: FC = () => {
const [isLoading, setIsLoading] = useState(false)
const [error, setError] = useState('')

useEffect(() => {
setPageStatus('vote')
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [account])

const handleSelectChoice = (choice: MascotChoices) => {
setSelectedChoice(choice)
}
Expand Down
4 changes: 1 addition & 3 deletions frontend/src/providers/Web3Context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createContext } from 'react'
import * as sapphire from '@oasisprotocol/sapphire-paratime'
import { BigNumberish, BrowserProvider, Signer, TransactionResponse } from 'ethers'
import { BigNumberish, BrowserProvider, TransactionResponse } from 'ethers'
import { type PollManager } from '@oasisprotocol/dapp-voting-backend/src/contracts'
import { DefaultReturnType } from '@oasisprotocol/dapp-voting-backend/src/contracts/common.ts'
import { Poll } from '../types'
Expand All @@ -10,11 +10,9 @@ export interface Web3ProviderState {
isVoidSignerConnected: boolean
ethProvider: BrowserProvider | null
sapphireEthProvider: (BrowserProvider & sapphire.SapphireAnnex) | null
signer: Signer | null
account: string | null
explorerBaseUrl: string | null
chainName: string | null
pollManager: PollManager | null
pollManagerVoidSigner: PollManager | null
}

Expand Down
18 changes: 5 additions & 13 deletions frontend/src/providers/Web3Provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ const web3ProviderInitialState: Web3ProviderState = {
isVoidSignerConnected: false,
ethProvider: null,
sapphireEthProvider: null,
signer: null,
account: null,
explorerBaseUrl: null,
chainName: null,
pollManager: null,
pollManagerVoidSigner: null,
}

Expand Down Expand Up @@ -138,17 +136,12 @@ export const Web3ContextProvider: FC<PropsWithChildren> = ({ children }) => {
const network = await sapphireEthProvider.getNetwork()
_setNetworkSpecificVars(network.chainId, sapphireEthProvider)

const signer = sapphire.wrapEthersSigner(await sapphireEthProvider.getSigner())
const pollManager = PollManager__factory.connect(VITE_CONTRACT_POLLMANAGER, signer)

setState(prevState => ({
...prevState,
isConnected: true,
ethProvider,
sapphireEthProvider,
account,
signer,
pollManager,
}))
} catch (ex) {
setState(prevState => ({
Expand Down Expand Up @@ -238,15 +231,14 @@ export const Web3ContextProvider: FC<PropsWithChildren> = ({ children }) => {
}

const vote = async (choiceId: BigNumberish) => {
const { pollManager, signer } = state
const { sapphireEthProvider } = state

if (!pollManager) {
throw new Error('[pollManager] not initialized!')
if (!sapphireEthProvider) {
throw new Error('[sapphireEthProvider] not initialized!')
}

if (!signer) {
throw new Error('[signer] Signer not connected!')
}
const signer = sapphire.wrapEthersSigner(await sapphireEthProvider.getSigner())
const pollManager = PollManager__factory.connect(VITE_CONTRACT_POLLMANAGER, signer)

const unsignedTx = await pollManager.vote.populateTransaction(VITE_PROPOSAL_ID, choiceId, EMPTY_IN_DATA)
unsignedTx.gasLimit = MAX_GAS_LIMIT
Expand Down

0 comments on commit d2705af

Please sign in to comment.