webapp/src/components/Pages/SearchPage.tsx

46 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-11-14 17:41:05 +00:00
import { PageWrap } from '../_shared/PageWrap'
2022-09-22 09:37:49 +00:00
import { SearchView } from '../Views/Search'
import type { PageProps } from '../types'
2022-10-05 15:11:14 +00:00
import { createMemo, createSignal, onCleanup, onMount, Show } from 'solid-js'
2022-11-15 14:24:50 +00:00
import { loadShoutsBy, resetSortedArticles } from '../../stores/zine/articles'
2022-10-05 15:11:14 +00:00
import { useRouter } from '../../stores/router'
2022-10-09 10:56:39 +00:00
import { Loading } from '../Loading'
2022-09-22 09:37:49 +00:00
export const SearchPage = (props: PageProps) => {
2022-10-05 15:11:14 +00:00
const [isLoaded, setIsLoaded] = createSignal(Boolean(props.searchResults))
const q = createMemo(() => {
2022-10-25 16:25:42 +00:00
const { page: getPage } = useRouter()
2022-10-05 15:11:14 +00:00
const page = getPage()
if (page.route !== 'search') {
throw new Error('ts guard')
}
return page.params.q
})
onMount(async () => {
if (isLoaded()) {
return
}
2022-11-15 14:24:50 +00:00
await loadShoutsBy({ by: { title: q(), body: q() }, limit: 50, offset: 0 })
2022-10-05 15:11:14 +00:00
setIsLoaded(true)
})
onCleanup(() => resetSortedArticles())
2022-09-22 09:37:49 +00:00
return (
<PageWrap>
2022-10-09 10:56:39 +00:00
<Show when={isLoaded()} fallback={<Loading />}>
2022-10-05 15:11:14 +00:00
<SearchView results={props.searchResults || []} query={props.searchQuery} />
</Show>
</PageWrap>
2022-09-22 09:37:49 +00:00
)
}
// for lazy loading
export default SearchPage