Custom Hook with Typesense.js for Product Search
TLDR Jameshwart seeks a way to set a state before starting a search function in typesense.js. Jason suggests using hooks exposed by axios, mentioning possible GraphQL integration with Typesense.
1
May 01, 2023 (7 months ago)
Jameshwart
04:21 AMJason
04:24 AMJameshwart
04:24 AMJameshwart
04:25 AMimport { useState } from 'react';
import {
ITSProductQueryResponse,
ITSTaxonomyProductQueryVars,
} from '../typesense/types/taxonomy';
import TSTaxonomy from '../typesense/taxonomy';
import { useUpdateEffect } from 'usehooks-ts';
const useFetchTsTaxonomyProducts = (queryVars: ITSTaxonomyProductQueryVars) => {
const [data, setData] = useState<ITSProductQueryResponse>();
const [error, setError] = useState(null);
const [loading, setLoading] = useState(false);
useUpdateEffect(() => {
console.log('useFetchTsTaxonomyProducts useUpdateEffect');
const controller = new AbortController();
setLoading(true);
let searchParameters = TSTaxonomy.generateSearchParams(queryVars);
const searchOptions = {
cacheSearchResultsForSeconds: 60,
abortSignal: controller.signal,
};
TSTaxonomy.getProductDocument()
.search(searchParameters, searchOptions)
.then((results) => {
setData(
TSTaxonomy.generateProductQueryResponse(queryVars, results)
);
})
.catch(setError)
.finally(() => setLoading(false));
return () => {
// controller.abort();
};
}, [queryVars]);
return { data, error, loading };
};
export default useFetchTsTaxonomyProducts;
Jason
04:25 AMJameshwart
04:25 AMJason
04:26 AMJameshwart
04:27 AMJason
04:27 AMJameshwart
04:30 AMMay 03, 2023 (7 months ago)
Jason
08:29 PMTypesense
Indexed 3015 threads (79% resolved)
Similar Threads
Issues with Typesense Frontend Search Integration
Akash experienced issues integrating Typesense into their frontend search, particularly with their search box. Jason guided them to troubleshoot, eventually identifying the issue to be with react-instantsearch-dom and suggesting a downgrade to react 17, which resolved the issue.
Debugging Search Issue with Typesense Server Caching
Jameshwart reported experiencing caching issues with typesense server, despite the server's default setting of not caching. Through troubleshooting steps with Kishore Nallan, they were able to confirm an issue with the Typesense Javascript client and resolved it by adding `cacheSearchResultsForSeconds: 0` after `apiKey` in the initialisation. Laura also participated in discussion, learning about server and client level caching.
Troubleshooting Typesense Setup and Understanding Facets and Keywords
Demitri encountered errors when exploring Typesense for the first time. Jason guided them through troubleshooting and discussed facets, keyword settings, and widget configurations. Helin shared a Python demo app and its source code to help Demitri with their project.
Troubleshooting TypeScript Error with Typesense
GM experienced an error with Typesense in TypeScript, requiring help to correct the issues. Jason helped propose solutions and adjustments to the code. Ultimately, they were able to resolve the errors and successfully implement a search function.
Implementing Search Suggestion/Autocomplete Functionality in Typesense
Denis needed advice on implementing search suggestions in Typesense. Jason provided a solution for fetching after 3+ letters. However, questions on design pattern and passing hooks remain unanswered.