Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

add functions to listen to note changes

ammarahm-edab8422fc167b
diff --git a/apps/mobile/src/components/SimpleList/NoteItemWrapper.js b/apps/mobile/src/components/SimpleList/NoteItemWrapper.js
index cbe81a83f..4cfe060e0 100644
--- a/apps/mobile/src/components/SimpleList/NoteItemWrapper.js
+++ b/apps/mobile/src/components/SimpleList/NoteItemWrapper.js
@@ -1,78 +1,103 @@
-import React, {useMemo} from 'react';
+import React, {useEffect, useMemo, useState} from 'react';
import NoteItem from '../../components/NoteItem';
import SelectionWrapper from '../../components/SelectionWrapper';
import {useTracked} from '../../provider';
import {ACTIONS} from '../../provider/actions';
-import {eSendEvent, openVault} from '../../services/eventManager';
-import {eShowMergeDialog, eOnLoadNote} from '../../services/events';
+import {eSendEvent, eSubscribeEvent, eUnSubscribeEvent, openVault} from '../../services/eventManager';
+import {eShowMergeDialog, eOnLoadNote, eOnNoteEdited} from '../../services/events';
import {simpleDialogEvent} from '../DialogManager/recievers';
import {TEMPLATE_TRASH} from '../DialogManager/templates';
import {openEditorAnimation} from '../../utils/animations';
-import {DDS} from '../../utils/utils';
+import {db, DDS} from '../../utils/utils';
+
+
export const NoteItemWrapper = ({
- item,
- index,
- isTrash = false,
- pinned = false,
-}) => {
- const [state, dispatch] = useTracked();
- const {colors, currentEditingNote, selectionMode} = state;
+ item,
+ index,
+ isTrash = false,
+ pinned = false,
+ }) => {
+ const [state, dispatch] = useTracked();
+ const {colors, selectionMode} = state;
+ const [note, setNote] = useState(item);
+ const [editing, setEditing] = useState(false);
+ const onNoteChange = (data) => {
+ if (data.id !== note.id) {
+ return;
+ }
+ if (editing !== true) {
+ setEditing(true);
+ }
- const style = useMemo(() => {
- return {width: selectionMode ? '90%' : '100%', marginHorizontal: 0};
- }, [selectionMode]);
+ if (data.closed) {
+ setEditing(false);
+ }
- const onLongPress = () => {
- if (!selectionMode) {
- dispatch({type: ACTIONS.SELECTION_MODE, enabled: true});
+ setNote(db.notes.note(data.id).data)
}
- dispatch({type: ACTIONS.SELECTED_ITEMS, item: item});
- };
- const onPress = async () => {
- if (item.conflicted) {
- eSendEvent(eShowMergeDialog, item);
+ useEffect(() => {
+ eSubscribeEvent(eOnNoteEdited + note.id, onNoteChange);
+ return () => {
+ eUnSubscribeEvent(eOnNoteEdited + note.id, onNoteChange);
+ }
+ }, [editing]);
+
- return;
- }
- if (selectionMode) {
- onLongPress();
- return;
- } else if (item.locked) {
- openVault(item, true, true, false, true, false);
- return;
- }
- if (isTrash) {
- simpleDialogEvent(TEMPLATE_TRASH(item.type));
- } else {
- eSendEvent(eOnLoadNote, item);
- }
- if (DDS.isPhone || DDS.isSmallTab) {
- openEditorAnimation();
- }
- };
- return (
- <SelectionWrapper
- index={index}
- pinned={pinned}
- onLongPress={onLongPress}
- onPress={onPress}
- currentEditingNote={
- currentEditingNote === item.id ? currentEditingNote : null
- }
- item={item}>
- <NoteItem
- colors={colors}
- customStyle={style}
- currentEditingNote={
- currentEditingNote === item.id ? currentEditingNote : null
+ const style = useMemo(() => {
+ return {width: selectionMode ? '90%' : '100%', marginHorizontal: 0};
+ }, [selectionMode]);
+
+
+ const onLongPress = () => {
+ if (!selectionMode) {
+ dispatch({type: ACTIONS.SELECTION_MODE, enabled: true});
+ }
+ dispatch({type: ACTIONS.SELECTED_ITEMS, item: item});
+ };
+
+ const onPress = async () => {
+ if (item.conflicted) {
+ eSendEvent(eShowMergeDialog, item);
+
+ return;
}
- selectionMode={selectionMode}
- item={item}
- isTrash={isTrash}
- />
- </SelectionWrapper>
- );
+ if (selectionMode) {
+ onLongPress();
+ return;
+ } else if (item.locked) {
+ openVault(item, true, true, false, true, false);
+ return;
+ }
+ if (isTrash) {
+ simpleDialogEvent(TEMPLATE_TRASH(item.type));
+ } else {
+ eSendEvent(eOnLoadNote, item);
+ }
+ if (DDS.isPhone || DDS.isSmallTab) {
+ openEditorAnimation();
+ }
+ };
+
+
+ return (
+ <SelectionWrapper
+ index={index}
+ pinned={pinned}
+ onLongPress={onLongPress}
+ onPress={onPress}
+ currentEditingNote={editing}
+ item={note}>
+ <NoteItem
+ colors={colors}
+ customStyle={style}
+ currentEditingNote={editing}
+ selectionMode={selectionMode}
+ item={note}
+ isTrash={isTrash}
+ />
+ </SelectionWrapper>
+ );
};