-
-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(suite): refactor connect popup to store state in redux
- Loading branch information
Showing
12 changed files
with
159 additions
and
78 deletions.
There are no files selected for viewing
92 changes: 46 additions & 46 deletions
92
packages/suite/src/components/suite/modals/ReduxModal/UserContextModal/ConnectPopupModal.tsx
This file contains 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,54 +1,54 @@ | ||
import { connectPopupActions, selectConnectPopupCall } from '@suite-common/connect-popup'; | ||
import { H2, NewModal, Paragraph } from '@trezor/components'; | ||
import { ERRORS } from '@trezor/connect'; | ||
import { spacings } from '@trezor/theme'; | ||
|
||
import { Translation } from 'src/components/suite'; | ||
import { useDispatch, useSelector } from 'src/hooks/suite'; | ||
|
||
interface ConnectPopupModalProps { | ||
method: string; | ||
processName?: string; | ||
origin?: string; | ||
onConfirm: () => void; | ||
onCancel: () => void; | ||
} | ||
export const ConnectPopupModal = () => { | ||
const dispatch = useDispatch(); | ||
const popupCall = useSelector(selectConnectPopupCall); | ||
if (!popupCall) return null; | ||
|
||
export const ConnectPopupModal = ({ | ||
method, | ||
processName, | ||
origin, | ||
onConfirm, | ||
onCancel, | ||
}: ConnectPopupModalProps) => ( | ||
<NewModal | ||
onCancel={onCancel} | ||
iconName="plugs" | ||
variant="primary" | ||
bottomContent={ | ||
<> | ||
<NewModal.Button variant="tertiary" onClick={onCancel}> | ||
<Translation id="TR_CANCEL" /> | ||
</NewModal.Button> | ||
<NewModal.Button variant="primary" onClick={onConfirm}> | ||
<Translation id="TR_CONFIRM" /> | ||
</NewModal.Button> | ||
</> | ||
} | ||
heading={<Translation id="TR_TREZOR_CONNECT" />} | ||
> | ||
<H2>{method}</H2> | ||
const { method, processName, origin } = popupCall; | ||
const onConfirm = () => dispatch(connectPopupActions.approveCall()); | ||
const onCancel = () => | ||
dispatch(connectPopupActions.rejectCall(ERRORS.TypedError('Method_Cancel'))); | ||
|
||
{processName && ( | ||
<Paragraph margin={{ top: spacings.xs }}> | ||
<Translation id="TR_CONNECT_MODAL_PROCESS" /> <strong>{processName}</strong> | ||
</Paragraph> | ||
)} | ||
{origin && ( | ||
<Paragraph> | ||
<Translation id="TR_CONNECT_MODAL_WEB_ORIGIN" /> <strong>{origin}</strong> | ||
</Paragraph> | ||
)} | ||
return ( | ||
<NewModal | ||
onCancel={onCancel} | ||
iconName="plugs" | ||
variant="primary" | ||
bottomContent={ | ||
<> | ||
<NewModal.Button variant="tertiary" onClick={onCancel}> | ||
<Translation id="TR_CANCEL" /> | ||
</NewModal.Button> | ||
<NewModal.Button variant="primary" onClick={onConfirm}> | ||
<Translation id="TR_CONFIRM" /> | ||
</NewModal.Button> | ||
</> | ||
} | ||
heading={<Translation id="TR_TREZOR_CONNECT" />} | ||
> | ||
<H2>{method}</H2> | ||
|
||
<Paragraph variant="tertiary" margin={{ top: spacings.xs }}> | ||
<Translation id="TR_CONNECT_MODAL_REQUEST_DESCRIPTION" /> | ||
</Paragraph> | ||
</NewModal> | ||
); | ||
{processName && ( | ||
<Paragraph margin={{ top: spacings.xs }}> | ||
<Translation id="TR_CONNECT_MODAL_PROCESS" /> <strong>{processName}</strong> | ||
</Paragraph> | ||
)} | ||
{origin && ( | ||
<Paragraph> | ||
<Translation id="TR_CONNECT_MODAL_WEB_ORIGIN" /> <strong>{origin}</strong> | ||
</Paragraph> | ||
)} | ||
|
||
<Paragraph variant="tertiary" margin={{ top: spacings.xs }}> | ||
<Translation id="TR_CONNECT_MODAL_REQUEST_DESCRIPTION" /> | ||
</Paragraph> | ||
</NewModal> | ||
); | ||
}; |
This file contains 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 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 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 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 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,21 @@ | ||
import { createAction } from '@reduxjs/toolkit'; | ||
|
||
import { ConnectPopupCall } from './connectPopupTypes'; | ||
|
||
export const ACTION_PREFIX = '@suite-common/connect-popup'; | ||
|
||
const initiateCall = createAction(`${ACTION_PREFIX}/initiateCall`, (payload: ConnectPopupCall) => ({ | ||
payload, | ||
})); | ||
|
||
const approveCall = createAction(`${ACTION_PREFIX}/approveCall`); | ||
|
||
const rejectCall = createAction(`${ACTION_PREFIX}/rejectCall`, (payload: Error) => ({ | ||
payload, | ||
})); | ||
|
||
export const connectPopupActions = { | ||
initiateCall, | ||
approveCall, | ||
rejectCall, | ||
} as const; |
This file contains 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,21 @@ | ||
import { createMiddlewareWithExtraDeps } from '@suite-common/redux-utils'; | ||
|
||
import { connectPopupActions } from './connectPopupActions'; | ||
|
||
export const prepareConnectPopupMiddleware = createMiddlewareWithExtraDeps( | ||
async (action, { dispatch, next, extra }) => { | ||
await next(action); | ||
|
||
if (connectPopupActions.initiateCall.match(action)) { | ||
dispatch(extra.actions.openModal({ type: 'connect-popup' })); | ||
} | ||
if ( | ||
connectPopupActions.approveCall.match(action) || | ||
connectPopupActions.rejectCall.match(action) | ||
) { | ||
dispatch(extra.actions.onModalCancel()); | ||
} | ||
|
||
return action; | ||
}, | ||
); |
This file contains 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,37 @@ | ||
import { createReducerWithExtraDeps } from '@suite-common/redux-utils'; | ||
|
||
import { connectPopupActions } from './connectPopupActions'; | ||
import { ConnectPopupCall } from './connectPopupTypes'; | ||
|
||
export type ConnectPopupState = { | ||
activeCall?: ConnectPopupCall; | ||
}; | ||
|
||
type ConnectPopupStateRootState = { | ||
wallet: { connectPopup: ConnectPopupState }; | ||
}; | ||
|
||
const connectPopupInitialState: ConnectPopupState = { | ||
activeCall: undefined, | ||
}; | ||
|
||
export const prepareConnectPopupReducer = createReducerWithExtraDeps( | ||
connectPopupInitialState, | ||
(builder, _extra) => { | ||
builder | ||
.addCase(connectPopupActions.initiateCall, (state, { payload }) => { | ||
state.activeCall = payload; | ||
}) | ||
.addCase(connectPopupActions.approveCall, state => { | ||
state?.activeCall?.confirmation.resolve(); | ||
state.activeCall = undefined; | ||
}) | ||
.addCase(connectPopupActions.rejectCall, (state, { payload }) => { | ||
state?.activeCall?.confirmation.reject(payload); | ||
state.activeCall = undefined; | ||
}); | ||
}, | ||
); | ||
|
||
export const selectConnectPopupCall = (state: ConnectPopupStateRootState) => | ||
state.wallet.connectPopup.activeCall; |
This file contains 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 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,8 @@ | ||
import { Deferred } from '@trezor/utils'; | ||
|
||
export type ConnectPopupCall = { | ||
method: string; | ||
processName?: string; | ||
origin?: string; | ||
confirmation: Deferred<void>; | ||
}; |
This file contains 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 +1,4 @@ | ||
export * from './connectPopupActions'; | ||
export * from './connectPopupThunks'; | ||
export * from './connectPopupMiddleware'; | ||
export * from './connectPopupReducer'; |
This file contains 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