-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathBasicFunctionalityModal.tsx
More file actions
214 lines (201 loc) · 7.21 KB
/
BasicFunctionalityModal.tsx
File metadata and controls
214 lines (201 loc) · 7.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Third party dependencies.
import React, { useCallback, useRef } from 'react';
import { View } from 'react-native';
// External dependencies.
import BottomSheet, {
BottomSheetRef,
} from '../../../../component-library/components/BottomSheets/BottomSheet';
import { strings } from '../../../../../locales/i18n';
import Text, {
TextVariant,
} from '../../../../component-library/components/Texts/Text';
import { useTheme } from '../../../../util/theme';
import Button, {
ButtonSize,
ButtonVariants,
} from '../../../../component-library/components/Buttons/Button';
import Checkbox from '../../../../component-library/components/Checkbox/Checkbox';
import { useSelector } from 'react-redux';
import { toggleBasicFunctionality } from '../../../../actions/settings';
import createStyles from '../../Notification/Modal/styles';
import { RootState } from 'app/reducers';
import Icon, {
IconColor,
IconName,
IconSize,
} from '../../../../component-library/components/Icons/Icon';
import Routes from '../../../../constants/navigation/Routes';
import NotificationsService from '../../../../util/notifications/services/NotificationService';
import { MetaMetricsEvents } from '../../../../core/Analytics';
import { useEnableNotifications } from '../../../../util/notifications/hooks/useNotifications';
import { useAnalytics } from '../../../hooks/useAnalytics/useAnalytics';
import { selectIsMetamaskNotificationsEnabled } from '../../../../selectors/notifications';
import { selectIsBackupAndSyncEnabled } from '../../../../selectors/identity';
import useThunkDispatch from '../../../hooks/useThunkDispatch';
interface Props {
route: {
params: {
caller: string;
};
};
}
const BasicFunctionalityModal = ({ route }: Props) => {
const { trackEvent, createEventBuilder } = useAnalytics();
const { colors } = useTheme();
const styles = createStyles(colors);
const bottomSheetRef = useRef<BottomSheetRef>(null);
const [isChecked, setIsChecked] = React.useState(false);
const dispatch = useThunkDispatch();
const isEnabled = useSelector(
(state: RootState) => state?.settings?.basicFunctionalityEnabled,
);
const isBackupAndSyncEnabled = useSelector(selectIsBackupAndSyncEnabled);
const isNotificationsFeatureEnabled = useSelector(
selectIsMetamaskNotificationsEnabled,
);
const { enableNotifications } = useEnableNotifications();
const enableNotificationsFromModal = useCallback(async () => {
const { permission } = await NotificationsService.getAllPermissions(false);
if (permission !== 'authorized') {
return;
}
enableNotifications();
}, [enableNotifications]);
const closeBottomSheet = async () => {
bottomSheetRef.current?.onCloseBottomSheet(async () => {
await dispatch(toggleBasicFunctionality(!isEnabled));
trackEvent(
createEventBuilder(
!isEnabled
? MetaMetricsEvents.BASIC_FUNCTIONALITY_ENABLED
: MetaMetricsEvents.BASIC_FUNCTIONALITY_DISABLED,
).build(),
);
trackEvent(
createEventBuilder(MetaMetricsEvents.SETTINGS_UPDATED)
.addProperties({
settings_group: 'security_privacy',
settings_type: 'basic_functionality',
old_value: isEnabled,
new_value: !isEnabled,
was_notifications_on: isEnabled
? isNotificationsFeatureEnabled
: false,
was_profile_syncing_on: isEnabled ? isBackupAndSyncEnabled : false,
})
.build(),
);
});
if (
route.params.caller === Routes.SETTINGS.NOTIFICATIONS ||
route.params.caller === Routes.NOTIFICATIONS.OPT_IN
) {
await enableNotificationsFromModal();
}
};
const handleSwitchToggle = () => {
closeBottomSheet();
};
const handleCancel = () => {
bottomSheetRef.current?.onCloseBottomSheet();
};
const renderTurnOffContent = () => (
<View style={styles.container}>
<Icon
name={IconName.Danger}
color={IconColor.Error}
size={IconSize.Xl}
style={styles.icon}
/>
<Text variant={TextVariant.HeadingMD} style={styles.title}>
{strings('default_settings.sheet.title_off')}
</Text>
<Text variant={TextVariant.BodyMD} style={styles.description}>
{strings('default_settings.sheet.description_off')}
</Text>
<Text variant={TextVariant.BodyMD} style={styles.description}>
{strings('default_settings.sheet.description_off2')}{' '}
<Text variant={TextVariant.BodyMDBold} style={styles.description}>
{strings(
'default_settings.sheet.description_off2_related_features1',
)}{' '}
</Text>
<Text variant={TextVariant.BodyMD} style={styles.description}>
{strings(
'default_settings.sheet.description_off2_related_features1_and',
)}{' '}
</Text>
<Text variant={TextVariant.BodyMDBold} style={styles.description}>
{strings('default_settings.sheet.description_off2_related_features2')}
</Text>
</Text>
<View style={styles.bottom}>
<Checkbox
label={strings('default_settings.sheet.checkbox_label')}
isChecked={isChecked}
onPress={() => setIsChecked(!isChecked)}
/>
<View style={styles.buttonsContainer}>
<Button
variant={ButtonVariants.Secondary}
size={ButtonSize.Lg}
style={styles.button}
accessibilityRole={'button'}
accessible
label={strings('default_settings.sheet.buttons.cancel')}
onPress={handleCancel}
/>
<View style={styles.spacer} />
<Button
variant={ButtonVariants.Primary}
isDisabled={!isChecked}
isDanger
size={ButtonSize.Lg}
style={styles.button}
accessibilityRole={'button'}
accessible
label={strings('default_settings.sheet.buttons.turn_off')}
onPress={handleSwitchToggle}
/>
</View>
</View>
</View>
);
const renderTurnOnContent = () => (
<View style={styles.container}>
<Text variant={TextVariant.HeadingMD} style={styles.title}>
{strings('default_settings.sheet.title_on')}
</Text>
<Text variant={TextVariant.BodyMD} style={styles.subtitle}>
{strings('default_settings.sheet.description_on')}
</Text>
<View style={styles.buttonsContainer}>
<Button
variant={ButtonVariants.Secondary}
size={ButtonSize.Lg}
style={styles.button}
accessibilityRole={'button'}
accessible
label={strings('default_settings.sheet.buttons.cancel')}
onPress={handleCancel}
/>
<View style={styles.spacer} />
<Button
variant={ButtonVariants.Primary}
size={ButtonSize.Lg}
style={styles.button}
accessibilityRole={'button'}
accessible
label={strings('default_settings.sheet.buttons.turn_on')}
onPress={handleSwitchToggle}
/>
</View>
</View>
);
return (
<BottomSheet ref={bottomSheetRef}>
{isEnabled ? renderTurnOffContent() : renderTurnOnContent()}
</BottomSheet>
);
};
export default BasicFunctionalityModal;