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'
3
5
import z from 'zod'
4
6
5
7
import { useAddNotification } from 'services/toastNotification'
6
8
import Api from 'shared/api'
7
- import { NetworkErrorObject } from 'shared/api/helpers'
9
+ import { Provider , rejectNetworkError } from 'shared/api/helpers'
8
10
import A from 'ui/A'
9
11
10
12
import { OktaConfigQueryOpts } from '../queries/OktaConfigQueryOpts'
11
13
12
14
const TOAST_DURATION = 10000
13
15
14
16
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
31
29
}
32
30
}
33
31
}
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
+ ] )
35
48
36
49
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 ( ) ,
57
51
} )
58
52
59
53
export const SaveOktaConfigMessage = ( ) => (
@@ -67,7 +61,7 @@ export const SaveOktaConfigMessage = () => (
67
61
)
68
62
69
63
interface URLParams {
70
- provider : string
64
+ provider : Provider
71
65
owner : string
72
66
}
73
67
@@ -83,7 +77,7 @@ export const useUpdateOktaConfig = ({ provider, owner }: URLParams) => {
83
77
const addToast = useAddNotification ( )
84
78
const queryClientV5 = useQueryClientV5 ( )
85
79
86
- return useMutation ( {
80
+ return useMutationV5 ( {
87
81
mutationFn : ( {
88
82
clientId,
89
83
clientSecret,
@@ -110,26 +104,21 @@ export const useUpdateOktaConfig = ({ provider, owner }: URLParams) => {
110
104
onSuccess : ( { data } ) => {
111
105
const parsedData = ResponseSchema . safeParse ( data )
112
106
if ( ! parsedData . success ) {
113
- return Promise . reject ( {
107
+ return rejectNetworkError ( {
114
108
status : 404 ,
115
109
data : { } ,
116
110
dev : 'useUpdateOktaConfig - 404 failed to parse' ,
117
- } satisfies NetworkErrorObject )
111
+ error : parsedData . error ,
112
+ } )
118
113
}
119
114
120
115
const error = parsedData . data . saveOktaConfig ?. error
121
116
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
+ } )
133
122
} else {
134
123
addToast ( {
135
124
type : 'success' ,
0 commit comments