-
-
Notifications
You must be signed in to change notification settings - Fork 807
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix/4407 set query data use inifinite query (#4413)
* fix(4407): setQueryData with useInifiniteQuery * chore(4407): merge develop * fix(4407): typing * chore(4407): add changeset * chore(4407): remove prefetch on infninite mutate page * chore(4407): recreate changeset * fix(4407): mutating data in infinite * fix(4407): remove web from changeset --------- Co-authored-by: Roman Rajchert <[email protected]>
- Loading branch information
1 parent
ce1a603
commit 11c9f00
Showing
4 changed files
with
113 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@blitzjs/rpc": patch | ||
--- | ||
|
||
fix(4407): setQueryData with useInfiniteQuery |
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,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 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