Skip to content

Commit 3050dc9

Browse files
committed
connection search implement
1 parent 46ee340 commit 3050dc9

File tree

6 files changed

+64
-23
lines changed

6 files changed

+64
-23
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"http-proxy-middleware": "^2.0.2",
3333
"js-base64": "^3.7.2",
3434
"js-cookie": "^3.0.1",
35+
"lodash-es": "^4.17.21",
3536
"matcher": "^5.0.0",
3637
"node-fetch": "^3.2.0",
3738
"ohmyfetch": "^0.4.15",
@@ -62,6 +63,7 @@
6263
"@graphql-codegen/schema-ast": "^2.4.1",
6364
"@types/js-cookie": "^3.0.1",
6465
"@types/loadable__component": "^5.13.4",
66+
"@types/lodash-es": "^4.17.6",
6567
"@types/node": "^17.0.15",
6668
"@types/react": "^17.0.39",
6769
"@types/react-dom": "^17.0.11",

schema/schema.graphql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ type Query {
100100
myInfo: User
101101
node(id: ID!): Node
102102
post(id: ID!): Post
103-
posts(after: ID, first: Int, orderBy: [OrderInput!]): PostConnect!
103+
posts(after: ID, first: Int, orderBy: [OrderInput!], search: String): PostConnect!
104104
users(before: ID, last: Int): UserConnect!
105105
}
106106

src/components/Posts.tsx

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { useMemo } from "react";
1+
import { debounce } from "lodash-es";
2+
import { useEffect, useMemo } from "react";
23
import { graphql, usePaginationFragment } from "react-relay";
34
import { HomePostsQuery$data } from "../pages/__generated__/HomePostsQuery.graphql";
45
import { InfinateScrollGrid } from "./InfinateScrollGrid";
@@ -8,6 +9,7 @@ import { PostsQuery } from "./__generated__/PostsQuery.graphql";
89

910
interface PostsProps {
1011
postsRef: HomePostsQuery$data;
12+
search: string;
1113
}
1214

