Newer
Older
import React, { Component } from 'react'
import RNFS from 'react-native-fs'
import Button from '../../common/button'
import ConfirmWithPassword from '../common/confirm-with-password'
import alertError from '../common/alert-error'
import { clearDb, isDbEmpty } from '../../../db'
import { showToast } from '../../helpers/general'
import { hasEncryptionObservable } from '../../../local-storage'
import settings from '../../../i18n/en/settings'
import { shared as sharedLabels } from '../../../i18n/en/labels'
import { EXPORT_FILE_NAME } from './constants'
mashazyu
committed
const exportedFilePath = `${RNFS.DocumentDirectoryPath}/${EXPORT_FILE_NAME}`
export default class DeleteData extends Component {
constructor() {
super()
this.state = {
isPasswordSet: hasEncryptionObservable.value,
isConfirmingWithPassword: false
onAlertConfirmation = () => {
if (this.state.isPasswordSet) {
this.setState({ isConfirmingWithPassword: true })
} else {
this.deleteAppData()
mashazyu
committed
alertBeforeDeletion = async () => {
const { question, message, confirmation, errors } = settings.deleteSegment
if (isDbEmpty() && !await RNFS.exists(exportedFilePath)) {
alertError(errors.noData)
} else {
Alert.alert(
question,
message,
[{
text: confirmation,
onPress: this.onAlertConfirmation
}, {
text: sharedLabels.cancel,
style: 'cancel',
onPress: this.cancelConfirmationWithPassword
}]
)
}
mashazyu
committed
if (await RNFS.exists(exportedFilePath)) {
await RNFS.unlink(exportedFilePath)
const { errors, success } = settings.deleteSegment
mashazyu
committed
mashazyu
committed
if (!isDbEmpty()) {
clearDb()
}
mashazyu
committed
alertError(errors.couldNotDeleteFile)
mashazyu
committed
this.cancelConfirmationWithPassword()
cancelConfirmationWithPassword = () => {
this.setState({ isConfirmingWithPassword: false })
const { isConfirmingWithPassword } = this.state
<ConfirmWithPassword
onSuccess={this.deleteAppData}
onCancel={this.cancelConfirmationWithPassword}
/>
<Button isCTA onPress={this.alertBeforeDeletion}>
{settings.deleteSegment.title}
}
DeleteData.propTypes = {
isDeletingData: PropTypes.bool,
onStartDeletion: PropTypes.func.isRequired