Skip to content

Commit 2314dbd

Browse files
authored
🆙 Update React Query to TanStack Query (fastapi#1153)
1 parent 3628e03 commit 2314dbd

File tree

19 files changed

+225
-517
lines changed

19 files changed

+225
-517
lines changed

‎frontend/package-lock.json‎

Lines changed: 78 additions & 370 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎frontend/package.json‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@
1515
"@chakra-ui/react": "2.8.2",
1616
"@emotion/react": "11.11.3",
1717
"@emotion/styled": "11.11.0",
18+
"@tanstack/react-query": "^5.28.14",
19+
"@tanstack/react-query-devtools": "^5.28.14",
1820
"@tanstack/react-router": "1.19.1",
1921
"axios": "1.6.2",
2022
"form-data": "4.0.0",
2123
"framer-motion": "10.16.16",
2224
"react": "^18.2.0",
2325
"react-dom": "^18.2.0",
2426
"react-hook-form": "7.49.3",
25-
"react-icons": "5.0.1",
26-
"react-query": "3.39.3"
27+
"react-icons": "5.0.1"
2728
},
2829
"devDependencies": {
2930
"@biomejs/biome": "1.6.1",

‎frontend/src/components/Admin/AddUser.tsx‎

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
ModalOverlay,
1616
} from "@chakra-ui/react"
1717
import { type SubmitHandler, useForm } from "react-hook-form"
18-
import { useMutation, useQueryClient } from "react-query"
18+
import { useMutation, useQueryClient } from "@tanstack/react-query"
1919

2020
import { type UserCreate, UsersService } from "../../client"
2121
import type { ApiError } from "../../client/core/ApiError"
@@ -53,23 +53,22 @@ const AddUser = ({ isOpen, onClose }: AddUserProps) => {
5353
},
5454
})
5555

