-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
185 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,46 @@ | ||
import QueryLoader from "components/QueryLoader"; | ||
import { Info } from "components/Status"; | ||
import { useNavigation, useSearchParams } from "react-router-dom"; | ||
import type { EndowListPaginatedAWSQueryRes, EndowmentCard } from "types/aws"; | ||
import Card from "./Card"; | ||
import useCards from "./useCards"; | ||
|
||
export default function Cards({ classes = "" }: { classes?: string }) { | ||
const { | ||
hasMore, | ||
isLoading, | ||
isFetching, | ||
isLoadingNextPage, | ||
loadNextPage, | ||
data, | ||
isError, | ||
} = useCards(); | ||
interface Props { | ||
classes?: string; | ||
page: EndowListPaginatedAWSQueryRes<EndowmentCard[]>; | ||
} | ||
|
||
export default function Cards({ classes = "", page }: Props) { | ||
const navigation = useNavigation(); | ||
const [params, setParams] = useSearchParams(); | ||
const { Items: endowments, NumOfPages, Page } = page; | ||
|
||
const hasMore = Page < NumOfPages; | ||
|
||
if (endowments.length === 0) { | ||
return <Info>No organisations found</Info>; | ||
} | ||
|
||
return ( | ||
<QueryLoader | ||
queryState={{ | ||
data: data?.Items || [], | ||
isLoading, | ||
isFetching, | ||
isError, | ||
}} | ||
messages={{ | ||
error: "Failed to get organisations", | ||
loading: "Getting organisations..", | ||
empty: "No organisations found", | ||
}} | ||
classes={{ | ||
container: `${classes} dark:text-white`, | ||
}} | ||
<div | ||
className={`${classes} w-full grid sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-4 content-start`} | ||
> | ||
{(endowments) => ( | ||
<div | ||
className={`${classes} w-full grid sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-4 content-start`} | ||
> | ||
{endowments.map((endow) => ( | ||
<Card {...endow} key={endow.id} /> | ||
))} | ||
{endowments.map((endow) => ( | ||
<Card {...endow} key={endow.id} /> | ||
))} | ||
|
||
{hasMore && ( | ||
<button | ||
className="col-span-full btn-blue rounded-md p-2 text-sm w-full mt-6" | ||
onClick={loadNextPage} | ||
disabled={isLoading || isLoadingNextPage} | ||
> | ||
Load more organizations | ||
</button> | ||
)} | ||
</div> | ||
{hasMore && ( | ||
<button | ||
type="button" | ||
disabled={navigation.state === "loading"} | ||
className="col-span-full btn-blue rounded-md p-2 text-sm w-full mt-6" | ||
onClick={() => { | ||
const n = new URLSearchParams(params); | ||
n.set("page", String(Page + 1)); | ||
setParams(n, { replace: true, preventScrollReset: true }); | ||
}} | ||
> | ||
Load more organizations | ||
</button> | ||
)} | ||
</QueryLoader> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,53 @@ | ||
import Icon from "components/Icon"; | ||
import { useState } from "react"; | ||
import debounce from "lodash/debounce"; | ||
import { type ChangeEventHandler, useEffect, useRef } from "react"; | ||
import { useSearchParams } from "react-router-dom"; | ||
|
||
export default function Search({ classes = "" }: { classes?: string }) { | ||
const ref = useRef<ReturnType<typeof debounce>>(); | ||
const inputRef = useRef<HTMLInputElement>(null); | ||
const [params, setParams] = useSearchParams(); | ||
const q = params.get("query") || ""; | ||
const [search, setSearch] = useState(q); | ||
const q = decodeURIComponent(params.get("query") || ""); | ||
const _id = params.get("_f") || ""; | ||
|
||
const onChange: ChangeEventHandler<HTMLInputElement> = (e) => { | ||
const n = new URLSearchParams(params); | ||
n.set("query", encodeURIComponent(e.target.value)); | ||
n.set("page", "1"); | ||
n.set("_f", "search"); | ||
setParams(n, { | ||
replace: true, | ||
preventScrollReset: true, | ||
}); | ||
}; | ||
|
||
useEffect(() => { | ||
if (!_id) return; | ||
inputRef.current?.focus(); | ||
}, [_id]); | ||
|
||
useEffect(() => { | ||
return () => { | ||
ref.current?.cancel(); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<form | ||
onSubmit={(e) => { | ||
e.preventDefault(); | ||
const n = new URLSearchParams(params); | ||
n.set("query", e.currentTarget.query.value); | ||
setParams(n, { replace: true, preventScrollReset: true }); | ||
}} | ||
className={`${classes} flex gap-2 items-center`} | ||
<div | ||
className={`${classes} flex gap-2 items-center border border-gray-l4 rounded-lg relative`} | ||
> | ||
<Icon | ||
type="Search" | ||
size={20} | ||
className="absolute left-3 top-1/2 -translate-y-1/2" | ||
/> | ||
<input | ||
onChange={(e) => { | ||
const s = e.target.value; | ||
//user deleted all text | ||
setSearch(s); | ||
if (!s && q) { | ||
const n = new URLSearchParams(params); | ||
n.delete("query"); | ||
setParams(n, { replace: true, preventScrollReset: true }); | ||
} | ||
}} | ||
type="search" | ||
name="query" | ||
value={search} | ||
className="w-full h-full p-3 placeholder:text-navy-l3 text-navy-d4 font-medium font-heading border border-gray-l4 rounded-lg outline-blue-d1 outline-offset-4" | ||
ref={inputRef} | ||
defaultValue={q} | ||
onChange={debounce(onChange, 500)} | ||
className="w-full h-full p-3 pl-10 placeholder:text-navy-l3 text-navy-d4 font-medium font-heading rounded-lg outline-blue-d1 outline-offset-4" | ||
placeholder={q || "Search organizations..."} | ||
/> | ||
<button | ||
disabled={!search} | ||
type="submit" | ||
className="rounded-lg border border-gray-l4 h-full p-3 enabled:hover:border-blue-d1 enabled:hover:text-blue-d1 disabled:text-gray" | ||
> | ||
<Icon type="Search" size={20} /> | ||
</button> | ||
</form> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters