- Search Results
+
+ Search Results
+
+
+ {propResults && (
+ <>
+
+
+ {results.map((tutorial, index) => (
+
+
+
+ ))}
+
+ >
+ )}
+
+
+ {!propResults &&
+ results.map((tutorial, index) => (
+
+ {!tutorial?.featured_image ? (
+
+ ) : (
+
+ )}
+
+ ))}
+
+
+
+ {results.length === 0 && (
+
+ No CodeLabz found with the given query.
+
+ )}
-
- {results.map((tutorial, index) => (
-
-
-
- ))}
- {results.length === 0 && "No CodeLabz with the given query"}
diff --git a/src/components/Tutorials/MyTutorials/Search/index.jsx b/src/components/Tutorials/MyTutorials/Search/index.jsx
index 405672fd..eceb71ec 100644
--- a/src/components/Tutorials/MyTutorials/Search/index.jsx
+++ b/src/components/Tutorials/MyTutorials/Search/index.jsx
@@ -51,10 +51,7 @@ const Header = () => {
if (result.length > 0) {
let tempArray = [];
result.forEach(item => {
- tempArray = [
- ...tempArray,
- ..._.filter(indexData, ref => ref.tutorial_id === item.ref)
- ];
+ tempArray = [...tempArray, item.ref];
});
setViewResults(true);
return setResults(tempArray);
diff --git a/src/helpers/elasticlunr.jsx b/src/helpers/elasticlunr.jsx
index 1f797e14..864e3b1f 100644
--- a/src/helpers/elasticlunr.jsx
+++ b/src/helpers/elasticlunr.jsx
@@ -1,4 +1,5 @@
import elasticlunr from "elasticlunr";
+
export default class Elasticlunr {
constructor(key, ...fields) {
this.elasticlunr = elasticlunr();
@@ -8,11 +9,34 @@ export default class Elasticlunr {
});
}
- addDocToIndex = doc => {
- this.elasticlunr.addDoc(doc);
+ addDocToIndex(doc) {
+ try {
+ this.elasticlunr.addDoc(doc);
+ } catch (error) {
+ console.error("Error adding document to index:", error);
+ }
+ }
+
+ searchFromIndex(query, options = { expand: true }) {
+ try {
+ if (typeof query !== "string") {
+ throw new Error("Query must be a string.");
+ }
+
+ return this.elasticlunr.search(query, options);
+ } catch (error) {
+ console.error("Error searching the index:", error);
+ return [];
+ }
+ }
+
+ // To get a specific document by its reference key
+ getDocById = id => {
+ return this.elasticlunr.documentStore.getDoc(id);
};
- searchFromIndex = query => {
- return this.elasticlunr.search(query, { expand: true });
+ // To get all documents in the index
+ getAllDocs = () => {
+ return Object.values(this.elasticlunr.documentStore.docs);
};
}
diff --git a/src/routes.jsx b/src/routes.jsx
index 39493a49..9a2cbd44 100644
--- a/src/routes.jsx
+++ b/src/routes.jsx
@@ -27,6 +27,7 @@ import MainNavbar from "./components/NavBar/new/MainNavbar";
import UserDashboard from "./components/UserDashboard";
import TutorialPage from "./components/TutorialPage";
import Notification from "./components/Notification";
+import SearchResultsComponent from "./components/Tutorials/MyTutorials/Search/SearchResultsComponent";
const AuthIsLoaded = ({ children }) => {
const profile = useSelector(({ firebase: { profile } }) => profile);
@@ -157,6 +158,7 @@ const Routes = () => {
path={"/tutorial/:id"}
component={UserIsAllowedUserDashboard(TutorialPage)}
/>
+
async (firebase, firestore) => {
+ try {
+ const snapshot = await firestore.collection("tutorials").get();
+ snapshot.forEach(doc => {
+ const data = doc.data();
+ const tutorial = { id: doc.id, ...data };
+ // console.log("Adding tutorial to index:", tutorial);
+ tutorials_index.addDocToIndex(tutorial);
+ });
+
+ // console.log("All docs in index:", tutorials_index.getAllDocs());
+ } catch (error) {
+ console.error("Error fetching or indexing tutorials:", error);
+ }
+};
+
export const searchFromTutorialsIndex = query => {
- return tutorials_index.searchFromIndex(query);
+ const results = tutorials_index.searchFromIndex(query);
+ // console.log("searchFromIndex", query, results);
+ return results;
};
// Gets all the tutorials with this user having edit access