Rocket.Chat.ReactNative/app/views/E2EEncryptionSecurityView/ChangePassword.tsx

104 lines
3.1 KiB
TypeScript
Raw Normal View History

import React, { useRef, useState } from 'react';
import { StyleSheet, Text, TextInput as RNTextInput } from 'react-native';
import { useTheme } from '../../theme';
import * as List from '../../containers/List';
import I18n from '../../i18n';
import log, { events, logEvent } from '../../lib/methods/helpers/log';
import { FormTextInput } from '../../containers/TextInput';
import Button from '../../containers/Button';
import { Encryption } from '../../lib/encryption';
import { showConfirmationAlert, showErrorAlert } from '../../lib/methods/helpers/info';
import EventEmitter from '../../lib/methods/helpers/events';
import { LISTENER } from '../../containers/Toast';
import { useDebounce } from '../../lib/methods/helpers';
import sharedStyles from '../Styles';
import { useAppSelector } from '../../lib/hooks';
const styles = StyleSheet.create({
title: {
fontSize: 16,
...sharedStyles.textMedium
},
description: {
fontSize: 14,
paddingVertical: 12,
...sharedStyles.textRegular
},
changePasswordButton: {
marginBottom: 4
},
separator: {
marginBottom: 16
}
});
const ChangePassword = () => {
const [newPassword, setNewPassword] = useState('');
const { colors } = useTheme();
const { encryptionEnabled, server } = useAppSelector(state => ({
encryptionEnabled: state.encryption.enabled,
server: state.server.server
}));
const newPasswordInputRef = useRef<RNTextInput | null>(null);
const onChangePasswordText = useDebounce((text: string) => setNewPassword(text), 300);
const changePassword = () => {
if (!newPassword.trim()) {
return;
}
showConfirmationAlert({
title: I18n.t('Are_you_sure_question_mark'),
message: I18n.t('E2E_encryption_change_password_message'),
confirmationText: I18n.t('E2E_encryption_change_password_confirmation'),
onPress: async () => {
logEvent(events.E2E_SEC_CHANGE_PASSWORD);
try {
await Encryption.changePassword(server, newPassword);
EventEmitter.emit(LISTENER, { message: I18n.t('E2E_encryption_change_password_success') });
newPasswordInputRef?.current?.clear();
newPasswordInputRef?.current?.blur();
} catch (e) {
log(e);
showErrorAlert(I18n.t('E2E_encryption_change_password_error'));
}
}
});
};
if (!encryptionEnabled) {
return null;
}
return (
<>
<List.Section>
feat: mobile color normalization (#5616) * chore: remove auxiliaryText color * chore: remove titleText * chore: password colors change * chore: use fontDefault on ActionSheet item * wip: type * chore: set custom icon default color * remove tintActive color * only set color when checked * remove icon color * remove tintActive * chore: remove STATUS_COLORS * chore: remove mentions colors * chore: remove switch color * chore: background color * chore: auxiliaryBackground * chore: one local colors * wip: some colors * wip: colors * wip: colors * wip: end colors * test: update * chore: fix some colors * chore: fix lint * chore: back to text props * chore: fix ts errors * revert * chore: fix lint * test: update snapshot * update storybook * cocoapods * fix app theme color * remove unused color * fix login service button color * remove unused color * unused backgroundColor * fix background color * fix transparent color * fix background color * wip: key * fix color * chore: revert to 1 tick * test: update * chore: use same color as front end * test: update * fix radius * fix background color * fix wrong color * change some colors * chore: update stories * chore: fix button style * chore: fix item color * lint * update snapshot * link * remove background color * change send to channel color * call icons * video conf colors * fix app default color * bottom sheet * remove background * two factor color * update tests * feat: add force-logout stream listener * remove icon colors * improve badge color * update tests * fix header colors * fix collapsible icon color * imagePreview color * wip * update test * lint --------- Co-authored-by: Diego Mello <diegolmello@gmail.com>
2024-04-18 10:19:54 +00:00
<Text style={[styles.title, { color: colors.fontTitlesLabels }]}>{I18n.t('E2E_encryption_change_password_title')}</Text>
<Text style={[styles.description, { color: colors.fontDefault }]}>
{I18n.t('E2E_encryption_change_password_description')}
</Text>
<FormTextInput
inputRef={newPasswordInputRef}
placeholder={I18n.t('New_Password')}
returnKeyType='send'
secureTextEntry
onSubmitEditing={changePassword}
testID='e2e-encryption-security-view-password'
onChangeText={onChangePasswordText}
/>
<Button
onPress={changePassword}
title={I18n.t('Save_Changes')}
disabled={!newPassword.trim()}
style={styles.changePasswordButton}
testID='e2e-encryption-security-view-change-password'
/>
</List.Section>
<List.Separator style={styles.separator} />
</>
);
};
export default ChangePassword;