|
1 | | -import { sha256 } from 'js-sha256'; |
2 | 1 | import React from 'react'; |
3 | | -import { Keyboard } from 'react-native'; |
| 2 | +import { AccessibilityInfo, Keyboard, StyleSheet, Text, View } from 'react-native'; |
| 3 | +import { sha256 } from 'js-sha256'; |
| 4 | +import { useForm } from 'react-hook-form'; |
4 | 5 | import { useDispatch } from 'react-redux'; |
5 | 6 |
|
6 | | -import { deleteAccount } from '../../../../actions/login'; |
7 | | -import { useActionSheet } from '../../../../containers/ActionSheet'; |
8 | | -import ActionSheetContentWithInputAndSubmit from '../../../../containers/ActionSheet/ActionSheetContentWithInputAndSubmit'; |
9 | 7 | import i18n from '../../../../i18n'; |
10 | | -import { showErrorAlert } from '../../../../lib/methods/helpers'; |
11 | | -import { events, logEvent } from '../../../../lib/methods/helpers/log'; |
| 8 | +import sharedStyles from '../../../Styles'; |
| 9 | +import FooterButtons from './FooterButtons'; |
| 10 | +import ConfirmDeleteAccountContent from './ConfirmDeleteAccountContent'; |
12 | 11 | import { deleteOwnAccount } from '../../../../lib/services/restApi'; |
| 12 | +import { deleteAccount } from '../../../../actions/login'; |
| 13 | +import { CustomIcon } from '../../../../containers/CustomIcon'; |
| 14 | +import { isIOS } from '../../../../lib/methods/helpers'; |
13 | 15 | import { useTheme } from '../../../../theme'; |
| 16 | +import { ControlledFormTextInput } from '../../../../containers/TextInput'; |
| 17 | +import { useActionSheet } from '../../../../containers/ActionSheet/Provider'; |
| 18 | +import { events, logEvent } from '../../../../lib/methods/helpers/log'; |
14 | 19 | import { getTranslations } from './getTranslations'; |
15 | | -import AlertText from './AlertText'; |
16 | 20 |
|
17 | | -export function DeleteAccountActionSheetContent(): React.ReactElement { |
| 21 | +const styles = StyleSheet.create({ |
| 22 | + subtitleText: { |
| 23 | + ...sharedStyles.textRegular, |
| 24 | + fontSize: 16, |
| 25 | + lineHeight: 24 |
| 26 | + }, |
| 27 | + titleContainerText: { |
| 28 | + ...sharedStyles.textBold, |
| 29 | + fontSize: 16, |
| 30 | + lineHeight: 24, |
| 31 | + paddingLeft: 12 |
| 32 | + }, |
| 33 | + titleContainer: { |
| 34 | + paddingRight: 80, |
| 35 | + marginBottom: 12, |
| 36 | + flexDirection: 'row', |
| 37 | + alignItems: 'center' |
| 38 | + }, |
| 39 | + inputContainer: { |
| 40 | + marginTop: 12, |
| 41 | + marginBottom: 36 |
| 42 | + } |
| 43 | +}); |
| 44 | + |
| 45 | +const DeleteAccountActionSheetContent = (): React.ReactElement => { |
| 46 | + const { colors } = useTheme(); |
18 | 47 | const { hideActionSheet, showActionSheet } = useActionSheet(); |
19 | 48 | const dispatch = useDispatch(); |
20 | | - const { colors } = useTheme(); |
| 49 | + const { |
| 50 | + control, |
| 51 | + getValues, |
| 52 | + setError, |
| 53 | + formState: { errors } |
| 54 | + } = useForm({ |
| 55 | + defaultValues: { |
| 56 | + password: '' |
| 57 | + } |
| 58 | + }); |
21 | 59 |
|
22 | | - const handleDeleteAccount = async (password: string) => { |
| 60 | + const handleDeleteAccount = async () => { |
| 61 | + const { password } = getValues(); |
23 | 62 | Keyboard.dismiss(); |
24 | 63 | try { |
25 | 64 | await deleteOwnAccount(sha256(password)); |
26 | 65 | hideActionSheet(); |
27 | 66 | } catch (error: any) { |
28 | | - hideActionSheet(); |
29 | 67 | if (error.data.errorType === 'user-last-owner') { |
30 | 68 | const { shouldChangeOwner, shouldBeRemoved } = error.data.details; |
31 | 69 | const { changeOwnerRooms, removedRooms } = getTranslations({ shouldChangeOwner, shouldBeRemoved }); |
32 | | - |
| 70 | + hideActionSheet(); |
33 | 71 | setTimeout(() => { |
34 | 72 | showActionSheet({ |
35 | 73 | children: ( |
36 | | - <ConfirmDeleteAccountActionSheetContent |
37 | | - changeOwnerRooms={changeOwnerRooms} |
38 | | - removedRooms={removedRooms} |
39 | | - password={sha256(password)} |
40 | | - /> |
| 74 | + <ConfirmDeleteAccountContent changeOwnerRooms={changeOwnerRooms} password={password} removedRooms={removedRooms} /> |
41 | 75 | ) |
42 | 76 | }); |
43 | 77 | }, 250); // timeout for hide effect |
44 | 78 | } else if (error.data.errorType === 'error-invalid-password') { |
45 | 79 | logEvent(events.DELETE_OWN_ACCOUNT_F); |
46 | | - showErrorAlert(i18n.t('error-invalid-password')); |
| 80 | + setError('password', { message: i18n.t('error-invalid-password'), type: 'validate' }); |
| 81 | + AccessibilityInfo.announceForAccessibility(i18n.t('error-invalid-password')); |
47 | 82 | } else { |
48 | 83 | logEvent(events.DELETE_OWN_ACCOUNT_F); |
49 | | - showErrorAlert(i18n.t(error.data.details)); |
| 84 | + setError('password', { message: i18n.t(error.data.details), type: 'validate' }); |
| 85 | + AccessibilityInfo.announceForAccessibility(i18n.t(error.data.details)); |
50 | 86 | } |
51 | 87 | return; |
52 | 88 | } |
53 | 89 | dispatch(deleteAccount()); |
54 | 90 | }; |
55 | 91 |
|
56 | 92 | return ( |
57 | | - <ActionSheetContentWithInputAndSubmit |
58 | | - title={i18n.t('Are_you_sure_you_want_to_delete_your_account')} |
59 | | - description={i18n.t('For_your_security_you_must_enter_your_current_password_to_continue')} |
60 | | - onCancel={hideActionSheet} |
61 | | - onSubmit={password => handleDeleteAccount(password as string)} |
62 | | - placeholder={i18n.t('Password')} |
63 | | - autoComplete='password' |
64 | | - testID='profile-view-delete-account-sheet' |
65 | | - iconName='warning' |
66 | | - confirmTitle={i18n.t('Delete_Account')} |
67 | | - confirmBackgroundColor={colors.buttonBackgroundDangerDefault} |
68 | | - /> |
69 | | - ); |
70 | | -} |
| 93 | + <View style={sharedStyles.containerScrollView} testID='action-sheet-content-with-input-and-submit'> |
| 94 | + <View accessible accessibilityLabel={i18n.t('Are_you_sure_you_want_to_delete_your_account')} style={styles.titleContainer}> |
| 95 | + <CustomIcon name={'warning'} size={32} color={colors.buttonBackgroundDangerDefault} /> |
| 96 | + <Text style={[styles.titleContainerText, { color: colors.fontDefault }]}> |
| 97 | + {i18n.t('Are_you_sure_you_want_to_delete_your_account')} |
| 98 | + </Text> |
| 99 | + </View> |
| 100 | + <Text style={[styles.subtitleText, { color: colors.fontTitlesLabels }]}> |
| 101 | + {i18n.t('For_your_security_you_must_enter_your_current_password_to_continue')} |
| 102 | + </Text> |
71 | 103 |
|
72 | | -function ConfirmDeleteAccountActionSheetContent({ changeOwnerRooms = '', removedRooms = '', password = '' }) { |
73 | | - const { hideActionSheet } = useActionSheet(); |
74 | | - const dispatch = useDispatch(); |
75 | | - const { colors } = useTheme(); |
76 | | - const handleDeleteAccount = async () => { |
77 | | - hideActionSheet(); |
78 | | - await deleteOwnAccount(password, true); |
79 | | - dispatch(deleteAccount()); |
80 | | - }; |
| 104 | + <ControlledFormTextInput |
| 105 | + control={control} |
| 106 | + name='password' |
| 107 | + onSubmitEditing={handleDeleteAccount} |
| 108 | + accessibilityLabel={i18n.t('Password')} |
| 109 | + autoComplete='password' |
| 110 | + testID='profile-view-delete-account-sheet-input' |
| 111 | + secureTextEntry |
| 112 | + bottomSheet={isIOS} |
| 113 | + containerStyle={styles.inputContainer} |
| 114 | + error={errors.password?.message} |
| 115 | + /> |
81 | 116 |
|
82 | | - return ( |
83 | | - <ActionSheetContentWithInputAndSubmit |
84 | | - title={i18n.t('Are_you_sure_question_mark')} |
85 | | - iconName='warning' |
86 | | - description={i18n.t('Deleting_a_user_will_delete_all_messages')} |
87 | | - onCancel={hideActionSheet} |
88 | | - onSubmit={handleDeleteAccount} |
89 | | - testID='room-info-edit-view-name' |
90 | | - confirmTitle={i18n.t('Delete_Account_confirm')} |
91 | | - confirmBackgroundColor={colors.buttonBackgroundDangerDefault} |
92 | | - showInput={false} |
93 | | - customText={ |
94 | | - <> |
95 | | - {!!changeOwnerRooms && <AlertText text={changeOwnerRooms} style={{ marginTop: 24 }} />} |
96 | | - {!!removedRooms && <AlertText text={removedRooms} style={{ marginBottom: 36 }} />} |
97 | | - </> |
98 | | - } |
99 | | - /> |
| 117 | + <FooterButtons |
| 118 | + confirmBackgroundColor={colors.buttonBackgroundDangerDefault} |
| 119 | + cancelAction={hideActionSheet} |
| 120 | + confirmAction={handleDeleteAccount} |
| 121 | + cancelTitle={i18n.t('Cancel')} |
| 122 | + confirmTitle={i18n.t('Delete_Account')} |
| 123 | + testID={'profile-view-delete-account-sheet'} |
| 124 | + /> |
| 125 | + </View> |
100 | 126 | ); |
101 | | -} |
| 127 | +}; |
| 128 | + |
| 129 | +export default DeleteAccountActionSheetContent; |
0 commit comments