Skip to content

Commit

Permalink
Move TransactionContext to redux store
Browse files Browse the repository at this point in the history
  • Loading branch information
kkosiorowska committed May 10, 2024
1 parent 15c3f9e commit b9dd2d8
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 76 deletions.
13 changes: 5 additions & 8 deletions dapp/src/components/TransactionModal/ActionFormModal.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import React, { useCallback, useState } from "react"
import { Box, ModalBody, ModalCloseButton, ModalHeader } from "@chakra-ui/react"
import {
useStakeFlowContext,
useTransactionContext,
useWalletContext,
} from "#/hooks"
import { useAppDispatch, useStakeFlowContext, useWalletContext } from "#/hooks"
import { ACTION_FLOW_TYPES, ActionFlowType } from "#/types"
import { TokenAmountFormValues } from "#/components/shared/TokenAmountForm/TokenAmountFormBase"
import { logPromiseFailure } from "#/utils"
import StakeFormModal from "./ActiveStakingStep/StakeFormModal"
import UnstakeFormModal from "./ActiveUnstakingStep/UnstakeFormModal"
import { setTokenAmount } from "#/store/action-flow"

Check failure on line 9 in dapp/src/components/TransactionModal/ActionFormModal.tsx

View workflow job for this annotation

GitHub Actions / dapp-format

`#/store/action-flow` import should occur before import of `./ActiveStakingStep/StakeFormModal`

const FORM_DATA: Record<
ActionFlowType,
Expand All @@ -34,8 +31,8 @@ const FORM_DATA: Record<

function ActionFormModal({ type }: { type: ActionFlowType }) {
const { btcAccount, ethAccount } = useWalletContext()
const { setTokenAmount } = useTransactionContext()
const { initStake } = useStakeFlowContext()
const dispatch = useAppDispatch()

const [isLoading, setIsLoading] = useState(false)

Expand All @@ -59,14 +56,14 @@ function ActionFormModal({ type }: { type: ActionFlowType }) {
// TODO: Init unstake flow
if (type === ACTION_FLOW_TYPES.STAKE) await handleInitStake()

setTokenAmount({ amount: values.amount, currency: "bitcoin" })
dispatch(setTokenAmount({ amount: values.amount, currency: "bitcoin" }))
} catch (error) {
console.error(error)
} finally {
setIsLoading(false)
}
},
[handleInitStake, setTokenAmount, type],
[dispatch, handleInitStake, type],
)

