Skip to content

Commit 3c30038

Browse files
authored
fe: fix landscapes search (#123)
1 parent 3d5532d commit 3c30038

File tree

5 files changed

+106
-9
lines changed

5 files changed

+106
-9
lines changed

app/src/app/(frontend)/[locale]/(app)/landscapes/page.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import { getTranslations } from "next-intl/server";
77

88
import { Header } from "@/containers/header";
99
import { Landscapes } from "@/containers/landscapes";
10-
import { LandscapesFilters } from "@/containers/landscapes/filters";
10+
import LandscapesFilters from "@/containers/landscapes/filters";
11+
import { LandscapesFilteredList } from "@/containers/landscapes/filters/list";
1112
import { LandscapesSearch } from "@/containers/landscapes/search";
1213

1314
export async function generateMetadata(): Promise<Metadata> {
@@ -32,12 +33,7 @@ export default async function LandscapesPage() {
3233

3334
<Suspense>
3435
<LandscapesFilters>
35-
<p className="text-background bg-foreground p-6">
36-
Lorem ipsum dolor sit amet consectetur adipisicing elit. Reprehenderit magni porro
37-
ipsum accusantium natus sunt blanditiis repudiandae, eum minus reiciendis,
38-
architecto ut veritatis eius exercitationem aperiam perspiciatis qui doloribus
39-
debitis?
40-
</p>
36+
<LandscapesFilteredList />
4137
</LandscapesFilters>
4238
</Suspense>
4339
</Command>

app/src/cms/utils/layer-validation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const LayerTypeValidation: CollectionBeforeValidateHook<Layer> = (args) =
1919
],
2020
});
2121
}
22-
console.log(data);
22+
2323
if (data && data.type === LAYER_TYPE.CONTEXTUAL && data.indicators?.length) {
2424
throw new ValidationError({
2525
collection: "layers",

app/src/containers/landscapes/filters.tsx renamed to app/src/containers/landscapes/filters/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { landscapesAtom } from "@/app/(frontend)/[locale]/(app)/store";
1010

1111
import { ScrollArea } from "@/components/ui/scroll-area";
1212

13-
export const LandscapesFilters = ({ children }: PropsWithChildren) => {
13+
const LandscapesFilters = ({ children }: PropsWithChildren) => {
1414
const { enabled } = useAtomValue(landscapesAtom);
1515

1616
const visible = enabled;
@@ -35,3 +35,5 @@ export const LandscapesFilters = ({ children }: PropsWithChildren) => {
3535
</section>
3636
);
3737
};
38+
39+
export default LandscapesFilters;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"use client";
2+
3+
import Link from "next/link";
4+
5+
import { convertLexicalToPlaintext } from "@payloadcms/richtext-lexical/plaintext";
6+
7+
import { CommandItem } from "cmdk";
8+
9+
import { Landscape } from "@/payload-types";
10+
11+
export const LandscapesItem = (props: Landscape) => {
12+
return (
13+
<CommandItem
14+
className="text-background data-[selected=true]:bg-accent data-[selected=true]:text-background cursor-pointer px-3 py-1 text-left transition-colors duration-150 data-[selected=true]:indent-1"
15+
asChild
16+
>
17+
<Link className="block" href={`/landscapes/${props.id}`}>
18+
{convertLexicalToPlaintext({ data: props.name })}
19+
</Link>
20+
</CommandItem>
21+
);
22+
};
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
"use client";
2+
3+
import { useQuery, keepPreviousData } from "@tanstack/react-query";
4+
import { CommandEmpty, CommandGroup, CommandList } from "cmdk";
5+
import { useAtomValue } from "jotai";
6+
import { useLocale } from "next-intl";
7+
import { useDebounceValue } from "usehooks-ts";
8+
9+
import { landscapesAtom } from "@/app/(frontend)/[locale]/(app)/store";
10+
11+
import { LandscapesItem } from "@/containers/landscapes/filters/item";
12+
13+
import { Loader } from "@/components/ui/loader";
14+
15+
import API from "@/services/api";
16+
17+
export const LandscapesFilteredList = () => {
18+
const locale = useLocale();
19+
const landscapes = useAtomValue(landscapesAtom);
20+
21+
const [search] = useDebounceValue(landscapes.search, 100);
22+
23+
const {
24+
data: landscapesData,
25+
isFetched,
26+
isFetching,
27+
} = useQuery({
28+
...API.queryOptions("get", "/api/landscapes", {
29+
params: {
30+
query: {
31+
depth: 1,
32+
limit: 0,
33+
page: 1,
34+
sort: "id",
35+
locale,
36+
select: {
37+
id: true,
38+
name: true,
39+
},
40+
where: {
41+
published: {
42+
equals: true,
43+
},
44+
...(!!search && {
45+
"name.root.children.children.text": {
46+
like: search,
47+
},
48+
}),
49+
},
50+
},
51+
},
52+
}),
53+
placeholderData: keepPreviousData,
54+
});
55+
56+
return (
57+
<CommandList className="bg-foreground relative rounded-4xl p-6">
58+
<Loader isLoading={isFetching} />
59+
60+
{!isFetching && isFetched && (
61+
<CommandEmpty>
62+
<span className="text-muted-foreground">No locations found</span>
63+
</CommandEmpty>
64+
)}
65+
66+
{!!landscapesData?.docs.length && (
67+
<CommandGroup className="[&_[cmdk-group-heading]]:text-muted-foreground [&_[cmdk-group-heading]]:text-bold [&_[cmdk-group-heading]]:text-xs [&_[cmdk-group-heading]]:uppercase">
68+
<div className="py-2">
69+
{landscapesData?.docs.map((location) => (
70+
<LandscapesItem key={location.id} {...location} />
71+
))}
72+
</div>
73+
</CommandGroup>
74+
)}
75+
</CommandList>
76+
);
77+
};

0 commit comments

Comments
 (0)