forked from parseablehq/console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseCorrelations.tsx
121 lines (112 loc) · 4.29 KB
/
useCorrelations.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { useMutation, useQuery } from 'react-query';
import _ from 'lodash';
import { deleteSavedCorrelation, getCorrelations, saveCorrelation, updateCorrelation } from '@/api/correlations';
import { correlationStoreReducers, useCorrelationStore } from '@/pages/Correlation/providers/CorrelationProvider';
import { notifyError, notifySuccess } from '@/utils/notification';
import { AxiosError, isAxiosError } from 'axios';
import { appStoreReducers, useAppStore } from '@/layouts/MainLayout/providers/AppProvider';
const {
setCorrelations,
setActiveCorrelation,
setCorrelationId,
setSavedCorrelationId,
cleanCorrelationStore,
toggleSavedCorrelationsModal,
} = correlationStoreReducers;
const { syncTimeRange } = appStoreReducers;
export const useCorrelationsQuery = () => {
const [{ correlationId }, setCorrelatedStore] = useCorrelationStore((store) => store);
const [, setAppStore] = useAppStore((store) => store);
const {
isError: fetchCorrelationsError,
isSuccess: fetchCorrelationsSuccess,
isLoading: fetchCorrelationsLoading,
refetch: fetchCorrelations,
} = useQuery(['correlations'], () => getCorrelations(), {
retry: false,
enabled: false,
refetchOnWindowFocus: false,
onSuccess: (data) => {
setCorrelatedStore((store) => setCorrelations(store, data.data || []));
},
onError: () => {
setCorrelatedStore((store) => setCorrelations(store, []));
notifyError({ message: 'Failed to fetch correlations' });
},
});
const { mutate: deleteSavedCorrelationMutation, isLoading: isDeleting } = useMutation(
(data: { correlationId: string; onSuccess?: () => void }) => deleteSavedCorrelation(data.correlationId),
{
onSuccess: (_data, variables) => {
variables.onSuccess && variables.onSuccess();
if (variables.correlationId === correlationId) {
setCorrelatedStore(cleanCorrelationStore);
setAppStore(syncTimeRange);
}
fetchCorrelations();
setCorrelatedStore((store) => toggleSavedCorrelationsModal(store, false));
notifySuccess({ message: 'Deleted Successfully' });
},
onError: (data: AxiosError) => {
if (isAxiosError(data) && data.response) {
const error = data.response?.data as string;
typeof error === 'string' && notifyError({ message: error });
} else if (data.message && typeof data.message === 'string') {
notifyError({ message: data.message });
}
},
},
);
const { mutate: saveCorrelationMutation, isLoading: isCorrelationSaving } = useMutation(
(data: { correlationData: any; onSuccess?: () => void }) => saveCorrelation(data.correlationData),
{
onSuccess: (data, variables) => {
variables.onSuccess && variables.onSuccess();
setCorrelatedStore((store) => setCorrelationId(store, data.data.id));
setCorrelatedStore((store) => setSavedCorrelationId(store, data.data.id));
fetchCorrelations();
notifySuccess({ message: 'Correlation saved successfully' });
},
onError: (data: AxiosError) => {
if (isAxiosError(data) && data.response) {
const error = data.response?.data as string;
typeof error === 'string' && notifyError({ message: error });
} else if (data.message && typeof data.message === 'string') {
notifyError({ message: data.message });
}
},
},
);
const { mutate: updateCorrelationMutation, isLoading: isCorrelationUpdating } = useMutation(
(data: { correlationData: any; onSuccess?: () => void }) => updateCorrelation(data.correlationData),
{
onSuccess: (data, variables) => {
variables.onSuccess && variables.onSuccess();
setCorrelatedStore((store) => setCorrelationId(store, data.data.id));
setCorrelatedStore((store) => setActiveCorrelation(store, data.data));
fetchCorrelations();
notifySuccess({ message: 'Correlation updated successfully' });
},
onError: (data: AxiosError) => {
if (isAxiosError(data) && data.response) {
const error = data.response?.data as string;
typeof error === 'string' && notifyError({ message: error });
} else if (data.message && typeof data.message === 'string') {
notifyError({ message: data.message });
}
},
},
);
return {
fetchCorrelationsError,
fetchCorrelationsSuccess,
fetchCorrelationsLoading,
fetchCorrelations,
deleteSavedCorrelationMutation,
isDeleting,
saveCorrelationMutation,
isCorrelationSaving,
updateCorrelationMutation,
isCorrelationUpdating,
};
};