Skip to content
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

feat(suite-native): send review cancelation dialog #14540

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions suite-native/intl/src/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,10 @@ export const en = {
},
review: {
confirmOnDeviceMessage: 'Go to your Trezor and confirm the amounts & recipients.',
cancelAlert: {
title: 'Are you sure you’d like to cancel sending the transaction?',
continueButton: 'Continue editing',
},
address: {
title: 'Check the address on your Trezor against the original to make sure it’s correct.',
step1: 'Go to the app or place where you originally got the address.',
Expand Down
1 change: 0 additions & 1 deletion suite-native/module-send/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
"@suite-native/navigation": "workspace:*",
"@suite-native/qr-code": "workspace:*",
"@suite-native/settings": "workspace:*",
"@suite-native/toasts": "workspace:*",
"@trezor/react-utils": "workspace:*",
"@trezor/styles": "workspace:*",
"@trezor/theme": "workspace:*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
} from '@suite-native/navigation';
import { Button, VStack } from '@suite-native/atoms';
import { Translation } from '@suite-native/intl';
import { useToast } from '@suite-native/toasts';
import { AccountsRootState, DeviceRootState, SendRootState } from '@suite-common/wallet-core';
import { nativeSpacings } from '@trezor/theme';

Expand All @@ -40,7 +39,6 @@ type NavigationProps = StackToStackCompositeNavigationProps<
export const AddressReviewStepList = () => {
const route = useRoute<RouteProps>();
const navigation = useNavigation<NavigationProps>();
const { showToast } = useToast();
const dispatch = useDispatch();

const [childHeights, setChildHeights] = useState<number[]>([]);
Expand Down Expand Up @@ -83,13 +81,6 @@ export const AddressReviewStepList = () => {
);

if (isRejected(response)) {
// TODO: display error message based on the error code
showToast({
variant: 'error',
message: 'Something went wrong',
icon: 'closeCircle',
});

navigation.navigate(RootStackRoutes.AccountDetail, {
accountKey,
closeActionType: 'back',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Color } from '@trezor/theme';
import { useDebounce } from '@trezor/react-utils';

import { SendAmountInputProps } from '../types';
import { useSendAmountTransformers } from '../useSendAmountTransformers';
import { useSendAmountTransformers } from '../hooks/useSendAmountTransformers';
import { getOutputFieldName } from '../utils';

export const sendAmountInputWrapperStyle = prepareNativeStyle<{ isDisabled: boolean }>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { selectFiatCurrencyCode } from '@suite-native/settings';

import { SendAmountCurrencyLabelWrapper, sendAmountInputWrapperStyle } from './CryptoAmountInput';
import { SendAmountInputProps } from '../types';
import { useSendAmountTransformers } from '../useSendAmountTransformers';
import { useSendAmountTransformers } from '../hooks/useSendAmountTransformers';
import { getOutputFieldName } from '../utils';

export const FiatAmountInput = ({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useDispatch } from 'react-redux';

import { useAlert } from '@suite-native/alerts';
import { Translation } from '@suite-native/intl';
import { cancelSignSendFormTransactionThunk } from '@suite-common/wallet-core';

type AlertResolveValue = { wasReviewCanceled: boolean };

export const useShowReviewCancellationAlert = () => {
const { showAlert } = useAlert();
const dispatch = useDispatch();

const showReviewCancellationAlert = () =>
new Promise<AlertResolveValue>(resolve =>
showAlert({
title: <Translation id="moduleSend.review.cancelAlert.title" />,
primaryButtonTitle: <Translation id="generic.buttons.cancel" />,
primaryButtonVariant: 'redBold',
secondaryButtonVariant: 'redElevation0',
secondaryButtonTitle: (
<Translation id="moduleSend.review.cancelAlert.continueButton" />
),
onPressPrimaryButton: () => {
dispatch(cancelSignSendFormTransactionThunk());

return resolve({ wasReviewCanceled: true });
},
onPressSecondaryButton: () => resolve({ wasReviewCanceled: false }),
}),
);

return showReviewCancellationAlert;
};
22 changes: 14 additions & 8 deletions suite-native/module-send/src/navigation/SendStackNavigator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@ export const SendStackNavigator = () => (
<SendStack.Screen name={SendStackRoutes.SendAccounts} component={SendAccountsScreen} />
<SendStack.Screen name={SendStackRoutes.SendOutputs} component={SendOutputsScreen} />
<SendStack.Screen name={SendStackRoutes.SendFees} component={SendFeesScreen} />
<SendStack.Screen
name={SendStackRoutes.SendAddressReview}
component={SendAddressReviewScreen}
/>
<SendStack.Screen
name={SendStackRoutes.SendOutputsReview}
component={SendOutputsReviewScreen}
/>
<SendStack.Group
screenOptions={{
gestureEnabled: false, // To not interrupt send review by back navigation.
}}
>
<SendStack.Screen
name={SendStackRoutes.SendAddressReview}
component={SendAddressReviewScreen}
/>
<SendStack.Screen
name={SendStackRoutes.SendOutputsReview}
component={SendOutputsReviewScreen}
/>
</SendStack.Group>
</SendStack.Navigator>
);
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import { useDispatch, useSelector } from 'react-redux';
import { useSelector } from 'react-redux';
import { useEffect } from 'react';

import { SendStackParamList, SendStackRoutes, StackProps } from '@suite-native/navigation';
import { Box, VStack } from '@suite-native/atoms';
import { Translation, useTranslate } from '@suite-native/intl';
import {
AccountsRootState,
DeviceRootState,
SendRootState,
cancelSignSendFormTransactionThunk,
} from '@suite-common/wallet-core';
import { AccountsRootState, DeviceRootState, SendRootState } from '@suite-common/wallet-core';
import { Text } from '@suite-native/atoms';

import {
Expand All @@ -20,15 +15,17 @@ import { AddressReviewStepList } from '../components/AddressReviewStepList';
import { SendScreen } from '../components/SendScreen';
import { SendScreenSubHeader } from '../components/SendScreenSubHeader';
import { SendConfirmOnDeviceImage } from '../components/SendConfirmOnDeviceImage';
import { useShowReviewCancellationAlert } from '../hooks/useShowReviewCancellationAlert';

export const SendAddressReviewScreen = ({
route,
navigation,
}: StackProps<SendStackParamList, SendStackRoutes.SendAddressReview>) => {
const { accountKey } = route.params;
const dispatch = useDispatch();
const { translate } = useTranslate();

const showReviewCancellationAlert = useShowReviewCancellationAlert();

const isAddressConfirmed = useSelector(
(state: AccountsRootState & DeviceRootState & SendRootState) =>
selectIsFirstTransactionAddressConfirmed(state, accountKey),
Expand All @@ -48,9 +45,8 @@ export const SendAddressReviewScreen = ({
useEffect(() => {
const unsubscribe = navigation.addListener('beforeRemove', e => {
if (isReviewInProgress) {
//Navigation is handled by signTransactionThunk response.
dispatch(cancelSignSendFormTransactionThunk());
e.preventDefault();
showReviewCancellationAlert();

return;
}
Expand Down
34 changes: 17 additions & 17 deletions suite-native/module-send/src/screens/SendOutputsReviewScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useDispatch } from 'react-redux';
import { useEffect } from 'react';
import { useDispatch } from 'react-redux';

import { isFulfilled } from '@reduxjs/toolkit';
import { useNavigation } from '@react-navigation/native';

import {
Expand All @@ -13,15 +12,15 @@ import {
StackToStackCompositeNavigationProps,
} from '@suite-native/navigation';
import { VStack } from '@suite-native/atoms';
import { cancelSignSendFormTransactionThunk } from '@suite-common/wallet-core';
import { useTranslate } from '@suite-native/intl';

import { ReviewOutputItemList } from '../components/ReviewOutputItemList';
import { OutputsReviewFooter } from '../components/OutputsReviewFooter';
import { SignSuccessMessage } from '../components/SignSuccessMessage';
import { cleanupSendFormThunk } from '../sendFormThunks';
import { SendScreen } from '../components/SendScreen';
import { SendScreenSubHeader } from '../components/SendScreenSubHeader';
import { useShowReviewCancellationAlert } from '../hooks/useShowReviewCancellationAlert';
import { cleanupSendFormThunk } from '../sendFormThunks';

type NavigationProps = StackToStackCompositeNavigationProps<
SendStackParamList,
Expand All @@ -34,25 +33,26 @@ export const SendOutputsReviewScreen = ({
}: StackProps<SendStackParamList, SendStackRoutes.SendOutputsReview>) => {
const { accountKey } = route.params;
const { translate } = useTranslate();
const dispatch = useDispatch();
const navigation = useNavigation<NavigationProps>();
const showReviewCancellationAlert = useShowReviewCancellationAlert();
const dispatch = useDispatch();

useEffect(() => {
const unsubscribe = navigation.addListener('beforeRemove', async e => {
if (e.data.action.type === 'GO_BACK') {
e.preventDefault();
}
const response = await dispatch(cancelSignSendFormTransactionThunk());
if (isFulfilled(response)) {
// If success navigation is handled by signTransactionThunk call on SendAddressReviewScreen.
// We want to modify only behavior of back button actions.
if (e.data.action.type !== 'GO_BACK') return;

vytick marked this conversation as resolved.
Show resolved Hide resolved
e.preventDefault();

const { wasReviewCanceled } = await showReviewCancellationAlert();

return;
if (wasReviewCanceled) {
dispatch(cleanupSendFormThunk({ accountKey, shouldDeleteDraft: false }));
navigation.navigate(RootStackRoutes.AccountDetail, {
accountKey,
closeActionType: 'back',
});
}
dispatch(cleanupSendFormThunk({ accountKey }));
navigation.navigate(RootStackRoutes.AccountDetail, {
accountKey,
closeActionType: 'back',
});
});

return unsubscribe;
Expand Down
7 changes: 0 additions & 7 deletions suite-native/module-send/src/screens/SendOutputsScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
import { getNetworkCompatible } from '@suite-common/wallet-utils';
import { Box, Button } from '@suite-native/atoms';
import { Translation } from '@suite-native/intl';
import { useToast } from '@suite-native/toasts';
import { useDebounce } from '@trezor/react-utils';
import { prepareNativeStyle, useNativeStyles } from '@trezor/styles';
import { FormState } from '@suite-common/wallet-types';
Expand Down Expand Up @@ -79,7 +78,6 @@ export const SendOutputsScreen = ({
const dispatch = useDispatch();
const { applyStyle } = useNativeStyles();
const debounce = useDebounce();
const { showToast } = useToast();
const navigation =
useNavigation<StackNavigationProps<SendStackParamList, SendStackRoutes.SendOutputs>>();

Expand Down Expand Up @@ -187,11 +185,6 @@ export const SendOutputsScreen = ({

return;
}

// TODO: display error message based on the error code saved in the redux state
showToast({ variant: 'error', message: 'Something went wrong', icon: 'closeCircle' });

navigation.navigate(SendStackRoutes.SendAccounts);
});

return (
Expand Down
12 changes: 10 additions & 2 deletions suite-native/module-send/src/sendFormThunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,19 @@ export const signTransactionNativeThunk = createThunk(

export const cleanupSendFormThunk = createThunk(
`${SEND_MODULE_PREFIX}/cleanupSendFormThunk`,
({ accountKey }: { accountKey: AccountKey }, { dispatch, getState }) => {
(
{
accountKey,
shouldDeleteDraft = true,
}: { accountKey: AccountKey; shouldDeleteDraft?: boolean },
{ dispatch, getState },
) => {
const device = selectDevice(getState());

dispatch(sendFormActions.dispose());
dispatch(sendFormActions.removeDraft({ accountKey }));

if (shouldDeleteDraft) dispatch(sendFormActions.removeDraft({ accountKey }));

dispatch(deviceActions.removeButtonRequests({ device }));
},
);
Expand Down
1 change: 0 additions & 1 deletion suite-native/module-send/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
{ "path": "../navigation" },
{ "path": "../qr-code" },
{ "path": "../settings" },
{ "path": "../toasts" },
{ "path": "../../packages/react-utils" },
{ "path": "../../packages/styles" },
{ "path": "../../packages/theme" },
Expand Down
1 change: 0 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10327,7 +10327,6 @@ __metadata:
"@suite-native/navigation": "workspace:*"
"@suite-native/qr-code": "workspace:*"
"@suite-native/settings": "workspace:*"
"@suite-native/toasts": "workspace:*"
"@trezor/react-utils": "workspace:*"
"@trezor/styles": "workspace:*"
"@trezor/theme": "workspace:*"
Expand Down
Loading