Skip to content

Commit 47e6e13

Browse files
ref: Migrate useUpdateOktaConfig to TSQ V5 (#3697)
1 parent 71c0a5b commit 47e6e13

File tree

4 files changed

+50
-68
lines changed

4 files changed

+50
-68
lines changed

src/pages/AccountSettings/tabs/OktaAccess/OktaConfigForm/OktaConfigForm.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { SubmitHandler, useForm } from 'react-hook-form'
55
import { useParams } from 'react-router-dom'
66
import { z } from 'zod'
77

8+
import { Provider } from 'shared/api/helpers'
89
import Banner from 'ui/Banner'
910
import BannerContent from 'ui/Banner/BannerContent'
1011
import Button from 'ui/Button'
@@ -13,7 +14,7 @@ import Icon from 'ui/Icon'
1314
import TextInput from 'ui/TextInput'
1415
import Toggle from 'ui/Toggle'
1516

16-
import { useUpdateOktaConfig } from '../hooks'
17+
import { useUpdateOktaConfig } from '../hooks/useUpdateOktaConfig'
1718
import { OktaConfigQueryOpts } from '../queries/OktaConfigQueryOpts'
1819

1920
const FormSchema = z.object({
@@ -24,7 +25,7 @@ const FormSchema = z.object({
2425

2526
type FormValues = z.infer<typeof FormSchema>
2627
interface URLParams {
27-
provider: string
28+
provider: Provider
2829
owner: string
2930
}
3031

src/pages/AccountSettings/tabs/OktaAccess/hooks/index.ts

-1
This file was deleted.

src/pages/AccountSettings/tabs/OktaAccess/hooks/useUpdateOktaConfig.test.tsx

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
21
import {
32
QueryClientProvider as QueryClientProviderV5,
43
QueryClient as QueryClientV5,
@@ -19,9 +18,6 @@ import {
1918
vi.mock('services/toastNotification')
2019
const mockedToastNotification = useAddNotification as Mock
2120

22-
const queryClient = new QueryClient({
23-
defaultOptions: { queries: { retry: false } },
24-
})
2521
const queryClientV5 = new QueryClientV5({
2622
defaultOptions: { queries: { retry: false } },
2723
})
@@ -30,11 +26,9 @@ const wrapper =
3026
(initialEntries = '/gh/codecov'): React.FC<React.PropsWithChildren> =>
3127
({ children }) => (
3228
<QueryClientProviderV5 client={queryClientV5}>
33-
<QueryClientProvider client={queryClient}>
34-
<MemoryRouter initialEntries={[initialEntries]}>
35-
<Route path="/:provider/:owner">{children}</Route>
36-
</MemoryRouter>
37-
</QueryClientProvider>
29+
<MemoryRouter initialEntries={[initialEntries]}>
30+
<Route path="/:provider/:owner">{children}</Route>
31+
</MemoryRouter>
3832
</QueryClientProviderV5>
3933
)
4034

@@ -57,7 +51,6 @@ beforeAll(() => {
5751
})
5852

5953
afterEach(() => {
60-
queryClient.clear()
6154
queryClientV5.clear()
6255
server.resetHandlers()
6356
})

src/pages/AccountSettings/tabs/OktaAccess/hooks/useUpdateOktaConfig.tsx

+44-55
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,53 @@
1-
import { useMutation } from '@tanstack/react-query'
2-
import { useQueryClient as useQueryClientV5 } from '@tanstack/react-queryV5'
1+
import {
2+
useMutation as useMutationV5,
3+
useQueryClient as useQueryClientV5,
4+
} from '@tanstack/react-queryV5'
35
import z from 'zod'
46

57
import { useAddNotification } from 'services/toastNotification'
68
import Api from 'shared/api'
7-
import { NetworkErrorObject } from 'shared/api/helpers'
9+
import { Provider, rejectNetworkError } from 'shared/api/helpers'
810
import A from 'ui/A'
911

1012
import { OktaConfigQueryOpts } from '../queries/OktaConfigQueryOpts'
1113

1214
const TOAST_DURATION = 10000
1315

1416
const query = `
15-
mutation SaveOktaConfig($input: SaveOktaConfigInput!) {
16-
saveOktaConfig(input: $input) {
17-
error {
18-
__typename
19-
... on UnauthorizedError {
20-
message
21-
__typename
22-
}
23-
... on ValidationError {
24-
message
25-
__typename
26-
}
27-
... on UnauthenticatedError {
28-
__typename
29-
message
30-
}
17+
mutation SaveOktaConfig($input: SaveOktaConfigInput!) {
18+
saveOktaConfig(input: $input) {
19+
error {
20+
__typename
21+
... on UnauthorizedError {
22+
message
23+
}
24+
... on ValidationError {
25+
message
26+
}
27+
... on UnauthenticatedError {
28+
message
3129
}
3230
}
3331
}
34-
`
32+
}`
33+
34+
const ErrorUnionSchema = z.discriminatedUnion('__typename', [
35+
z.object({
36+
__typename: z.literal('UnauthorizedError'),
37+
message: z.string(),
38+
}),
39+
z.object({
40+
__typename: z.literal('ValidationError'),
41+
message: z.string(),
42+
}),
43+
z.object({
44+
__typename: z.literal('UnauthenticatedError'),
45+
message: z.string(),
46+
}),
47+
])
3548

3649
const ResponseSchema = z.object({
37-
saveOktaConfig: z
38-
.object({
39-
error: z
40-
.union([
41-
z.object({
42-
__typename: z.literal('UnauthorizedError'),
43-
message: z.string(),
44-
}),
45-
z.object({
46-
__typename: z.literal('ValidationError'),
47-
message: z.string(),
48-
}),
49-
z.object({
50-
__typename: z.literal('UnauthenticatedError'),
51-
message: z.string(),
52-
}),
53-
])
54-
.nullable(),
55-
})
56-
.nullable(),
50+
saveOktaConfig: z.object({ error: ErrorUnionSchema.nullable() }).nullable(),
5751
})
5852

5953
export const SaveOktaConfigMessage = () => (
@@ -67,7 +61,7 @@ export const SaveOktaConfigMessage = () => (
6761
)
6862

6963
interface URLParams {
70-
provider: string
64+
provider: Provider
7165
owner: string
7266
}
7367

@@ -83,7 +77,7 @@ export const useUpdateOktaConfig = ({ provider, owner }: URLParams) => {
8377
const addToast = useAddNotification()
8478
const queryClientV5 = useQueryClientV5()
8579

86-
return useMutation({
80+
return useMutationV5({
8781
mutationFn: ({
8882
clientId,
8983
clientSecret,
@@ -110,26 +104,21 @@ export const useUpdateOktaConfig = ({ provider, owner }: URLParams) => {
110104
onSuccess: ({ data }) => {
111105
const parsedData = ResponseSchema.safeParse(data)
112106
if (!parsedData.success) {
113-
return Promise.reject({
107+
return rejectNetworkError({
114108
status: 404,
115109
data: {},
116110
dev: 'useUpdateOktaConfig - 404 failed to parse',
117-
} satisfies NetworkErrorObject)
111+
error: parsedData.error,
112+
})
118113
}
119114

120115
const error = parsedData.data.saveOktaConfig?.error
121116
if (error) {
122-
if (
123-
error.__typename === 'ValidationError' ||
124-
error.__typename === 'UnauthorizedError' ||
125-
error.__typename === 'UnauthenticatedError'
126-
) {
127-
addToast({
128-
type: 'error',
129-
text: <SaveOktaConfigMessage />,
130-
disappearAfter: TOAST_DURATION,
131-
})
132-
}
117+
addToast({
118+
type: 'error',
119+
text: <SaveOktaConfigMessage />,
120+
disappearAfter: TOAST_DURATION,
121+
})
133122
} else {
134123
addToast({
135124
type: 'success',

0 commit comments

Comments
 (0)