import {useEffect, useState} from "react"; import {BehaviorSubject, catchError, debounceTime, delay, distinctUntilChanged, filter, map, of, switchMap} from "rxjs"; import {useRouter} from "next/router"; import {MeiliSearch} from "meilisearch"; import {useDispatch} from "react-redux"; import {environment} from "../../environments/environment"; import styles from './blog-search.module.scss'; import {setBlogSearchState} from "../../store/blogSearchSlice"; /* eslint-disable-next-line */ export interface BlogSearchProps { } export function BlogSearch(props: BlogSearchProps) { const router = useRouter(); const [client, setClient] = useState(null); const [loading, setLoading] = useState(false); const [subject, setSubject] = useState(null); const dispatch = useDispatch(); useEffect(() => { if (subject === null) { const sub = new BehaviorSubject(''); const client = new MeiliSearch({ host: environment.meiliUrl, apiKey: environment.meiliApiKey }) setSubject(sub); setClient(client); } else { subject.pipe( map((s: string) => s.trim()), distinctUntilChanged(), filter((s: string) => s.length >= 2), debounceTime(200), map((value) => { setLoading(true); return value; }), delay(2000), switchMap(async (value: string) => { const index = await client.getIndex('article'); const articles = await index.search(value); setLoading(false); await router.push('/shop/results'); dispatch(setBlogSearchState(articles.hits)); }), catchError((err) => of(false)) ).subscribe(() => setLoading(false)); } }, [subject]); const onSearchChange = (e) => subject.next(e.target.value); return (