import { sha256 } from 'js-sha256';
import React from 'react';
import { Keyboard, Text } 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 ActionSheetContentWithInputAndSubmit from '../../../../containers/ActionSheet/ActionSheetContentWithInputAndSubmit';
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 sharedStyles from '../../../Styles';
export function DeleteAccountActionSheetContent(): React.ReactElement {
const { hideActionSheet, showActionSheet } = useActionSheet();
const dispatch = useDispatch();
const insets = useSafeAreaInsets();
const { colors } = useTheme();
const handleDeleteAccount = async (password: string) => {
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 (
handleDeleteAccount(password)}
placeholder={i18n.t('Password')}
testID='profile-view-delete-account-sheet'
iconName='warning'
confirmTitle={i18n.t('Delete_Account')}
confirmBackgroundColor={colors.dangerColor}
/>
);
}
const AlertText = ({ text = '' }) => {
const { colors } = useTheme();
return {text};
};
function ConfirmDeleteAccountActionSheetContent({ changeOwnerRooms = '', removedRooms = '', password = '' }) {
const { hideActionSheet } = useActionSheet();
const dispatch = useDispatch();
const { colors } = useTheme();
const handleDeleteAccount = async () => {
hideActionSheet();
await deleteOwnAccount(password, true);
dispatch(deleteAccount());
};
return (
{!!changeOwnerRooms && }
{!!removedRooms && }
>
}
/>
);
}