webapp/src/components/Views/Search.tsx

97 lines
2.6 KiB
TypeScript
Raw Normal View History

2022-09-22 09:37:49 +00:00
import { Show, For, createSignal } from 'solid-js'
2022-09-09 11:53:35 +00:00
import '../../styles/Search.scss'
import type { Shout } from '../../graphql/types.gen'
import { ArticleCard } from '../Feed/Card'
import { t } from '../../utils/intl'
2022-09-13 09:59:04 +00:00
import { useArticlesStore, loadSearchResults } from '../../stores/zine/articles'
2022-09-22 09:37:49 +00:00
import { handleClientRouteLinkClick, useRouter } from '../../stores/router'
type SearchPageSearchParams = {
by: '' | 'relevance' | 'rating'
}
2022-09-09 11:53:35 +00:00
type Props = {
2022-09-22 09:37:49 +00:00
query: string
results: Shout[]
2022-09-09 11:53:35 +00:00
}
2022-09-22 09:37:49 +00:00
export const SearchView = (props: Props) => {
2022-09-09 11:53:35 +00:00
const { getSortedArticles } = useArticlesStore({ sortedArticles: props.results })
2022-09-13 09:59:04 +00:00
const [getQuery, setQuery] = createSignal(props.query)
2022-09-09 11:53:35 +00:00
2022-09-22 09:37:49 +00:00
const { getSearchParams } = useRouter<SearchPageSearchParams>()
2022-09-13 09:59:04 +00:00
const handleQueryChange = (ev) => {
setQuery(ev.target.value)
}
2022-09-14 15:44:06 +00:00
const handleSubmit = (_ev) => {
2022-09-13 09:59:04 +00:00
// TODO page
// TODO sort
loadSearchResults({ query: getQuery() })
}
2022-09-09 11:53:35 +00:00
return (
<div class="search-page wide-container">
<form action="/search" class="search-form row">
<div class="col-sm-9">
2022-09-13 13:38:26 +00:00
{/*FIXME t*/}
2022-09-13 09:59:04 +00:00
<input type="search" name="q" onChange={handleQueryChange} placeholder="Введите текст..." />
2022-09-09 11:53:35 +00:00
</div>
<div class="col-sm-3">
2022-09-13 09:59:04 +00:00
<button class="button" type="submit" onClick={handleSubmit}>
{t('Search')}
</button>
2022-09-09 11:53:35 +00:00
</div>
</form>
<ul class="view-switcher">
2022-09-22 09:37:49 +00:00
<li
classList={{
selected: getSearchParams().by === 'relevance'
}}
>
<a href="?by=relevance" onClick={handleClientRouteLinkClick}>
2022-09-09 11:53:35 +00:00
{t('By relevance')}
</a>
</li>
2022-09-22 09:37:49 +00:00
<li
classList={{
selected: getSearchParams().by === 'rating'
}}
>
<a href="?by=rating" onClick={handleClientRouteLinkClick}>
2022-09-09 11:53:35 +00:00
{t('Top rated')}
</a>
</li>
</ul>
<Show when={getSortedArticles().length > 0}>
<h3>{t('Publications')}</h3>
<div class="floor">
<div class="row">
<For each={getSortedArticles()}>
{(article) => (
<div class="col-md-3">
<ArticleCard article={article} />
</div>
)}
</For>
<div class="col-md-3">
<a href="#" class="search__show-more">
<span class="search__show-more-inner">{t('Load more')}</span>
</a>
</div>
</div>
</div>
<h3>{t('Topics')}</h3>
<h3>{t('Authors')}</h3>
</Show>
</div>
)
}