-
-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathNotesStore.ts
39 lines (34 loc) · 1.08 KB
/
NotesStore.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { action, observable } from 'mobx';
import EncryptedStorage from 'react-native-encrypted-storage';
const NOTES_KEY = 'note-Keys';
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);
}
await this.writeNoteKeysToLocalStorage();
}
};
@action
public removeNoteKeys = async (key: string) => {
const index = this.noteKeys.indexOf(key);
if (index !== -1) {
this.noteKeys.splice(index, 1);
// write updated keys to storage
await this.writeNoteKeysToLocalStorage();
}
};
writeNoteKeysToLocalStorage = async () => {
try {
await EncryptedStorage.setItem(
NOTES_KEY,
JSON.stringify(this.noteKeys)
);
} catch (error) {
console.error('Error saving to encrypted storage');
}
};
}