fix recovery key dialog

This commit is contained in:
ammarahm-ed
2020-10-28 11:26:58 +05:00
parent 7dea7a9c50
commit aa17b2ba71

View File

@@ -4,15 +4,20 @@ import QRCode from 'react-native-qrcode-svg';
import Icon from 'react-native-vector-icons/MaterialCommunityIcons'; import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
import RNFetchBlob from 'rn-fetch-blob'; import RNFetchBlob from 'rn-fetch-blob';
import {LOGO_BASE64} from '../../assets/images/assets'; import {LOGO_BASE64} from '../../assets/images/assets';
import {eSubscribeEvent, eUnSubscribeEvent, ToastEvent} from '../../services/EventManager'; import {
eSubscribeEvent,
eUnSubscribeEvent,
ToastEvent,
} from '../../services/EventManager';
import {eOpenRecoveryKeyDialog} from '../../utils/Events'; import {eOpenRecoveryKeyDialog} from '../../utils/Events';
import {dWidth} from '../../utils'; import {dWidth} from '../../utils';
import ActionSheet from '../ActionSheet'; import ActionSheet from '../ActionSheet';
import {Button} from '../Button'; import {Button} from '../Button';
import Seperator from '../Seperator'; import Seperator from '../Seperator';
import {Toast} from '../Toast'; import {Toast} from '../Toast';
import {SIZE, WEIGHT} from "../../utils/SizeUtils"; import {SIZE, WEIGHT} from '../../utils/SizeUtils';
import {db} from "../../utils/DB"; import {db} from '../../utils/DB';
import Storage from '../../utils/storage';
class RecoveryKeyDialog extends React.Component { class RecoveryKeyDialog extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
@@ -21,6 +26,7 @@ class RecoveryKeyDialog extends React.Component {
}; };
this.actionSheetRef = createRef(); this.actionSheetRef = createRef();
this.svg = createRef(); this.svg = createRef();
this.user;
} }
open = () => { open = () => {
@@ -32,45 +38,32 @@ class RecoveryKeyDialog extends React.Component {
}; };
async componentDidMount() { async componentDidMount() {
eSubscribeEvent(eOpenRecoveryKeyDialog, this.open); eSubscribeEvent(eOpenRecoveryKeyDialog, this.open);
let k = await db.user.key();
if (k) {
this.setState({
key: k.key,
});
}
} }
async componentWillUnmount() { async componentWillUnmount() {
eUnSubscribeEvent(eOpenRecoveryKeyDialog, this.open); eUnSubscribeEvent(eOpenRecoveryKeyDialog, this.open);
} }
componentDidUpdate() {} saveQRCODE = async () => {
if ((await Storage.requestPermission()) === false) {
ToastEvent.show('Storage access not granted!', 'error', 'local');
return;
}
saveQRCODE = () => { this.svg.current?.toDataURL(async (data) => {
this.svg.current?.toDataURL((data) => { let path = await Storage.checkAndCreateDir('/');
RNFetchBlob.fs let fileName = 'nn_' + this.user.username + '_recovery_key_qrcode.png';
.writeFile( RNFetchBlob.fs.writeFile(path + fileName, data, 'base64').then((res) => {
RNFetchBlob.fs.dirs.SDCardDir +
'/Notesnook/nn_recovery_key_qrcode.png',
data,
'base64',
)
.then((res) => {
RNFetchBlob.fs RNFetchBlob.fs
.scanFile([ .scanFile([
{ {
path: path: path + fileName,
RNFetchBlob.fs.dirs.SDCardDir +
'/Notesnook/nn_recovery_key_qrcode.png',
mime: 'image/png', mime: 'image/png',
}, },
]) ])
.then((r) => { .then((r) => {
ToastEvent.show( ToastEvent.show(
'Recovery key saved to Gallery as ' + 'Recovery key saved to Gallery as ' + path + fileName,
RNFetchBlob.fs.dirs.SDCardDir +
'/Notesnook/nn_recovery_key_qrcode.png',
'success', 'success',
'local', 'local',
); );
@@ -79,6 +72,37 @@ class RecoveryKeyDialog extends React.Component {
}); });
}; };
saveToTextFile = async () => {
if ((await Storage.requestPermission()) === false) {
ToastEvent.show('Storage access not granted!', 'error', 'local');
return;
}
let path = await Storage.checkAndCreateDir('/');
let fileName = 'nn_' + this.user.username + '_recovery_key.txt';
RNFetchBlob.fs
.writeFile(path + fileName, this.state.key, 'utf8')
.then((r) => {
ToastEvent.show(
'Recovery key saved as ' + path + fileName,
'success',
'local',
);
})
.catch((e) => {});
};
onOpen = async () => {
let k = await db.user.key();
this.user = await db.user.get();
console.log(k);
if (k) {
this.setState({
key: k.key,
});
}
};
render() { render() {
const {colors} = this.props; const {colors} = this.props;
return ( return (
@@ -89,6 +113,7 @@ class RecoveryKeyDialog extends React.Component {
alignSelf: 'center', alignSelf: 'center',
borderRadius: 10, borderRadius: 10,
}} }}
onOpen={this.onOpen}
ref={this.actionSheetRef} ref={this.actionSheetRef}
initialOffsetFromBottom={1}> initialOffsetFromBottom={1}>
<View <View
@@ -169,14 +194,7 @@ class RecoveryKeyDialog extends React.Component {
/> />
<Seperator /> <Seperator />
<Button <Button
onPress={() => { onPress={this.saveToTextFile}
RNFetchBlob.fs.writeFile(
RNFetchBlob.fs.dirs.SDCardDir +
'/Notesnook/nn_recovery_key.txt',
this.state.key,
'utf8',
);
}}
title="Save as Text File" title="Save as Text File"
width="100%" width="100%"
height={40} height={40}
@@ -185,7 +203,7 @@ class RecoveryKeyDialog extends React.Component {
<Button <Button
onPress={() => { onPress={() => {
Clipboard.setString(this.state.key); Clipboard.setString(this.state.key);
ToastEvent.show('Recovery key copied'); ToastEvent.show('Copied!', 'success', 'local');
}} }}
title="Copy Key" title="Copy Key"
width="100%" width="100%"