-
-
Notifications
You must be signed in to change notification settings - Fork 93
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
4 changed files
with
142 additions
and
109 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--- | ||
title: Efficient client-side sorting in React | ||
sidebar_label: Sorting (client-side) | ||
--- | ||
|
||
import SortDemo from '../shared/\_SortDemo.mdx'; | ||
|
||
# Client Side Sorting | ||
|
||
Here we have an API that sorts based on the `orderBy` field. By wrapping our [Collection](../api/Collection.md) | ||
in a [Query](../api/Query.md) that sorts, we can ensure we maintain the correct order after [pushing](../api/RestEndpoint.md#push) | ||
new posts. | ||
|
||
Our example code starts sorting by `title`. Try adding some posts and see them inserted in the correct sort | ||
order. | ||
|
||
<SortDemo defaultTab="PostList" /> |
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 |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import HooksPlayground from '@site/src/components/HooksPlayground'; | ||
import { | ||
postFixtures, | ||
getInitialInterceptorData, | ||
} from '@site/src/fixtures/posts-collection'; | ||
|
||
<HooksPlayground fixtures={postFixtures} getInitialInterceptorData={getInitialInterceptorData} row defaultTab={props.defaultTab}> | ||
|
||
```ts title="getPosts" {20-27} | ||
import { Entity, RestEndpoint } from '@data-client/rest'; | ||
|
||
class Post extends Entity { | ||
id = ''; | ||
title = ''; | ||
group = ''; | ||
author = ''; | ||
|
||
pk() { | ||
return this.id; | ||
} | ||
} | ||
export const getPosts = new RestEndpoint({ | ||
path: '/:group/posts', | ||
searchParams: {} as { orderBy?: string; author?: string }, | ||
schema: new schema.Query( | ||
new schema.Collection([Post], { | ||
nonFilterArgumentKeys: /orderBy/, | ||
}), | ||
(posts, { orderBy } = {}) => { | ||
if (orderBy) { | ||
return [...posts].sort((a, b) => | ||
a[orderBy].localeCompare(b[orderBy]), | ||
); | ||
} | ||
return posts; | ||
}, | ||
), | ||
}); | ||
``` | ||
|
||
```tsx title="NewPost" collapsed | ||
import { useLoading } from '@data-client/react'; | ||
import { getPosts } from './getPosts'; | ||
|
||
export default function NewPost({ author }: Props) { | ||
const ctrl = useController(); | ||
|
||
const [handlePress, loading] = useLoading(async e => { | ||
if (e.key === 'Enter') { | ||
const title = e.currentTarget.value; | ||
e.currentTarget.value = ''; | ||
await ctrl.fetch( | ||
getPosts.push, | ||
{ group: 'react' }, | ||
{ | ||
title, | ||
author, | ||
}, | ||
); | ||
} | ||
}); | ||
|
||
return ( | ||
<div> | ||
<input type="text" onKeyDown={handlePress} /> | ||
{loading ? ' ...' : ''} | ||
</div> | ||
); | ||
} | ||
interface Props { | ||
author: string; | ||
} | ||
``` | ||
|
||
```tsx title="PostList" collapsed {8} | ||
import { useSuspense } from '@data-client/react'; | ||
import { getPosts } from './getPosts'; | ||
import NewPost from './NewPost'; | ||
|
||
export default function PostList({ author }: Props) { | ||
const posts = useSuspense(getPosts, { | ||
author, | ||
orderBy: 'title', | ||
group: 'react', | ||
}); | ||
return ( | ||
<div> | ||
{posts.map(post => ( | ||
<div key={post.pk()}>{post.title}</div> | ||
))} | ||
<NewPost author={author} /> | ||
</div> | ||
); | ||
} | ||
interface Props { | ||
author: string; | ||
} | ||
``` | ||
|
||
```tsx title="UserList" collapsed | ||
import PostList from './PostList'; | ||
|
||
function UserList() { | ||
const users = ['bob', 'clara']; | ||
return ( | ||
<div> | ||
{users.map(user => ( | ||
<section key={user}> | ||
<h3>{user}</h3> | ||
<PostList author={user} /> | ||
</section> | ||
))} | ||
</div> | ||
); | ||
} | ||
render(<UserList />); | ||
``` | ||
|
||
</HooksPlayground> |
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