const handleSubmitFormWrapper = useCallback(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React, { useCallback } from "react"
import {
useActionFlowTokenAmount,
useDepositBTCTransaction,
useDepositTelemetry,
useExecuteFunction,
useStakeFlowContext,
useToast,
useTransactionContext,
useWalletContext,
} from "#/hooks"
import { logPromiseFailure } from "#/utils"
Expand All @@ -24,7 +24,7 @@ const TOAST = TOASTS[TOAST_ID]

export default function DepositBTCModal() {
const { ethAccount } = useWalletContext()
const { tokenAmount } = useTransactionContext()
const tokenAmount = useActionFlowTokenAmount()
const { btcAddress, depositReceipt, stake } = useStakeFlowContext()
const depositTelemetry = useDepositTelemetry()
const { closeToast, openToast } = useToast()
Expand Down
4 changes: 2 additions & 2 deletions dapp/src/components/TransactionModal/ModalContentWrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from "react"
import {
useActionFlowStatus,
useActionFlowTokenAmount,
useActionFlowType,
useRequestBitcoinAccount,
useRequestEthereumAccount,
useTransactionContext,
useWalletContext,
} from "#/hooks"
import { BitcoinIcon, EthereumIcon } from "#/assets/icons"
Expand All @@ -26,7 +26,7 @@ export default function ModalContentWrapper({
const { requestAccount: requestEthereumAccount } = useRequestEthereumAccount()
const status = useActionFlowStatus()
const type = useActionFlowType()
const { tokenAmount } = useTransactionContext()
const tokenAmount = useActionFlowTokenAmount()

if (!btcAccount || !isSupportedBTCAddressType(btcAccount.address))
return (
Expand Down
16 changes: 7 additions & 9 deletions dapp/src/components/TransactionModal/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useEffect } from "react"
import { StakeFlowProvider, TransactionContextProvider } from "#/contexts"
import { StakeFlowProvider } from "#/contexts"
import { useAppDispatch, useSidebar } from "#/hooks"
import { ActionFlowType, BaseModalProps } from "#/types"
import { ModalCloseButton } from "@chakra-ui/react"
Expand Down Expand Up @@ -33,14 +33,12 @@ function TransactionModalBase({ type }: TransactionModalProps) {
}, [closeSidebar, openSideBar])

return (
<TransactionContextProvider>
<StakeFlowProvider>
<ModalContentWrapper>
<ModalCloseButton />
<ActiveFlowStep />
</ModalContentWrapper>
</StakeFlowProvider>
</TransactionContextProvider>
<StakeFlowProvider>
<ModalContentWrapper>
<ModalCloseButton />
<ActiveFlowStep />
</ModalContentWrapper>
</StakeFlowProvider>
)
}

Expand Down
36 changes: 0 additions & 36 deletions dapp/src/contexts/TransactionContext.tsx

This file was deleted.

1 change: 0 additions & 1 deletion dapp/src/contexts/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ export * from "./WalletApiReactTransportProvider"
export * from "./LedgerWalletAPIProvider"
export * from "./DocsDrawerContext"
export * from "./SidebarContext"
export * from "./TransactionContext"
export * from "./StakeFlowContext"
1 change: 0 additions & 1 deletion dapp/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export * from "./useRequestEthereumAccount"
export * from "./useWalletContext"
export * from "./useSidebar"
export * from "./useDocsDrawer"
export * from "./useTransactionContext"
export * from "./useTransactionDetails"
export * from "./useDepositBTCTransaction"
export * from "./useTransactionHistoryTable"
Expand Down
1 change: 1 addition & 0 deletions dapp/src/hooks/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ export * from "./useMinDepositAmount"
export * from "./useActionFlowType"
export * from "./useActionFlowStatus"
export * from "./useActionFlowActiveStep"
export * from "./useActionFlowTokenAmount"
// TODO: Rename when the old hook is deleted.
export { useActivities as useActivitiesNEW } from "./useActivities"
6 changes: 6 additions & 0 deletions dapp/src/hooks/store/useActionFlowTokenAmount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { selectActionFlowTokenAmount } from "#/store/action-flow"
import { useAppSelector } from "./useAppSelector"

export function useActionFlowTokenAmount() {
return useAppSelector(selectActionFlowTokenAmount)
}
14 changes: 0 additions & 14 deletions dapp/src/hooks/useTransactionContext.ts

This file was deleted.

6 changes: 5 additions & 1 deletion dapp/src/store/action-flow/actionFlowSelectors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ActionFlowType, ProcessStatus } from "#/types"
import { ActionFlowType, ProcessStatus, TokenAmount } from "#/types"
import { RootState } from ".."

export const selectActionFlowType = (state: RootState): ActionFlowType =>
Expand All @@ -9,3 +9,7 @@ export const selectActionFlowActiveStep = (state: RootState): number =>

export const selectActionFlowStatus = (state: RootState): ProcessStatus =>
state.actionFlow.status

export const selectActionFlowTokenAmount = (
state: RootState,
): TokenAmount | undefined => state.actionFlow.tokenAmount
13 changes: 11 additions & 2 deletions dapp/src/store/action-flow/actionFlowSlice.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { ActionFlowType, PROCESS_STATUSES, ProcessStatus } from "#/types"
import {
ActionFlowType,
PROCESS_STATUSES,
ProcessStatus,
TokenAmount,
} from "#/types"
import { PayloadAction, createSlice } from "@reduxjs/toolkit"

type ActionFlowState = {
type: ActionFlowType
activeStep: number
status: ProcessStatus
tokenAmount?: TokenAmount
}

const initialState: ActionFlowState = {
Expand All @@ -26,6 +32,9 @@ export const actionFlowSlice = createSlice({
setStatus(state, action: PayloadAction<ProcessStatus>) {
state.status = action.payload
},
setTokenAmount(state, action: PayloadAction<TokenAmount | undefined>) {
state.tokenAmount = action.payload
},
goNextStep(state) {
state.activeStep += 1
},
Expand All @@ -37,5 +46,5 @@ export const actionFlowSlice = createSlice({
},
})

export const { setType, setStatus, goNextStep, resetState } =
export const { setType, setStatus, setTokenAmount, goNextStep, resetState } =
actionFlowSlice.actions

0 comments on commit b9dd2d8

Please sign in to comment.