-
Notifications
You must be signed in to change notification settings - Fork 89
feat: improve reusability of operation form components #5133
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
Open
therealemjy
wants to merge
1
commit into
main
Choose a base branch
from
refactor/operation-details-forms
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@venusprotocol/evm": minor | ||
| --- | ||
|
|
||
| add Footer component to share between Operation forms |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,22 @@ | ||
| { | ||
| "editor.codeActionsOnSave": { | ||
| "source.organizeImports.biome": "explicit" | ||
| }, | ||
| "editor.defaultFormatter": "biomejs.biome", | ||
| "editor.formatOnSave": true, | ||
| "[typescriptreact]": { | ||
| "editor.defaultFormatter": "biomejs.biome" | ||
| }, | ||
| "files.associations": { | ||
| "*.css": "tailwindcss" | ||
| }, | ||
| "editor.quickSuggestions": { | ||
| "strings": "on" | ||
| }, | ||
| "[tailwindcss]": { | ||
| "editor.defaultFormatter": "vscode.css-language-features" | ||
| }, | ||
| "[css]": { | ||
| "editor.defaultFormatter": "vscode.css-language-features" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
apps/evm/src/containers/ApproveDelegate/__tests__/index.spec.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import type { Mock } from 'vitest'; | ||
|
|
||
| import { fireEvent, waitFor } from '@testing-library/react'; | ||
| import fakePoolComptrollerContractAddress, { | ||
| altAddress as delegateeAddress, | ||
| } from '__mocks__/models/address'; | ||
| import useDelegateApproval from 'hooks/useDelegateApproval'; | ||
| import { en } from 'libs/translations'; | ||
| import { renderComponent } from 'testUtils/render'; | ||
| import { ApproveDelegate, type ApproveDelegateProps } from '..'; | ||
|
|
||
| const props: ApproveDelegateProps = { | ||
| poolComptrollerContractAddress: fakePoolComptrollerContractAddress, | ||
| delegateeAddress: delegateeAddress, | ||
| secondStepButtonLabel: 'Fake second step button label', | ||
| children: 'Fake children', | ||
| }; | ||
|
|
||
| vi.mock('hooks/useDelegateApproval'); | ||
|
|
||
| describe('ApproveDelegate', () => { | ||
| it('prompts user to approve delegatee if it is not', async () => { | ||
| const mockUpdatePoolDelegateStatus = vi.fn(); | ||
|
|
||
| (useDelegateApproval as Mock).mockImplementation(() => ({ | ||
| updatePoolDelegateStatus: mockUpdatePoolDelegateStatus, | ||
| isDelegateApproved: false, | ||
| isUpdateDelegateStatusLoading: false, | ||
| isDelegateApprovedLoading: false, | ||
| })); | ||
|
|
||
| const { getByText } = renderComponent(<ApproveDelegate {...props} />); | ||
|
|
||
| await waitFor(() => expect(getByText(en.approveDelegateSteps.approveDelegateButton.text))); | ||
|
|
||
| fireEvent.click(getByText(en.approveDelegateSteps.approveDelegateButton.text)); | ||
|
|
||
| expect(mockUpdatePoolDelegateStatus).toHaveBeenCalledTimes(1); | ||
| expect((mockUpdatePoolDelegateStatus as Mock).mock.calls[0]).toMatchInlineSnapshot(` | ||
| [ | ||
| { | ||
| "approvedStatus": true, | ||
| }, | ||
| ] | ||
| `); | ||
| }); | ||
|
|
||
| it('displays children if delegate is approved', async () => { | ||
| (useDelegateApproval as Mock).mockImplementation(() => ({ | ||
| updatePoolDelegateStatus: vi.fn(), | ||
| isDelegateApproved: true, | ||
| isUpdateDelegateStatusLoading: false, | ||
| isDelegateApprovedLoading: false, | ||
| })); | ||
|
|
||
| const { getByText } = renderComponent(<ApproveDelegate {...props} />); | ||
|
|
||
| await waitFor(() => expect(getByText(props.children as string))); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import type { Address } from 'viem'; | ||
|
|
||
| import { ApprovalSteps } from 'components'; | ||
| import useDelegateApproval from 'hooks/useDelegateApproval'; | ||
| import { useTranslation } from 'libs/translations'; | ||
|
|
||
| export interface ApproveDelegateProps { | ||
| poolComptrollerContractAddress: Address; | ||
| delegateeAddress: Address; | ||
| secondStepButtonLabel: string; | ||
| children: React.ReactNode; | ||
| } | ||
|
|
||
| export const ApproveDelegate: React.FC<ApproveDelegateProps> = ({ | ||
| poolComptrollerContractAddress, | ||
| delegateeAddress, | ||
| secondStepButtonLabel, | ||
| children, | ||
| }) => { | ||
| const { t } = useTranslation(); | ||
|
|
||
| const { | ||
| isUpdateDelegateStatusLoading, | ||
| updatePoolDelegateStatus, | ||
| isDelegateApproved, | ||
| isDelegateApprovedLoading, | ||
| } = useDelegateApproval({ | ||
| delegateeAddress, | ||
| poolComptrollerAddress: poolComptrollerContractAddress, | ||
| }); | ||
|
|
||
| const approveDelegate = () => { | ||
| return updatePoolDelegateStatus({ approvedStatus: true }); | ||
| }; | ||
|
|
||
| const showApproveDelegateStep = | ||
| !isDelegateApprovedLoading && isDelegateApproved !== undefined && !isDelegateApproved; | ||
|
|
||
| return ( | ||
| <ApprovalSteps | ||
| showApprovalSteps={showApproveDelegateStep} | ||
| isApprovalActionLoading={isUpdateDelegateStatusLoading} | ||
| approvalAction={approveDelegate} | ||
| firstStepLabel={t('approveDelegateSteps.step1')} | ||
| firstStepTooltip={t('approveDelegateSteps.approveDelegateButton.tooltip')} | ||
| firstStepButtonLabel={t('approveDelegateSteps.approveDelegateButton.text')} | ||
| secondStepLabel={t('approveTokenSteps.step2')} | ||
| secondStepButtonLabel={secondStepButtonLabel} | ||
| > | ||
| {children} | ||
| </ApprovalSteps> | ||
| ); | ||
| }; |
57 changes: 57 additions & 0 deletions
57
apps/evm/src/containers/ApproveToken/__tests__/index.spec.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import type { Mock } from 'vitest'; | ||
|
|
||
| import { fireEvent, waitFor } from '@testing-library/react'; | ||
| import fakeSpenderAddress from '__mocks__/models/address'; | ||
| import { xvs } from '__mocks__/models/tokens'; | ||
| import useTokenApproval from 'hooks/useTokenApproval'; | ||
| import { en } from 'libs/translations'; | ||
| import { renderComponent } from 'testUtils/render'; | ||
| import { ApproveToken, type ApproveTokenProps } from '..'; | ||
|
|
||
| const props: ApproveTokenProps = { | ||
| token: xvs, | ||
| spenderAddress: fakeSpenderAddress, | ||
| secondStepButtonLabel: 'Fake second step button label', | ||
| children: 'Fake children', | ||
| }; | ||
|
|
||
| vi.mock('hooks/useTokenApproval'); | ||
|
|
||
| describe('ApproveToken', () => { | ||
| it('prompts user to approve token if it is not', async () => { | ||
| const mockApproveToken = vi.fn(); | ||
|
|
||
| (useTokenApproval as Mock).mockImplementation(() => ({ | ||
| approveToken: mockApproveToken, | ||
| isTokenApproved: false, | ||
| isApproveTokenLoading: false, | ||
| isWalletSpendingLimitLoading: false, | ||
| })); | ||
|
|
||
| const { getByText } = renderComponent(<ApproveToken {...props} />); | ||
|
|
||
| const submitButtonLabel = en.approveTokenSteps.approveTokenButton.text.replace( | ||
| '{{tokenSymbol}}', | ||
| xvs.symbol, | ||
| ); | ||
|
|
||
| await waitFor(() => expect(getByText(submitButtonLabel))); | ||
|
|
||
| fireEvent.click(getByText(submitButtonLabel)); | ||
|
|
||
| expect(mockApproveToken).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('displays children if token is approved', async () => { | ||
| (useTokenApproval as Mock).mockImplementation(() => ({ | ||
| approveToken: vi.fn(), | ||
| isTokenApproved: true, | ||
| isApproveTokenLoading: false, | ||
| isWalletSpendingLimitLoading: false, | ||
| })); | ||
|
|
||
| const { getByText } = renderComponent(<ApproveToken {...props} />); | ||
|
|
||
| await waitFor(() => expect(getByText(props.children as string))); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import type { Address } from 'viem'; | ||
|
|
||
| import { ApprovalSteps } from 'components'; | ||
| import useTokenApproval from 'hooks/useTokenApproval'; | ||
| import { useTranslation } from 'libs/translations'; | ||
| import { useAccountAddress } from 'libs/wallet'; | ||
| import type { Token } from 'types'; | ||
|
|
||
| export interface ApproveTokenProps { | ||
| token: Token; | ||
| spenderAddress: Address; | ||
| secondStepButtonLabel: string; | ||
| children: React.ReactNode; | ||
| } | ||
|
|
||
| export const ApproveToken: React.FC<ApproveTokenProps> = ({ | ||
| token, | ||
| spenderAddress, | ||
| secondStepButtonLabel, | ||
| children, | ||
| }) => { | ||
| const { accountAddress } = useAccountAddress(); | ||
| const { t } = useTranslation(); | ||
|
|
||
| const { isTokenApproved, approveToken, isApproveTokenLoading, isWalletSpendingLimitLoading } = | ||
| useTokenApproval({ | ||
| token, | ||
| spenderAddress, | ||
| accountAddress, | ||
| }); | ||
|
|
||
| const showApproveTokenStep = | ||
| !isWalletSpendingLimitLoading && isTokenApproved !== undefined && !isTokenApproved; | ||
|
|
||
| return ( | ||
| <ApprovalSteps | ||
| showApprovalSteps={showApproveTokenStep} | ||
| isApprovalActionLoading={isApproveTokenLoading} | ||
| approvalAction={approveToken} | ||
| firstStepLabel={t('approveTokenSteps.step1')} | ||
| firstStepTooltip={t('approveTokenSteps.approveTokenButton.tooltip')} | ||
| firstStepButtonLabel={t('approveTokenSteps.approveTokenButton.text', { | ||
| tokenSymbol: token.symbol, | ||
| })} | ||
| secondStepLabel={t('approveTokenSteps.step2')} | ||
| secondStepButtonLabel={secondStepButtonLabel} | ||
| > | ||
| {children} | ||
| </ApprovalSteps> | ||
| ); | ||
| }; |
45 changes: 0 additions & 45 deletions
45
apps/evm/src/containers/Form/RhfSubmitButton/ApproveTokenSteps/index.tsx
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.