+ `
${str.replace(
+ new RegExp(intersection, 'gi'),
+ (casePreservedMatch) => `${casePreservedMatch}`,
+ )}`
+
+const prepareSearchResults = (list, searchValue) =>
+ list.map((article, index) => ({
+ ...article,
+ body: '',
+ cover: '',
+ createdAt: '',
+ id: index,
+ slug: article.slug,
+ authors: [],
+ topics: [],
+ title: article.title
+ ? getSearchCoincidences({
+ str: article.title,
+ intersection: searchValue,
+ })
+ : '',
+ subtitle: article.subtitle
+ ? getSearchCoincidences({
+ str: article.subtitle,
+ intersection: searchValue,
+ })
+ : '',
+ }))
+
export const SearchModal = () => {
const { t } = useLocalize()
- const { changeSearchParams } = useRouter()
- let qElement: HTMLInputElement | undefined
- const submitQuery = async (ev) => {
- ev.preventDefault()
- changeSearchParams({}, true)
- hideModal()
- openPage(router, 'search', { q: qElement.value })
+ const [inputValue, setInputValue] = createSignal('')
+ const [searchResultsList, setSearchResultsList] = createSignal<[] | null>([])
+ const [isLoading, setIsLoading] = createSignal(false)
+ // const [isLoadMoreButtonVisible, setIsLoadMoreButtonVisible] = createSignal(false)
+
+ const handleSearch = async () => {
+ const searchValue = inputValue() || ''
+
+ if (Boolean(searchValue) && searchValue.length > 2) {
+ setIsLoading(true)
+
+ try {
+ const response = await apiClient.getSearchResults(searchValue)
+ const searchResult = await response.json()
+
+ if (searchResult.length > 0) {
+ const preparedSearchResultsList = prepareSearchResults(searchResult, searchValue)
+
+ setSearchResultsList(preparedSearchResultsList)
+ } else {
+ setSearchResultsList(null)
+ }
+ } catch (error) {
+ console.log('search request failed', error)
+ } finally {
+ setIsLoading(false)
+ }
+ }
}
+
return (
-