Skip to content

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

add a simple SearchService

ammarahm-ed744f8020ec15
diff --git a/apps/mobile/src/services/SearchService.js b/apps/mobile/src/services/SearchService.js
new file mode 100644
index 000000000..7d2154ea7
--- /dev/null
+++ b/apps/mobile/src/services/SearchService.js
@@ -0,0 +1,50 @@
+import {updateEvent} from '../components/DialogManager/recievers';
+import {Actions} from '../provider/Actions';
+import {db} from '../utils/DB';
+import {ToastEvent} from './EventManager';
+
+let searchInformation = {
+ placeholder: 'Search all notes',
+ data: [],
+ type: 'notes',
+};
+
+function update(data) {
+ searchInformation = data;
+}
+
+async function search(term) {
+ console.log(term, term.length);
+ if (!term || term.length === 0) {
+ updateEvent({
+ type: Actions.SEARCH_RESULTS,
+ results: [],
+ });
+ return;
+ }
+ let results;
+ if (!searchInformation.type) return;
+ results = await db.lookup[searchInformation.type](
+ searchInformation.data,
+ term,
+ );
+ console.log(results.length);
+ if (!results || results.length === 0) {
+ ToastEvent.show('No search results found for ' + term, 'error');
+ return;
+ }
+ updateEvent({
+ type: Actions.SEARCH_RESULTS,
+ results: results,
+ });
+}
+
+function getSearchInformation() {
+ return searchInformation;
+}
+
+export default {
+ update,
+ getSearchInformation,
+ search,
+};