import { sha256 } from 'js-sha256';
import React, { useState } from 'react';
import { Keyboard, Text, View } from 'react-native';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import { useDispatch } from 'react-redux';
import { deleteAccount } from '../../../../actions/login';
import { useActionSheet } from '../../../../containers/ActionSheet';
import FooterButtons from '../../../../containers/ActionSheet/FooterButtons';
import { CustomIcon } from '../../../../containers/CustomIcon';
import { FormTextInput } from '../../../../containers/TextInput/FormTextInput';
import i18n from '../../../../i18n';
import { showErrorAlert } from '../../../../lib/methods/helpers';
import { events, logEvent } from '../../../../lib/methods/helpers/log';
import { deleteOwnAccount } from '../../../../lib/services/restApi';
import { useTheme } from '../../../../theme';
import { getTranslations } from './getTranslations';
import styles from './styles';
const AlertHeader = ({ title = '', subTitle = '' }) => {
const { colors } = useTheme();
return (
<>
{title}
{subTitle}
>
);
};
export function DeleteAccountActionSheetContent(): React.ReactElement {
const [password, setPassword] = useState('');
const { hideActionSheet, showActionSheet } = useActionSheet();
const dispatch = useDispatch();
const insets = useSafeAreaInsets();
const handleDeleteAccount = async () => {
Keyboard.dismiss();
try {
await deleteOwnAccount(sha256(password));
hideActionSheet();
} catch (error: any) {
hideActionSheet();
if (error.data.errorType === 'user-last-owner') {
const { shouldChangeOwner, shouldBeRemoved } = error.data.details;
const { changeOwnerRooms, removedRooms } = getTranslations({ shouldChangeOwner, shouldBeRemoved });
setTimeout(() => {
showActionSheet({
children: (
),
headerHeight: 225 + insets.bottom
});
}, 250); // timeout for hide effect
} else if (error.data.errorType === 'error-invalid-password') {
logEvent(events.DELETE_OWN_ACCOUNT_F);
showErrorAlert(i18n.t('error-invalid-password'));
} else {
logEvent(events.DELETE_OWN_ACCOUNT_F);
showErrorAlert(i18n.t(error.data.details));
}
return;
}
dispatch(deleteAccount());
};
return (
setPassword(value)}
onSubmitEditing={handleDeleteAccount}
testID='room-info-edit-view-name'
secureTextEntry
inputStyle={{ borderWidth: 2 }}
bottomSheet
/>
);
}
function ConfirmDeleteAccountActionSheetContent({ changeOwnerRooms = '', removedRooms = '', password = '' }) {
const { colors } = useTheme();
const { hideActionSheet } = useActionSheet();
const dispatch = useDispatch();
const handleDeleteAccount = async () => {
hideActionSheet();
await deleteOwnAccount(password, true);
dispatch(deleteAccount());
};
return (
{!!changeOwnerRooms && (
{changeOwnerRooms}
)}
{!!removedRooms && {removedRooms}}
);
}