From d0a75966bae5d3d5bb78ec9c97989aae342008ea Mon Sep 17 00:00:00 2001 From: ammarahm-ed Date: Wed, 23 Mar 2022 12:23:51 +0500 Subject: [PATCH] fix: search items list not updating --- .../src/components/dialogs/vault/index.js | 2 ++ apps/mobile/src/components/list/empty.js | 2 +- apps/mobile/src/components/properties/index.js | 12 +++++++++++- .../src/components/sheets/addto/index.js | 2 ++ .../components/sheets/movenotes/movenote.tsx | 2 ++ .../src/components/sheets/publishnote/index.js | 1 + apps/mobile/src/screens/favorites/index.js | 4 +++- apps/mobile/src/screens/home/index.js | 15 ++++++--------- apps/mobile/src/screens/notebook/index.js | 6 +++++- apps/mobile/src/screens/notebooks/index.js | 4 +++- apps/mobile/src/screens/notes/index.js | 12 ++++++++---- apps/mobile/src/screens/tags/index.js | 4 +++- apps/mobile/src/screens/trash/index.js | 3 ++- apps/mobile/src/services/search.js | 18 ++++++++++++++---- apps/mobile/src/utils/functions.js | 3 +++ apps/mobile/src/utils/hooks/useActions.js | 1 + apps/mobile/src/utils/hooks/useAppEvents.js | 1 + 17 files changed, 68 insertions(+), 24 deletions(-) diff --git a/apps/mobile/src/components/dialogs/vault/index.js b/apps/mobile/src/components/dialogs/vault/index.js index 6e00972e4..53a5dc0ec 100644 --- a/apps/mobile/src/components/dialogs/vault/index.js +++ b/apps/mobile/src/components/dialogs/vault/index.js @@ -33,6 +33,7 @@ import Input from '../../ui/input'; import Seperator from '../../ui/seperator'; import { Toast } from '../../toast'; import Paragraph from '../../ui/typography/paragraph'; +import SearchService from '../../../services/search'; let Keychain; const passInputRef = createRef(); @@ -199,6 +200,7 @@ export class VaultDialog extends Component { ]); this.password = null; this.confirmPassword = null; + SearchService.updateAndSearch(); this.setState({ visible: false, note: {}, diff --git a/apps/mobile/src/components/list/empty.js b/apps/mobile/src/components/list/empty.js index 558f276ce..052a617dd 100644 --- a/apps/mobile/src/components/list/empty.js +++ b/apps/mobile/src/components/list/empty.js @@ -40,7 +40,7 @@ export const Empty = React.memo( <> {}, item, buttons = [], getRef }) => { const colors = useThemeStore(state => state.colors); @@ -102,7 +103,16 @@ export const Properties = ({ close = () => {}, item, buttons = [], getRef }) => {item.type === 'note' ? : null} - + { + close(); + setTimeout(() => { + SearchService.updateAndSearch(); + }, 1000); + }} + /> diff --git a/apps/mobile/src/components/sheets/addto/index.js b/apps/mobile/src/components/sheets/addto/index.js index c0e8047f7..c518a6b5c 100644 --- a/apps/mobile/src/components/sheets/addto/index.js +++ b/apps/mobile/src/components/sheets/addto/index.js @@ -4,6 +4,7 @@ import { FlatList } from 'react-native-gesture-handler'; import { notesnook } from '../../../../e2e/test.ids'; import { eSubscribeEvent, eUnSubscribeEvent, ToastEvent } from '../../../services/event-manager'; import Navigation from '../../../services/navigation'; +import SearchService from '../../../services/search'; import { useNotebookStore, useSelectionStore } from '../../../stores/stores'; import { useThemeStore } from '../../../stores/theme'; import { getTotalNotes } from '../../../utils'; @@ -141,6 +142,7 @@ const MoveNoteComponent = ({ note }) => { ]); setNotebooks(); updateNoteExists(); + SearchService.updateAndSearch(); }; useEffect(() => { diff --git a/apps/mobile/src/components/sheets/movenotes/movenote.tsx b/apps/mobile/src/components/sheets/movenotes/movenote.tsx index 99c66660e..b9aefaa8d 100644 --- a/apps/mobile/src/components/sheets/movenotes/movenote.tsx +++ b/apps/mobile/src/components/sheets/movenotes/movenote.tsx @@ -17,6 +17,7 @@ import { PressableButton } from '../../ui/pressable'; import Seperator from '../../ui/seperator'; import Heading from '../../ui/typography/heading'; import Paragraph from '../../ui/typography/paragraph'; +import SearchService from '../../../services/search'; export const MoveNotes = ({ notebook, @@ -274,6 +275,7 @@ export const MoveNotes = ({ Navigation.routeNames.Notebooks, Navigation.routeNames.Notebook ]); + SearchService.updateAndSearch(); eSendEvent(eCloseProgressDialog); }} title="Move selected notes" diff --git a/apps/mobile/src/components/sheets/publishnote/index.js b/apps/mobile/src/components/sheets/publishnote/index.js index c9ebdb311..657ae478c 100644 --- a/apps/mobile/src/components/sheets/publishnote/index.js +++ b/apps/mobile/src/components/sheets/publishnote/index.js @@ -18,6 +18,7 @@ import Seperator from '../../ui/seperator'; import SheetWrapper from '../../ui/sheet'; import Heading from '../../ui/typography/heading'; import Paragraph from '../../ui/typography/paragraph'; +import SearchService from '../../../services/search'; let passwordValue = null; const PublishNoteSheet = () => { diff --git a/apps/mobile/src/screens/favorites/index.js b/apps/mobile/src/screens/favorites/index.js index ef415fe6d..7964d12a5 100644 --- a/apps/mobile/src/screens/favorites/index.js +++ b/apps/mobile/src/screens/favorites/index.js @@ -8,6 +8,7 @@ import { useFavoriteStore, useNoteStore } from '../../stores/stores'; import Navigation from '../../services/navigation'; import SearchService from '../../services/search'; import { InteractionManager } from '../../utils'; +import { db } from '../../utils/database'; export const Favorites = ({ navigation }) => { const favorites = useFavoriteStore(state => state.favorites); @@ -74,7 +75,8 @@ export const Favorites = ({ navigation }) => { placeholder: 'Search in favorites', data: favorites, type: 'notes', - title: 'Favorites' + title: 'Favorites', + get: () => db.notes.favorites }); }; diff --git a/apps/mobile/src/screens/home/index.js b/apps/mobile/src/screens/home/index.js index b804ec776..c52b3fd83 100755 --- a/apps/mobile/src/screens/home/index.js +++ b/apps/mobile/src/screens/home/index.js @@ -1,23 +1,19 @@ import React, { useCallback, useEffect } from 'react'; -import { FloatingButton } from '../../components/container/floating-button'; import { ContainerHeader } from '../../components/container/containerheader'; +import { FloatingButton } from '../../components/container/floating-button'; import { Header } from '../../components/header/index'; -import SelectionHeader from '../../components/selection-header'; import List from '../../components/list'; -import { useNoteStore } from '../../stores/stores'; +import SelectionHeader from '../../components/selection-header'; import { DDS } from '../../services/device-detection'; -import { eSendEvent, presentSheet } from '../../services/event-manager'; +import { eSendEvent } from '../../services/event-manager'; import Navigation from '../../services/navigation'; import SearchService from '../../services/search'; +import { useNoteStore } from '../../stores/stores'; import { editing, InteractionManager } from '../../utils'; import { db } from '../../utils/database'; import { eOnLoadNote } from '../../utils/events'; import { tabBarRef } from '../../utils/global-refs'; import { getNote } from '../editor/Functions'; -import { presentDialog } from '../../components/dialog/functions'; -import Paragraph from '../../components/ui/typography/paragraph'; -import Heading from '../../components/ui/typography/heading'; -import NewFeature from '../../components/sheets/new-feature'; export const Home = ({ navigation }) => { const notes = useNoteStore(state => state.notes); @@ -78,7 +74,8 @@ export const Home = ({ navigation }) => { placeholder: 'Type a keyword to search in notes', data: db?.notes?.all, type: 'notes', - title: 'Notes' + title: 'Notes', + get: () => db.notes.all }); }; diff --git a/apps/mobile/src/screens/notebook/index.js b/apps/mobile/src/screens/notebook/index.js index 7a5cb2e5c..e6d604b02 100644 --- a/apps/mobile/src/screens/notebook/index.js +++ b/apps/mobile/src/screens/notebook/index.js @@ -69,7 +69,11 @@ export const Notebook = ({ route, navigation }) => { placeholder: `Search in "${params.current.title}"`, data: topics, type: 'topics', - title: params.current.title + title: params.current.title, + get: () => { + let notebook = db.notebooks.notebook(params?.current?.notebook?.id)?.data; + return notebook.topics; + } }); }; diff --git a/apps/mobile/src/screens/notebooks/index.js b/apps/mobile/src/screens/notebooks/index.js index 111a85e1e..838ab428b 100644 --- a/apps/mobile/src/screens/notebooks/index.js +++ b/apps/mobile/src/screens/notebooks/index.js @@ -10,6 +10,7 @@ import { useNotebookStore } from '../../stores/stores'; import Navigation from '../../services/navigation'; import SearchService from '../../services/search'; import { InteractionManager } from '../../utils'; +import { db } from '../../utils/database'; export const Notebooks = ({ navigation }) => { const notebooks = useNotebookStore(state => state.notebooks); @@ -67,7 +68,8 @@ export const Notebooks = ({ navigation }) => { placeholder: 'Type a keyword to search in notebooks', data: notebooks, type: 'notebooks', - title: 'Notebooks' + title: 'Notebooks', + get: () => db.notebooks.all }); }, []); diff --git a/apps/mobile/src/screens/notes/index.js b/apps/mobile/src/screens/notes/index.js index edfdb10ee..1b5934e68 100644 --- a/apps/mobile/src/screens/notes/index.js +++ b/apps/mobile/src/screens/notes/index.js @@ -19,7 +19,7 @@ import { openLinkInBrowser } from '../../utils/functions'; import { tabBarRef } from '../../utils/global-refs'; import { getNote } from '../editor/Functions'; -const getNotes = params => { +const getNotes = (params, group = true) => { if (!params) return []; let notes = []; if (params.type !== 'topic' && params.get !== 'monographs') { @@ -32,7 +32,7 @@ const getNotes = params => { if (!notes) { notes = []; } - return groupArray(notes, db.settings?.getGroupOptions('notes')); + return group ? groupArray(notes, db.settings?.getGroupOptions('notes')) : notes; }; function getAlias(params) { @@ -128,7 +128,7 @@ export const Notes = ({ route, navigation }) => { }, []); useEffect(() => { - if (navigation.isFocused()) { + if (navigation?.isFocused()) { updateSearch(); } }, [notes]); @@ -143,7 +143,11 @@ export const Notes = ({ route, navigation }) => { title: params.current?.type === 'tag' ? '#' + alias - : alias.slice(0, 1).toUpperCase() + alias.slice(1) + : alias.slice(0, 1).toUpperCase() + alias.slice(1), + + get: () => { + return getNotes(params.current, false); + } }); }; diff --git a/apps/mobile/src/screens/tags/index.js b/apps/mobile/src/screens/tags/index.js index 7178ca395..03cf9a41b 100644 --- a/apps/mobile/src/screens/tags/index.js +++ b/apps/mobile/src/screens/tags/index.js @@ -7,6 +7,7 @@ import Navigation from '../../services/navigation'; import SearchService from '../../services/search'; import { useTagStore } from '../../stores/stores'; import { InteractionManager } from '../../utils'; +import { db } from '../../utils/database'; export const Tags = ({ navigation }) => { const tags = useTagStore(state => state.tags); @@ -59,7 +60,8 @@ export const Tags = ({ navigation }) => { placeholder: 'Search in tags', data: tags, type: 'tags', - title: 'Tags' + title: 'Tags', + get: () => db.tags.all }); }; diff --git a/apps/mobile/src/screens/trash/index.js b/apps/mobile/src/screens/trash/index.js index 706516f17..c08c32a70 100644 --- a/apps/mobile/src/screens/trash/index.js +++ b/apps/mobile/src/screens/trash/index.js @@ -78,7 +78,8 @@ export const Trash = ({ navigation }) => { placeholder: 'Search in trash', data: trash, type: 'trash', - title: 'Trash' + title: 'Trash', + get: () => db.trash.all }); }; diff --git a/apps/mobile/src/services/search.js b/apps/mobile/src/services/search.js index ccf833303..edc036cff 100644 --- a/apps/mobile/src/services/search.js +++ b/apps/mobile/src/services/search.js @@ -4,7 +4,8 @@ import { db } from '../utils/database'; let searchInformation = { placeholder: 'Search in all notes', data: [], - type: 'notes' + type: 'notes', + get: null }; let keyword = null; @@ -17,7 +18,7 @@ function setTerm(term) { keyword = term; } -async function search() { +async function search(silent) { let searchstore = useSearchStore.getState(); let term = keyword; @@ -25,7 +26,9 @@ async function search() { searchstore.setSearchResults([]); return; } - searchstore.setSearchStatus(true, `Searching for "${term}" in ${searchInformation.title}`); + if (!silent) { + searchstore.setSearchStatus(true, `Searching for "${term}" in ${searchInformation.title}`); + } let results; if (!searchInformation.type) return; @@ -48,11 +51,18 @@ function getSearchInformation() { return searchInformation; } +function updateAndSearch() { + if (!keyword || keyword === '') return; + searchInformation.data = searchInformation.get() || []; + search(true); +} + const SearchService = { update, getSearchInformation, search, - setTerm + setTerm, + updateAndSearch }; export default SearchService; diff --git a/apps/mobile/src/utils/functions.js b/apps/mobile/src/utils/functions.js index bf52585d2..b86f8045e 100644 --- a/apps/mobile/src/utils/functions.js +++ b/apps/mobile/src/utils/functions.js @@ -6,6 +6,7 @@ import { eSendEvent, ToastEvent } from '../services/event-manager'; import Navigation from '../services/navigation'; import { db } from './database'; import { eClearEditor } from './events'; +import SearchService from '../services/search'; export const deleteItems = async item => { if (item && db.monographs.isPublished(item.id)) { @@ -110,6 +111,8 @@ export const deleteItems = async item => { useSelectionStore.getState().clearSelection(true); useMenuStore.getState().setMenuPins(); useMenuStore.getState().setColorNotes(); + console.log('running search again'); + SearchService.updateAndSearch(); }; export const openLinkInBrowser = async (link, colors) => { diff --git a/apps/mobile/src/utils/hooks/useActions.js b/apps/mobile/src/utils/hooks/useActions.js index 1438a3eb6..7d4351996 100644 --- a/apps/mobile/src/utils/hooks/useActions.js +++ b/apps/mobile/src/utils/hooks/useActions.js @@ -393,6 +393,7 @@ export const useActions = ({ close = () => {}, item }) => { } else { try { close(); + console.log('moving note to trash'); await deleteItems(item); } catch (e) {} } diff --git a/apps/mobile/src/utils/hooks/useAppEvents.js b/apps/mobile/src/utils/hooks/useAppEvents.js index bd7e22631..b3c46a00c 100644 --- a/apps/mobile/src/utils/hooks/useAppEvents.js +++ b/apps/mobile/src/utils/hooks/useAppEvents.js @@ -299,6 +299,7 @@ export const useAppEvents = () => { console.log('sync started'); await Sync.run(); console.log('hide progress dialog'); + await sleep(300); eSendEvent(eCloseProgressDialog); } setLastSynced(await db.lastSynced());