1- import type { EnterpriseConnectionResource } from '../../types/enterpriseConnection' ;
1+ import { useCallback } from 'react' ;
2+
3+ import type { DeletedObjectResource } from '../../types/deletedObject' ;
4+ import type {
5+ CreateMeEnterpriseConnectionParams ,
6+ EnterpriseConnectionResource ,
7+ UpdateMeEnterpriseConnectionParams ,
8+ } from '../../types/enterpriseConnection' ;
29import { defineKeepPreviousDataFn } from '../clerk-rq/keep-previous-data' ;
10+ import { useClerkQueryClient } from '../clerk-rq/use-clerk-query-client' ;
311import { useClerkQuery } from '../clerk-rq/useQuery' ;
412import { useClerkInstanceContext } from '../contexts' ;
513import { useUserBase } from './base/useUserBase' ;
@@ -17,6 +25,15 @@ export type UseUserEnterpriseConnectionsReturn = {
1725 error : Error | null ;
1826 isLoading : boolean ;
1927 isFetching : boolean ;
28+ createEnterpriseConnection : (
29+ params : CreateMeEnterpriseConnectionParams ,
30+ ) => Promise < EnterpriseConnectionResource | undefined > ;
31+ updateEnterpriseConnection : (
32+ enterpriseConnectionId : string ,
33+ params : UpdateMeEnterpriseConnectionParams ,
34+ ) => Promise < EnterpriseConnectionResource | undefined > ;
35+ deleteEnterpriseConnection : ( enterpriseConnectionId : string ) => Promise < DeletedObjectResource | undefined > ;
36+ revalidate : ( ) => Promise < void > ;
2037} ;
2138
2239/**
@@ -30,6 +47,7 @@ function useUserEnterpriseConnections(
3047 const { keepPreviousData = true , enabled = true , withOrganizationAccountLinking = false } = params ;
3148 const clerk = useClerkInstanceContext ( ) ;
3249 const user = useUserBase ( ) ;
50+ const [ queryClient ] = useClerkQueryClient ( ) ;
3351
3452 const { queryKey, stableKey, authenticated } = useUserEnterpriseConnectionsCacheKeys ( {
3553 userId : user ?. id ?? null ,
@@ -51,11 +69,47 @@ function useUserEnterpriseConnections(
5169 placeholderData : defineKeepPreviousDataFn ( keepPreviousData ) ,
5270 } ) ;
5371
72+ const revalidate = useCallback (
73+ ( ) => queryClient . invalidateQueries ( { queryKey : [ stableKey ] } ) ,
74+ [ queryClient , stableKey ] ,
75+ ) ;
76+
77+ const createEnterpriseConnection = useCallback (
78+ async ( createParams : CreateMeEnterpriseConnectionParams ) => {
79+ const created = await user ?. createEnterpriseConnection ( createParams ) ;
80+ await revalidate ( ) ;
81+ return created ;
82+ } ,
83+ [ user , revalidate ] ,
84+ ) ;
85+
86+ const updateEnterpriseConnection = useCallback (
87+ async ( enterpriseConnectionId : string , updateParams : UpdateMeEnterpriseConnectionParams ) => {
88+ const updated = await user ?. updateEnterpriseConnection ( enterpriseConnectionId , updateParams ) ;
89+ await revalidate ( ) ;
90+ return updated ;
91+ } ,
92+ [ user , revalidate ] ,
93+ ) ;
94+
95+ const deleteEnterpriseConnection = useCallback (
96+ async ( enterpriseConnectionId : string ) => {
97+ const deleted = await user ?. deleteEnterpriseConnection ( enterpriseConnectionId ) ;
98+ await revalidate ( ) ;
99+ return deleted ;
100+ } ,
101+ [ user , revalidate ] ,
102+ ) ;
103+
54104 return {
55105 data : query . data ,
56106 error : query . error ?? null ,
57107 isLoading : query . isLoading ,
58108 isFetching : query . isFetching ,
109+ createEnterpriseConnection,
110+ updateEnterpriseConnection,
111+ deleteEnterpriseConnection,
112+ revalidate,
59113 } ;
60114}
61115
0 commit comments