1315
export const postsFragment = graphql`
@@ -19,10 +21,14 @@ export const postsFragment = graphql`
1921
type: "[OrderInput!]"
2022
defaultValue: [{ property: "id", direction: DESC }]
2123
}
24+
search: { type: "String", defaultValue: null }
2225
)
2326
@refetchable(queryName: "PostsQuery") {
24-
posts(first: $first, after: $after, orderBy: $orderBy)
25-
@connection(key: "HomePostConnectionFragment_posts") {
27+
posts(first: $first, after: $after, orderBy: $orderBy, search: $search)
28+
@connection(
29+
key: "HomePostConnectionFragment_posts"
30+
filters: ["orderBy", "search"]
31+
) {
2632
__id
2733
edges {
2834
node {
@@ -38,27 +44,37 @@ export const postsFragment = graphql`
3844
}
3945
`;
4046

41-
export const Posts = ({ postsRef }: PostsProps) => {
47+
export const Posts = ({ postsRef, search }: PostsProps) => {
4248
const pagination = usePaginationFragment<PostsQuery, PostsFragment_query$key>(
4349
postsFragment,
4450
postsRef
4551
);
4652

53+
const searching = useMemo(
54+
() =>
55+
debounce(
56+
(search: string) => {
57+
pagination.refetch({
58+
first: 10,
59+
after: null,
60+
orderBy: [{ property: "id", direction: "DESC" }],
61+
search: search,
62+
});
63+
},
64+
300,
65+
),
66+
[]
67+
);
68+
69+
useEffect(() => {
70+
searching(search);
71+
}, [searching, search]);
72+
4773
const edges = useMemo(() => {
4874
return pagination.data.posts.edges.filter((edge) => edge.node != null);
4975
}, [pagination.data.posts.edges]);
5076

51-
if (!edges.length) {
52-
return (
53-
<div className="w-full m-16 text-center">
54-
There is no posts. Be the first to write
55-
</div>
56-
);
57-
}
58-
59-
pagination.isLoadingNext;
60-
61-
return (
77+
return edges.length ? (
6278
<InfinateScrollGrid
6379
data={edges}
6480
endReached={() => {
@@ -70,5 +86,7 @@ export const Posts = ({ postsRef }: PostsProps) => {
7086
return <PostCard key={post.cursor} post={post.node} />;
7187
}}
7288
/>
89+
) : (
90+
<div className="w-full m-16 text-center">There is no posts.</div>
7391
);
7492
};

src/components/Progress.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
export function Progress() {
2-
console.log("Progress");
3-
42
return <div className="w-full text-center my-36">Loading...</div>;
53
}

src/pages/Home.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { encode } from "js-base64";
2-
import { Suspense } from "react";
2+
import { Suspense, useState } from "react";
33
import {
44
graphql,
55
PreloadedQuery,
@@ -28,6 +28,8 @@ export const homePostsQuery = graphql`
2828
export default function HomePage({ preloaded }: HomePageProps) {
2929
const auth = useSnapshot(useAuth());
3030

31+
const [search, setSearch] = useState("");
32+
3133
const postsRef = usePreloadedQuery<HomePostsQuery>(
3234
homePostsQuery,
3335
preloaded.query
@@ -40,13 +42,23 @@ export default function HomePage({ preloaded }: HomePageProps) {
4042
const connectionID = fragmentData.posts.__id;
4143

4244
return (
43-
<Suspense fallback={<Progress />}>
45+
<div>
4446
{auth.isAuthentiated && (
4547
<div className="flex justify-end px-4 pt-2">
4648
<Link to={`/post/new?cid=${encode(connectionID)}`}>New Post</Link>
4749
</div>
4850
)}
49-
<Posts postsRef={postsRef} />
50-
</Suspense>
51+
<div className="mx-8 mt-4">
52+
<input
53+
className="font-sans block text-sm leading-5 w-full py-2 px-3 border-2 border-slate-600 text-slate-500 rounded-lg shadow-sm focus:outline-none focus:ring focus:ring-slate-200"
54+
placeholder="Search"
55+
type="text"
56+
onChange={(e) => setSearch(e.target.value)}
57+
/>
58+
</div>
59+
<Suspense fallback={<Progress />}>
60+
<Posts postsRef={postsRef} search={search} />
61+
</Suspense>
62+
</div>
5163
);
5264
}

yarn.lock

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1144,7 +1144,16 @@ __metadata:
11441144
languageName: node
11451145
linkType: hard
11461146

1147-
"@types/lodash@npm:^4.14.175":
1147+
"@types/lodash-es@npm:^4.17.6":
1148+
version: 4.17.6
1149+
resolution: "@types/lodash-es@npm:4.17.6"
1150+
dependencies:
1151+
"@types/lodash": "*"
1152+
checksum: 9bd239dd525086e278821949ce12fbdd4f100a060fed9323fc7ad5661113e1641f28a7ebab617230ed3474680d8f4de705c1928b48252bb684be6ec9eed715db
1153+
languageName: node
1154+
linkType: hard
1155+
1156+
"@types/lodash@npm:*, @types/lodash@npm:^4.14.175":
11481157
version: 4.14.178
11491158
resolution: "@types/lodash@npm:4.14.178"
11501159
checksum: a69a04a60bfc5257c3130a554b4efa0c383f0141b7b3db8ab7cf07ad2a46ea085fce66d0242da41da7e5647b133d5dfb2c15add9cbed8d7fef955e4a1e5b3128
@@ -5953,6 +5962,7 @@ __metadata:
59535962
"@loadable/component": ^5.15.2
59545963
"@types/js-cookie": ^3.0.1
59555964
"@types/loadable__component": ^5.13.4
5965+
"@types/lodash-es": ^4.17.6
59565966
"@types/node": ^17.0.15
59575967
"@types/react": ^17.0.39
59585968
"@types/react-dom": ^17.0.11
@@ -5971,6 +5981,7 @@ __metadata:
59715981
http-proxy-middleware: ^2.0.2
59725982
js-base64: ^3.7.2
59735983
js-cookie: ^3.0.1
5984+
lodash-es: ^4.17.21
59745985
matcher: ^5.0.0
59755986
node-fetch: ^3.2.0
59765987
npm-run-all: ^4.1.5

0 commit comments

Comments
 (0)