1
- import type { Automation } from 'sentry/types/workflowEngine/automations' ;
2
- import { useApiQuery } from 'sentry/utils/queryClient' ;
1
+ import type { Automation , NewAutomation } from 'sentry/types/workflowEngine/automations' ;
2
+ import { useApiQuery , useMutation , useQueryClient } from 'sentry/utils/queryClient' ;
3
+ import useApi from 'sentry/utils/useApi' ;
3
4
import useOrganization from 'sentry/utils/useOrganization' ;
4
5
5
6
interface UseAutomationsQueryOptions {
@@ -14,3 +15,56 @@ export function useAutomationsQuery(_options: UseAutomationsQueryOptions = {}) {
14
15
retry : false ,
15
16
} ) ;
16
17
}
18
+
19
+ export const makeAutomationQueryKey = (
20
+ orgSlug : string ,
21
+ automationId = ''
22
+ ) : [ url : string ] => [ `/organizations/${ orgSlug } /workflows/${ automationId } /` ] ;
23
+
24
+ export function useCreateAutomation ( automation : NewAutomation ) {
25
+ const org = useOrganization ( ) ;
26
+
27
+ return useApiQuery < Automation [ ] > (
28
+ [ ...makeAutomationQueryKey ( org . slug ) , { method : 'POST' , data : automation } ] ,
29
+ {
30
+ staleTime : 0 ,
31
+ retry : false ,
32
+ }
33
+ ) ;
34
+ }
35
+
36
+ export function useAutomationQuery ( automationId : string ) {
37
+ const org = useOrganization ( ) ;
38
+
39
+ return useApiQuery < Automation > ( [ ...makeAutomationQueryKey ( org . slug , automationId ) ] , {
40
+ staleTime : 0 ,
41
+ retry : false ,
42
+ } ) ;
43
+ }
44
+
45
+ export function useAutomationMutation ( automation : Partial < Automation > & { id : string } ) {
46
+ const api = useApi ( { persistInFlight : true } ) ;
47
+ const queryClient = useQueryClient ( ) ;
48
+ const org = useOrganization ( ) ;
49
+ const queryKey = makeAutomationQueryKey ( org . slug , automation . id ) ;
50
+ return useMutation < Automation > ( {
51
+ mutationFn : data =>
52
+ api . requestPromise ( queryKey [ 0 ] , {
53
+ method : 'PUT' ,
54
+ data,
55
+ } ) ,
56
+ onSuccess : _ => {
57
+ queryClient . invalidateQueries ( { queryKey} ) ;
58
+ // setApiQueryData<Project>(
59
+ // queryClient,
60
+ // makeDetailedProjectQueryKey({
61
+ // orgSlug: organization.slug,
62
+ // projectSlug: project.slug,
63
+ // }),
64
+ // existingData => (updatedProject ? updatedProject : existingData)
65
+ // );
66
+ // return onSuccess?.(updatedProject);
67
+ } ,
68
+ onError : _ => { } ,
69
+ } ) ;
70
+ }
0 commit comments