Skip to content

Commit

Permalink
NotesStore: Storing and removing keys
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhamkmr04 committed May 16, 2023
1 parent 45294d4 commit 9f5c2f5
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 17 deletions.
1 change: 1 addition & 0 deletions App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default class App extends React.PureComponent {
ActivityStore={Stores.activityStore}
PosStore={Stores.posStore}
ModalStore={Stores.modalStore}
NotesStore={Stores.NotesStore}
>
<AppContainer>
<Navigation />
Expand Down
31 changes: 31 additions & 0 deletions stores/NotesStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { action, observable } from 'mobx';
import EncryptedStorage from 'react-native-encrypted-storage';

export default class NotesStore {
@observable public noteKeys: string[] = [];

@action
public storeNoteKeys = async (key: string, notes: string) => {
if (!this.noteKeys.includes(key)) {
if (notes) {
this.noteKeys.push(key);
}
try {
await EncryptedStorage.setItem(
'note-Keys',
JSON.stringify(this.noteKeys)
);
} catch (error) {
console.error('Error saving to encrypted storage');
}
}
};

@action
public removeNoteKeys = (key: string) => {
const index = this.noteKeys.indexOf(key);
if (index !== -1) {
this.noteKeys.splice(index, 1);
}
};
}
3 changes: 3 additions & 0 deletions stores/Stores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import MessageSignStore from './MessageSignStore';
import ActivityStore from './ActivityStore';
import PosStore from './PosStore';
import ModalStore from './ModalStore';
import NotesStore from './NotesStore';

class Stores {
public channelsStore: ChannelsStore;
Expand All @@ -32,6 +33,7 @@ class Stores {
public activityStore: ActivityStore;
public posStore: PosStore;
public modalStore: ModalStore;
public NotesStore: NotesStore;

constructor() {
this.settingsStore = new SettingsStore();
Expand All @@ -55,6 +57,7 @@ class Stores {
this.feeStore = new FeeStore(this.settingsStore, this.nodeInfoStore);
this.utxosStore = new UTXOsStore(this.settingsStore);
this.messageSignStore = new MessageSignStore();
this.NotesStore = new NotesStore();
this.activityStore = new ActivityStore(
this.settingsStore,
this.paymentsStore,
Expand Down
31 changes: 14 additions & 17 deletions views/AddNotes.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react';
import { View, TextInput } from 'react-native';
import EncryptedStorage from 'react-native-encrypted-storage';
import { inject, observer } from 'mobx-react';

import Header from '../components/Header';
import Screen from '../components/Screen';
Expand All @@ -9,17 +10,21 @@ import Button from '../components/Button';
import { localeString } from '../utils/LocaleUtils';
import { themeColor } from '../utils/ThemeUtils';

import NotesStore from '../stores/NotesStore';

interface AddNotesProps {
navigation: any;
NotesStore: NotesStore;
}
interface AddNotesState {
notes?: string;
payment_hash?: string;
txid?: string;
RPreimage?: string;
}
const noteKeys: string[] = [];

@inject('NotesStore')
@observer
export default class AddNotes extends React.Component<
AddNotesProps,
AddNotesState
Expand All @@ -44,7 +49,7 @@ export default class AddNotes extends React.Component<
};
}
async componentDidMount() {
const key: any =
const key: string =
'note-' +
(this.state.txid ||
this.state.payment_hash ||
Expand All @@ -56,7 +61,8 @@ export default class AddNotes extends React.Component<
}

render() {
const { navigation } = this.props;
const { navigation, NotesStore } = this.props;
const { storingNoteKeys, removeNoteKeys } = NotesStore;
const { payment_hash, txid, RPreimage } = this.state;
const { notes } = this.state;
return (
Expand All @@ -80,13 +86,10 @@ export default class AddNotes extends React.Component<
onChangeText={(text: string) => {
this.setState({ notes: text });
if (!text) {
const key: any =
const key: string =
'note-' +
(payment_hash || txid || RPreimage);
const index = noteKeys.indexOf(key);
if (index !== -1) {
noteKeys.splice(index, 1);
}
removeNoteKeys(key);
}
}}
multiline
Expand All @@ -102,17 +105,11 @@ export default class AddNotes extends React.Component<
: localeString('views.SendingLightning.AddANote')
}
onPress={async () => {
navigation.goBack();
const key: any =
const key: string =
'note-' + (payment_hash || txid || RPreimage);
await EncryptedStorage.setItem(key, notes);
if (!noteKeys.includes(key)) {
noteKeys.push(key);
await EncryptedStorage.setItem(
'note-Keys',
JSON.stringify(noteKeys)
);
}
await storingNoteKeys(key, notes);
navigation.goBack();
}}
containerStyle={{ position: 'absolute', bottom: 40 }}
buttonStyle={{ padding: 15 }}
Expand Down

0 comments on commit 9f5c2f5

Please sign in to comment.