-
-
Notifications
You must be signed in to change notification settings - Fork 817
Fix/4407 set query data use inifinite query #4413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
siddhsuresh
merged 9 commits into
blitz-js:main
from
Daidalos117:fix/4407-setQueryData-useInifiniteQuery
Feb 7, 2025
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f1c4eeb
fix(4407): setQueryData with useInifiniteQuery
81cdd52
Merge branch 'refs/heads/main' into fix/4407-setQueryData-useInifinit…
ef09ea2
chore(4407): merge develop
2f3473c
fix(4407): typing
a0d7800
chore(4407): add changeset
ac85a12
chore(4407): remove prefetch on infninite mutate page
5d090de
chore(4407): recreate changeset
eca928e
fix(4407): mutating data in infinite
5dbd31d
fix(4407): remove web from changeset
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,6 @@ | ||
| --- | ||
| "web": patch | ||
| "@blitzjs/rpc": patch | ||
| --- | ||
|
|
||
| fix(4407): setQueryData with useInfiniteQuery | ||
This file contains hidden or 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,90 @@ | ||
| import {useSuspenseInfiniteQuery} from "@blitzjs/rpc" | ||
| import getInfiniteUsers from "src/queries/getInfiniteUsers" | ||
| import {useActionState} from "react" | ||
|
|
||
| function PageWithInfiniteQueryMutate(props) { | ||
| const [usersPages, extraInfo] = useSuspenseInfiniteQuery( | ||
| getInfiniteUsers, | ||
| (page = {take: 3, skip: 0}) => page, | ||
| { | ||
| getNextPageParam: (lastPage) => lastPage.nextPage, | ||
| initialPageParam: {take: 3, skip: 0}, | ||
| }, | ||
| ) | ||
| const {isFetchingNextPage, fetchNextPage, hasNextPage, setQueryData} = extraInfo | ||
|
|
||
| const onOnContactSave = async (previousState, formData: FormData) => { | ||
| const name = formData.get("name") as string | null | ||
|
|
||
| await setQueryData( | ||
| (oldData) => { | ||
| if (!oldData) { | ||
| return { | ||
| pages: [], | ||
| pageParams: [], | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| ...oldData, | ||
| pages: oldData.pages.map((page, index) => { | ||
| if (index === 0) { | ||
| return { | ||
| ...page, | ||
| users: [ | ||
| { | ||
| id: Math.random(), | ||
| name, | ||
| role: "user", | ||
| email: `${name}@yopmail.com`, | ||
| createdAt: new Date(), | ||
| updatedAt: new Date(), | ||
| hashedPassword: "alsdklaskdoaskdokdo", | ||
| }, | ||
| ...page.users, | ||
| ], | ||
| } | ||
| } | ||
| return page | ||
| }), | ||
| } | ||
| }, | ||
| {refetch: false}, | ||
| ) | ||
| } | ||
|
|
||
| const [, formAction] = useActionState(onOnContactSave, {name: ""}) | ||
|
|
||
| return ( | ||
| <div> | ||
| <form action={formAction}> | ||
| <input type="text" name="name" placeholder="User name" /> | ||
| <button type="submit">Add user</button> | ||
| </form> | ||
| {usersPages.map((usersPage) => ( | ||
| <> | ||
| {usersPage?.users.map((u) => ( | ||
| <div key={u.name}> | ||
| <p>name: {u.name}</p> | ||
| <p>role: {u.role}</p> | ||
| <p>email: {u.email}</p> | ||
| <hr /> | ||
| </div> | ||
| ))} | ||
|
|
||
| {usersPage.hasMore && ( | ||
| <button onClick={() => fetchNextPage()} disabled={!hasNextPage || !!isFetchingNextPage}> | ||
| {isFetchingNextPage | ||
| ? "Loading more..." | ||
| : hasNextPage | ||
| ? "Load More" | ||
| : "Nothing more to load"} | ||
| </button> | ||
| )} | ||
| </> | ||
| ))} | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| export default PageWithInfiniteQueryMutate |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.