server/assets/js/react/pages/Search.tsx

81 lines
2.2 KiB
TypeScript
Raw Normal View History

2023-12-04 13:42:32 +00:00
import * as React from "react"
2023-12-04 20:35:01 +00:00
import axios from "axios"
import { useQuery } from "@tanstack/react-query"
2023-12-04 13:42:32 +00:00
2023-12-04 20:35:01 +00:00
import type { Coach } from "../types/Coach"
2023-12-04 13:42:32 +00:00
import { Container } from "../components/Container"
import { FadeIn, FadeInStagger } from "../components/FadeIn"
import { FallbackMessage } from "../components/FallbackMessage"
2023-12-04 23:04:30 +00:00
import { FilterModal } from "../components/FilterModal"
2023-12-04 13:42:32 +00:00
import { FilterScroll } from "../components/FilterScroll"
import { Loading } from "../components/Loading"
2023-12-04 20:35:01 +00:00
import { SearchResult } from "../components/SearchResult"
2023-12-04 23:04:30 +00:00
import { defaultSearchParams } from "../types/SearchParams"
2023-12-04 13:42:32 +00:00
2023-12-04 20:35:01 +00:00
function SearchResults() {
const { isLoading, isError, data } = useQuery({
queryKey: ["coaches"],
queryFn: async () => {
const response = await axios.get<{ data: Coach[] }>("/api/coaches/")
return response.data.data
},
})
2023-12-04 13:42:32 +00:00
2023-12-04 20:35:01 +00:00
if (isLoading) {
2023-12-04 23:16:50 +00:00
return <Loading className="mt-40" loading />
2023-12-04 20:35:01 +00:00
}
if (isError) {
return (
<FallbackMessage
2023-12-04 23:16:50 +00:00
className="mt-40"
title="Blunder!"
body="Our tech team is working to restore the checkmate."
2023-12-04 20:35:01 +00:00
/>
)
}
2023-12-04 13:42:32 +00:00
2023-12-04 20:35:01 +00:00
return (
<FadeInStagger
2023-12-04 23:16:50 +00:00
className="mt-10 grid grid-cols-1 gap-6 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4"
2023-12-04 20:35:01 +00:00
faster
>
{data?.map((coach, index) => (
<FadeIn key={index} className="flex cursor-pointer flex-col">
<SearchResult
src={coach.image_url ?? ""}
title={coach.name ?? ""}
subtitle={coach.name ?? ""}
/>
</FadeIn>
))}
</FadeInStagger>
)
2023-12-04 13:42:32 +00:00
}
export function Search() {
2023-12-04 20:35:01 +00:00
const [searchParams, setSearchParams] = React.useState(defaultSearchParams)
2023-12-04 23:04:30 +00:00
const [modalOpen, setModalOpen] = React.useState(false)
2023-12-04 13:42:32 +00:00
return (
<Container className="pt-8">
2023-12-04 20:35:01 +00:00
<FilterScroll
params={searchParams}
onSelect={setSearchParams}
2023-12-04 23:04:30 +00:00
onModal={() => setModalOpen(true)}
/>
<FilterModal
open={modalOpen}
defaultValues={searchParams}
onClose={() => setModalOpen(false)}
onSubmit={(q) => {
setSearchParams(q)
setModalOpen(false)
}}
2023-12-04 20:35:01 +00:00
/>
<SearchResults />
2023-12-04 13:42:32 +00:00
</Container>
)
}