web: added core functions for search

This commit is contained in:
alihamuh
2023-02-28 11:17:25 +05:00
committed by Abdullah Atta
parent 797d8b86ab
commit abd254d952
2 changed files with 37 additions and 37 deletions

View File

@@ -21,9 +21,8 @@ import { hexToRGB } from "../../utils/color";
import DatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";
import "./datepicker.css";
import { filterSearchEngine } from "./search";
import { Text } from "@theme-ui/components";
import { mainSearchEngine } from "./search";
import { db } from "../../common/db";
export function FilterInput(props) {
const {
@@ -59,7 +58,7 @@ export function FilterInput(props) {
<CustomInput
{...props}
id={`inputId_${index}`}
bg={item.input.state.error ? "errorBg" : hexToRGB("#9E9E9E", 0.1)}
bg={item.input.error ? "errorBg" : hexToRGB("#9E9E9E", 0.1)}
onFocus={async (e) => {
await checkErrors(props, e.target.innerText);
setSuggestions(await getSuggestions(e.target.innerText, item.input));
@@ -171,42 +170,16 @@ const checkErrors = async (props, query) => {
const { setFilters, index, item } = props;
query = query.trim();
let input = item.input;
let result = await (await mainSearchEngine(input.type, query)).result;
let result = await (await db.search.filter(input.filterName, query)).result;
let error = true;
if (!input.hasSuggestions || result.length > 0) error = false;
setFilters((filters) => {
let _filters = [...filters];
_filters[index].input.state = filterInputState(input, result, query);
_filters[index].input.error = error;
return _filters;
});
};
const filterInputState = (input, result, query) => {
console.log(
"filterInputState",
input.id,
query,
!input.hasSuggestions,
result.length > 0
);
if (!input.hasSuggestions)
return {
error: false,
message: "",
result: { type: input.type, value: query }
};
if (result.length > 0)
return { error: false, message: "", result: result[0] };
return {
error: true,
message: `This ${input.type.replace(
"s",
""
)} is not present in the database.`,
result: undefined
};
};
const deleteDefinition = (definitions, id) => {
let index = 0;
for (let definition of definitions) {
@@ -243,7 +216,7 @@ const onKeyPress = async (e, props) => {
Enter: async () => {
props.onKeyDown(e);
focusInput(index + 1);
let results = await filterSearchEngine(searchDefinitions);
let results = await db.search.filters(searchDefinitions);
onSearch(results);
setSuggestions([]);
e.preventDefault();

View File

@@ -20,13 +20,13 @@ import { store } from "../../stores/note-store";
import { db } from "../../common/db";
import { filter, parse } from "liqe";
export const mainSearchEngine = async (searchType, value) => {
export const mainSearchEngine = async (filter, query) => {
//name is cumbersome
// better name? fetchResults
const context = store.context;
const [lookupType, allData] = await filterItemsToType(searchType, context);
const [lookupType, allData] = await filterItemsToType(filter, context);
if (lookupType !== undefined && allData !== undefined) {
let result = await db.lookup[lookupType](allData, value);
let result = await db.lookup[lookupType](allData, query);
return { result, allData };
} else {
return { result: [], allData: [] };
@@ -65,7 +65,15 @@ const beginSearch = async (definition, type) => {
console.log("beginSearch", definition);
let notes = [];
type = type.trim();
<<<<<<< Updated upstream
<<<<<<< Updated upstream
db.notebooks.notebook();
=======
>>>>>>> Stashed changes
=======
>>>>>>> Stashed changes
switch (type) {
case "notebook": {
console.log("beginSearch", definition.topics);
@@ -78,6 +86,20 @@ const beginSearch = async (definition, type) => {
}
}
console.log("beginSearch notebook", notes);
<<<<<<< Updated upstream
<<<<<<< Updated upstream
=======
>>>>>>> Stashed changes
return notes;
}
case "notes": {
console.log("beginSearch notes", notes);
let allNotes = db.notes.all;
let notes = await db.lookup["notes"](allNotes, definition.value);
<<<<<<< Updated upstream
return notes;
}
=======
return notes;
}
case "notes": {
@@ -86,6 +108,11 @@ const beginSearch = async (definition, type) => {
let notes = await db.lookup["notes"](allNotes, definition.value);
return notes;
}
>>>>>>> Stashed changes
=======
return notes;
}
>>>>>>> Stashed changes
case "topic": {
for (let note of definition.notes) {
if (db.notes.note(note)) notes.push(db.notes.note(note)._note);