@@ -14,7 +14,7 @@ import CodeEditor from 'components/ui/CodeEditor/CodeEditor'
1414import FunctionSelector from 'components/ui/FunctionSelector'
1515import SchemaSelector from 'components/ui/SchemaSelector'
1616import { AuthConfigResponse } from 'data/auth/auth-config-query'
17- import { useAuthHooksUpdateMutation } from 'data/auth/auth-hooks -update-mutation'
17+ import { useAuthConfigUpdateMutation } from 'data/auth/auth-config -update-mutation'
1818import { executeSql } from 'data/sql/execute-sql-query'
1919import { useFlag } from 'hooks/ui/useFlag'
2020import {
@@ -42,10 +42,10 @@ import { extractMethod, getRevokePermissionStatements, isValidHook } from './hoo
4242
4343interface CreateHookSheetProps {
4444 visible : boolean
45- title : HOOK_DEFINITION_TITLE | null
46- authConfig : AuthConfigResponse
4745 onClose : ( ) => void
4846 onDelete : ( ) => void
47+ title : HOOK_DEFINITION_TITLE | null
48+ authConfig : AuthConfigResponse
4949}
5050
5151export function generateAuthHookSecret ( ) {
@@ -109,13 +109,14 @@ const FormSchema = z
109109
110110export const CreateHookSheet = ( {
111111 visible,
112- title,
113- authConfig,
114112 onClose,
115113 onDelete,
114+ title,
115+ authConfig,
116116} : CreateHookSheetProps ) => {
117117 const { ref : projectRef } = useParams ( )
118118 const { project } = useProjectContext ( )
119+ const { mutate : updateAuthConfig , isLoading : isUpdatingConfig } = useAuthConfigUpdateMutation ( )
119120 const httpsAuthHooksEnabled = useFlag ( 'httpsAuthHooksEnabled' )
120121
121122 const definition = useMemo (
@@ -154,6 +155,45 @@ export const CreateHookSheet = ({
154155 } ,
155156 } )
156157
158+ useEffect ( ( ) => {
159+ if ( visible ) {
160+ if ( definition ) {
161+ const values = extractMethod (
162+ authConfig ?. [ definition . uriKey ] || '' ,
163+ authConfig ?. [ definition . secretsKey ] || ''
164+ )
165+
166+ form . reset ( {
167+ hookType : definition . title ,
168+ enabled : authConfig ?. [ definition . enabledKey ] || false ,
169+ selectedType : values . type ,
170+ httpsValues : {
171+ url : ( values . type === 'https' && values . url ) || '' ,
172+ secret : ( values . type === 'https' && values . secret ) || '' ,
173+ } ,
174+ postgresValues : {
175+ schema : ( values . type === 'postgres' && values . schema ) || 'public' ,
176+ functionName : ( values . type === 'postgres' && values . functionName ) || '' ,
177+ } ,
178+ } )
179+ } else {
180+ form . reset ( {
181+ hookType : title || '' ,
182+ enabled : true ,
183+ selectedType : 'postgres' ,
184+ httpsValues : {
185+ url : '' ,
186+ secret : '' ,
187+ } ,
188+ postgresValues : {
189+ schema : 'public' ,
190+ functionName : '' ,
191+ } ,
192+ } )
193+ }
194+ }
195+ } , [ authConfig , title , visible , definition ] )
196+
157197 const values = form . watch ( )
158198
159199 const statements = useMemo ( ( ) => {
@@ -182,23 +222,6 @@ export const CreateHookSheet = ({
182222 return permissionChanges
183223 } , [ hook , values . postgresValues . schema , values . postgresValues . functionName ] )
184224
185- const { mutate : updateAuthHooks , isLoading : isUpdatingAuthHooks } = useAuthHooksUpdateMutation ( {
186- onSuccess : ( ) => {
187- toast . success ( `Successfully created ${ values . hookType } .` )
188- if ( statements . length > 0 ) {
189- executeSql ( {
190- projectRef,
191- connectionString : project ! . connectionString ,
192- sql : statements . join ( '\n' ) ,
193- } )
194- }
195- onClose ( )
196- } ,
197- onError : ( error ) => {
198- toast . error ( `Failed to create hook: ${ error . message } ` )
199- } ,
200- } )
201-
202225 const onSubmit : SubmitHandler < z . infer < typeof FormSchema > > = async ( values ) => {
203226 if ( ! project ) return console . error ( 'Project is required' )
204227 const definition = HOOKS_DEFINITIONS . find ( ( d ) => values . hookType === d . title )
@@ -224,47 +247,27 @@ export const CreateHookSheet = ({
224247 [ secretsLabel ] : values . selectedType === 'https' ? values . httpsValues . secret : null ,
225248 }
226249
227- updateAuthHooks ( { projectRef : projectRef ! , config : payload } )
228- }
229-
230- useEffect ( ( ) => {
231- if ( visible ) {
232- if ( definition ) {
233- const values = extractMethod (
234- authConfig ?. [ definition . uriKey ] || '' ,
235- authConfig ?. [ definition . secretsKey ] || ''
236- )
237-
238- form . reset ( {
239- hookType : definition . title ,
240- enabled : authConfig ?. [ definition . enabledKey ] || false ,
241- selectedType : values . type ,
242- httpsValues : {
243- url : ( values . type === 'https' && values . url ) || '' ,
244- secret : ( values . type === 'https' && values . secret ) || '' ,
245- } ,
246- postgresValues : {
247- schema : ( values . type === 'postgres' && values . schema ) || 'public' ,
248- functionName : ( values . type === 'postgres' && values . functionName ) || '' ,
249- } ,
250- } )
251- } else {
252- form . reset ( {
253- hookType : title || '' ,
254- enabled : true ,
255- selectedType : 'postgres' ,
256- httpsValues : {
257- url : '' ,
258- secret : '' ,
259- } ,
260- postgresValues : {
261- schema : 'public' ,
262- functionName : '' ,
263- } ,
264- } )
250+ updateAuthConfig (
251+ { projectRef : projectRef ! , config : payload } ,
252+ {
253+ onSuccess : ( ) => {
254+ toast . success ( `Successfully created ${ values . hookType } .` )
255+ if ( statements . length > 0 ) {
256+ executeSql ( {
257+ projectRef,
258+ connectionString : project . connectionString ,
259+ sql : statements . join ( '\n' ) ,
260+ } )
261+ }
262+ onClose ( )
263+ } ,
264+ onError : ( error ) => {
265+ toast . error ( `Failed to create hook: ${ error . message } ` )
266+ onClose ( )
267+ } ,
265268 }
266- }
267- } , [ authConfig , title , visible , definition ] )
269+ )
270+ }
268271
269272 return (
270273 < Sheet open = { visible } onOpenChange = { ( ) => onClose ( ) } >
@@ -489,16 +492,16 @@ export const CreateHookSheet = ({
489492 </ div >
490493 ) }
491494
492- < Button disabled = { isUpdatingAuthHooks } type = "default" onClick = { ( ) => onClose ( ) } >
495+ < Button disabled = { isUpdatingConfig } type = "default" onClick = { ( ) => onClose ( ) } >
493496 Cancel
494497 </ Button >
495498 < Button
496499 form = { FORM_ID }
497500 htmlType = "submit"
498- disabled = { isUpdatingAuthHooks }
499- loading = { isUpdatingAuthHooks }
501+ disabled = { isUpdatingConfig }
502+ loading = { isUpdatingConfig }
500503 >
501- { isCreating ? 'Create hook ' : 'Update hook ' }
504+ { isCreating ? 'Create' : 'Update' }
502505 </ Button >
503506 </ SheetFooter >
504507 </ SheetContent >
0 commit comments