56-
const mutation = useMutation(
57-
(data: UserCreate) => UsersService.createUser({ requestBody: data }),
58-
{
59-
onSuccess: () => {
60-
showToast("Success!", "User created successfully.", "success")
61-
reset()
62-
onClose()
63-
},
64-
onError: (err: ApiError) => {
65-
const errDetail = (err.body as any)?.detail
66-
showToast("Something went wrong.", `${errDetail}`, "error")
67-
},
68-
onSettled: () => {
69-
queryClient.invalidateQueries("users")
70-
},
56+
const mutation = useMutation({
57+
mutationFn: (data: UserCreate) =>
58+
UsersService.createUser({ requestBody: data }),
59+
onSuccess: () => {
60+
showToast("Success!", "User created successfully.", "success")
61+
reset()
62+
onClose()
7163
},
72-
)
64+
onError: (err: ApiError) => {
65+
const errDetail = (err.body as any)?.detail
66+
showToast("Something went wrong.", `${errDetail}`, "error")
67+
},
68+
onSettled: () => {
69+
queryClient.invalidateQueries({ queryKey: ["users"] })
70+
},
71+
})
7372

7473
const onSubmit: SubmitHandler<UserCreateForm> = (data) => {
7574
mutation.mutate(data)

‎frontend/src/components/Admin/EditUser.tsx‎

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
ModalOverlay,
1616
} from "@chakra-ui/react"
1717
import { type SubmitHandler, useForm } from "react-hook-form"
18-
import { useMutation, useQueryClient } from "react-query"
18+
import { useMutation, useQueryClient } from "@tanstack/react-query"
1919

2020
import {
2121
type ApiError,
@@ -52,23 +52,21 @@ const EditUser = ({ user, isOpen, onClose }: EditUserProps) => {
5252
defaultValues: user,
5353
})
5454

55-
const mutation = useMutation(
56-
(data: UserUpdateForm) =>
55+
const mutation = useMutation({
56+
mutationFn: (data: UserUpdateForm) =>
5757
UsersService.updateUser({ userId: user.id, requestBody: data }),
58-
{
59-
onSuccess: () => {
60-
showToast("Success!", "User updated successfully.", "success")
61-
onClose()
62-
},
63-
onError: (err: ApiError) => {
64-
const errDetail = (err.body as any)?.detail
65-
showToast("Something went wrong.", `${errDetail}`, "error")
66-
},
67-
onSettled: () => {
68-
queryClient.invalidateQueries("users")
69-
},
58+
onSuccess: () => {
59+
showToast("Success!", "User updated successfully.", "success")
60+
onClose()
7061
},
71-
)
62+
onError: (err: ApiError) => {
63+
const errDetail = (err.body as any)?.detail
64+
showToast("Something went wrong.", `${errDetail}`, "error")
65+
},
66+
onSettled: () => {
67+
queryClient.invalidateQueries({ queryKey: ["users"] })
68+
},
69+
})
7270

7371
const onSubmit: SubmitHandler<UserUpdateForm> = async (data) => {
7472
if (data.password === "") {

‎frontend/src/components/Common/DeleteAlert.tsx‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from "@chakra-ui/react"
1010
import React from "react"
1111
import { useForm } from "react-hook-form"
12-
import { useMutation, useQueryClient } from "react-query"
12+
import { useMutation, useQueryClient } from "@tanstack/react-query"
1313

1414
import { ItemsService, UsersService } from "../../client"
1515
import useCustomToast from "../../hooks/useCustomToast"
@@ -40,7 +40,8 @@ const Delete = ({ type, id, isOpen, onClose }: DeleteProps) => {
4040
}
4141
}
4242

43-
const mutation = useMutation(deleteEntity, {
43+
const mutation = useMutation({
44+
mutationFn: deleteEntity,
4445
onSuccess: () => {
4546
showToast(
4647
"Success",
@@ -57,7 +58,9 @@ const Delete = ({ type, id, isOpen, onClose }: DeleteProps) => {
5758
)
5859
},
5960
onSettled: () => {
60-
queryClient.invalidateQueries(type === "Item" ? "items" : "users")
61+
queryClient.invalidateQueries({
62+
queryKey: [type === "Item" ? "items" : "users"],
63+
})
6164
},
6265
})
6366

‎frontend/src/components/Common/Sidebar.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
useDisclosure,
1414
} from "@chakra-ui/react"
1515
import { FiLogOut, FiMenu } from "react-icons/fi"
16-
import { useQueryClient } from "react-query"
16+
import { useQueryClient } from "@tanstack/react-query"
1717

1818
import Logo from "../../assets/images/fastapi-logo.svg"
1919
import type { UserOut } from "../../client"
@@ -25,7 +25,7 @@ const Sidebar = () => {
2525
const bgColor = useColorModeValue("ui.light", "ui.dark")
2626
const textColor = useColorModeValue("ui.dark", "ui.light")
2727
const secBgColor = useColorModeValue("ui.secondary", "ui.darkSlate")
28-
const currentUser = queryClient.getQueryData<UserOut>("currentUser")
28+
const currentUser = queryClient.getQueryData<UserOut>(["currentUser"])
2929
const { isOpen, onOpen, onClose } = useDisclosure()
3030
const { logout } = useAuth()
3131

‎frontend/src/components/Common/SidebarItems.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Box, Flex, Icon, Text, useColorModeValue } from "@chakra-ui/react"
22
import { Link } from "@tanstack/react-router"
33
import { FiBriefcase, FiHome, FiSettings, FiUsers } from "react-icons/fi"
4-
import { useQueryClient } from "react-query"
4+
import { useQueryClient } from "@tanstack/react-query"
55

66
import type { UserOut } from "../../client"
77

@@ -19,7 +19,7 @@ const SidebarItems = ({ onClose }: SidebarItemsProps) => {
1919
const queryClient = useQueryClient()
2020
const textColor = useColorModeValue("ui.main", "ui.light")
2121
const bgActive = useColorModeValue("#E2E8F0", "#4A5568")
22-
const currentUser = queryClient.getQueryData<UserOut>("currentUser")
22+
const currentUser = queryClient.getQueryData<UserOut>(["currentUser"])
2323

2424
const finalItems = currentUser?.is_superuser
2525
? [...items, { icon: FiUsers, title: "Admin", path: "/admin" }]

‎frontend/src/components/Items/AddItem.tsx‎

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
ModalOverlay,
1414
} from "@chakra-ui/react"
1515
import { type SubmitHandler, useForm } from "react-hook-form"
16-
import { useMutation, useQueryClient } from "react-query"
16+
import { useMutation, useQueryClient } from "@tanstack/react-query"
1717

1818
import { type ApiError, type ItemCreate, ItemsService } from "../../client"
1919
import useCustomToast from "../../hooks/useCustomToast"
@@ -40,23 +40,22 @@ const AddItem = ({ isOpen, onClose }: AddItemProps) => {
4040
},
4141
})
4242

43-
const mutation = useMutation(
44-
(data: ItemCreate) => ItemsService.createItem({ requestBody: data }),
45-
{
46-
onSuccess: () => {
47-
showToast("Success!", "Item created successfully.", "success")
48-
reset()
49-
onClose()
50-
},
51-
onError: (err: ApiError) => {
52-
const errDetail = (err.body as any)?.detail
53-
showToast("Something went wrong.", `${errDetail}`, "error")
54-
},
55-
onSettled: () => {
56-
queryClient.invalidateQueries("items")
57-
},
43+
const mutation = useMutation({
44+
mutationFn: (data: ItemCreate) =>
45+
ItemsService.createItem({ requestBody: data }),
46+
onSuccess: () => {
47+
showToast("Success!", "Item created successfully.", "success")
48+
reset()
49+
onClose()
5850
},
59-
)
51+
onError: (err: ApiError) => {
52+
const errDetail = (err.body as any)?.detail
53+
showToast("Something went wrong.", `${errDetail}`, "error")
54+
},
55+
onSettled: () => {
56+
queryClient.invalidateQueries({ queryKey: ["items"] })
57+
},
58+
})
6059

6160
const onSubmit: SubmitHandler<ItemCreate> = (data) => {
6261
mutation.mutate(data)

‎frontend/src/components/Items/EditItem.tsx‎

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
ModalOverlay,
1414
} from "@chakra-ui/react"
1515
import { type SubmitHandler, useForm } from "react-hook-form"
16-
import { useMutation, useQueryClient } from "react-query"
16+
import { useMutation, useQueryClient } from "@tanstack/react-query"
1717

1818
import {
1919
type ApiError,
@@ -43,23 +43,21 @@ const EditItem = ({ item, isOpen, onClose }: EditItemProps) => {
4343
defaultValues: item,
4444
})
4545

46-
const mutation = useMutation(
47-
(data: ItemUpdate) =>
46+
const mutation = useMutation({
47+
mutationFn: (data: ItemUpdate) =>
4848
ItemsService.updateItem({ id: item.id, requestBody: data }),
49-
{
50-
onSuccess: () => {
51-
showToast("Success!", "Item updated successfully.", "success")
52-
onClose()
53-
},
54-
onError: (err: ApiError) => {
55-
const errDetail = (err.body as any)?.detail
56-
showToast("Something went wrong.", `${errDetail}`, "error")
57-
},
58-
onSettled: () => {
59-
queryClient.invalidateQueries("items")
60-
},
49+
onSuccess: () => {
50+
showToast("Success!", "Item updated successfully.", "success")
51+
onClose()
6152
},
62-
)
53+
onError: (err: ApiError) => {
54+
const errDetail = (err.body as any)?.detail
55+
showToast("Something went wrong.", `${errDetail}`, "error")
56+
},
57+
onSettled: () => {
58+
queryClient.invalidateQueries({ queryKey: ["items"] })
59+
},
60+
})
6361

6462
const onSubmit: SubmitHandler<ItemUpdate> = async (data) => {
6563
mutation.mutate(data)

‎frontend/src/components/UserSettings/ChangePassword.tsx‎

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
useColorModeValue,
1111
} from "@chakra-ui/react"
1212
import { type SubmitHandler, useForm } from "react-hook-form"
13-
import { useMutation } from "react-query"
13+
import { useMutation } from "@tanstack/react-query"
1414

1515
import { type ApiError, type UpdatePassword, UsersService } from "../../client"
1616
import useCustomToast from "../../hooks/useCustomToast"
@@ -34,20 +34,18 @@ const ChangePassword = () => {
3434
criteriaMode: "all",
3535
})
3636

37-
const mutation = useMutation(
38-
(data: UpdatePassword) =>
37+
const mutation = useMutation({
38+
mutationFn: (data: UpdatePassword) =>
3939
UsersService.updatePasswordMe({ requestBody: data }),
40-
{
41-
onSuccess: () => {
42-
showToast("Success!", "Password updated.", "success")
43-
reset()
44-
},
45-
onError: (err: ApiError) => {
46-
const errDetail = (err.body as any)?.detail
47-
showToast("Something went wrong.", `${errDetail}`, "error")
48-
},
40+
onSuccess: () => {
41+
showToast("Success!", "Password updated.", "success")
42+
reset()
4943
},
50-
)
44+
onError: (err: ApiError) => {
45+
const errDetail = (err.body as any)?.detail
46+
showToast("Something went wrong.", `${errDetail}`, "error")
47+
},
48+
})
5149

5250
const onSubmit: SubmitHandler<UpdatePasswordForm> = async (data) => {
5351
mutation.mutate(data)

0 commit comments

Comments
 (0)