|
| 1 | +import { useMutation, useQuery } from 'react-query'; |
| 2 | +import _ from 'lodash'; |
| 3 | + |
| 4 | +import { |
| 5 | + deleteSavedCorrelation, |
| 6 | + getCorrelationById, |
| 7 | + getCorrelations, |
| 8 | + saveCorrelation, |
| 9 | + updateCorrelation, |
| 10 | +} from '@/api/correlations'; |
| 11 | +import { correlationStoreReducers, useCorrelationStore } from '@/pages/Correlation/providers/CorrelationProvider'; |
| 12 | +import { notifyError, notifySuccess } from '@/utils/notification'; |
| 13 | +import { AxiosError, isAxiosError } from 'axios'; |
| 14 | +import { appStoreReducers, useAppStore } from '@/layouts/MainLayout/providers/AppProvider'; |
| 15 | +import dayjs from 'dayjs'; |
| 16 | + |
| 17 | +const { |
| 18 | + setCorrelations, |
| 19 | + setActiveCorrelation, |
| 20 | + setCorrelationId, |
| 21 | + setSavedCorrelationId, |
| 22 | + cleanCorrelationStore, |
| 23 | + toggleSavedCorrelationsModal, |
| 24 | +} = correlationStoreReducers; |
| 25 | +const { setTimeRange, syncTimeRange } = appStoreReducers; |
| 26 | +export const useCorrelationsQuery = () => { |
| 27 | + const [{ correlationId }, setCorrelatedStore] = useCorrelationStore((store) => store); |
| 28 | + const [, setAppStore] = useAppStore((store) => store); |
| 29 | + const { |
| 30 | + isError: fetchCorrelationsError, |
| 31 | + isSuccess: fetchCorrelationsSuccess, |
| 32 | + isLoading: fetchCorrelationsLoading, |
| 33 | + refetch: fetchCorrelations, |
| 34 | + } = useQuery(['correlations'], () => getCorrelations(), { |
| 35 | + retry: false, |
| 36 | + enabled: false, |
| 37 | + refetchOnWindowFocus: false, |
| 38 | + onSuccess: (data) => { |
| 39 | + setCorrelatedStore((store) => setCorrelations(store, data.data || [])); |
| 40 | + }, |
| 41 | + onError: () => { |
| 42 | + setCorrelatedStore((store) => setCorrelations(store, [])); |
| 43 | + notifyError({ message: 'Failed to fetch correlations' }); |
| 44 | + }, |
| 45 | + }); |
| 46 | + |
| 47 | + const { |
| 48 | + mutate: getCorrelationByIdMutation, |
| 49 | + isError: fetchCorrelationIdError, |
| 50 | + isSuccess: fetchCorrelationIdSuccess, |
| 51 | + isLoading: fetchCorrelationIdLoading, |
| 52 | + } = useMutation((correlationId: string) => getCorrelationById(correlationId), { |
| 53 | + onSuccess: (data: any) => { |
| 54 | + data.data.startTime && |
| 55 | + data.data.endTime && |
| 56 | + setAppStore((store) => |
| 57 | + setTimeRange(store, { |
| 58 | + startTime: dayjs(data.data.startTime), |
| 59 | + endTime: dayjs(data.data.endTime), |
| 60 | + type: 'custom', |
| 61 | + }), |
| 62 | + ); |
| 63 | + setCorrelatedStore((store) => setCorrelationId(store, data.data.id)); |
| 64 | + setCorrelatedStore((store) => setActiveCorrelation(store, data.data)); |
| 65 | + }, |
| 66 | + onError: () => { |
| 67 | + notifyError({ message: 'Failed to fetch correlation' }); |
| 68 | + }, |
| 69 | + }); |
| 70 | + |
| 71 | + const { mutate: deleteSavedCorrelationMutation, isLoading: isDeleting } = useMutation( |
| 72 | + (data: { correlationId: string; onSuccess?: () => void }) => deleteSavedCorrelation(data.correlationId), |
| 73 | + { |
| 74 | + onSuccess: (_data, variables) => { |
| 75 | + variables.onSuccess && variables.onSuccess(); |
| 76 | + if (variables.correlationId === correlationId) { |
| 77 | + setCorrelatedStore(cleanCorrelationStore); |
| 78 | + setAppStore(syncTimeRange); |
| 79 | + } |
| 80 | + fetchCorrelations(); |
| 81 | + setCorrelatedStore((store) => toggleSavedCorrelationsModal(store, false)); |
| 82 | + notifySuccess({ message: 'Deleted Successfully' }); |
| 83 | + }, |
| 84 | + onError: (data: AxiosError) => { |
| 85 | + if (isAxiosError(data) && data.response) { |
| 86 | + const error = data.response?.data as string; |
| 87 | + typeof error === 'string' && notifyError({ message: error }); |
| 88 | + } else if (data.message && typeof data.message === 'string') { |
| 89 | + notifyError({ message: data.message }); |
| 90 | + } |
| 91 | + }, |
| 92 | + }, |
| 93 | + ); |
| 94 | + |
| 95 | + const { mutate: saveCorrelationMutation, isLoading: isCorrelationSaving } = useMutation( |
| 96 | + (data: { correlationData: any; onSuccess?: () => void }) => saveCorrelation(data.correlationData), |
| 97 | + { |
| 98 | + onSuccess: (data, variables) => { |
| 99 | + variables.onSuccess && variables.onSuccess(); |
| 100 | + setCorrelatedStore((store) => setCorrelationId(store, data.data.id)); |
| 101 | + setCorrelatedStore((store) => setSavedCorrelationId(store, data.data.id)); |
| 102 | + fetchCorrelations(); |
| 103 | + notifySuccess({ message: 'Correlation saved successfully' }); |
| 104 | + }, |
| 105 | + onError: (data: AxiosError) => { |
| 106 | + if (isAxiosError(data) && data.response) { |
| 107 | + const error = data.response?.data as string; |
| 108 | + typeof error === 'string' && notifyError({ message: error }); |
| 109 | + } else if (data.message && typeof data.message === 'string') { |
| 110 | + notifyError({ message: data.message }); |
| 111 | + } |
| 112 | + }, |
| 113 | + }, |
| 114 | + ); |
| 115 | + |
| 116 | + const { mutate: updateCorrelationMutation, isLoading: isCorrelationUpdating } = useMutation( |
| 117 | + (data: { correlationData: any; onSuccess?: () => void }) => updateCorrelation(data.correlationData), |
| 118 | + { |
| 119 | + onSuccess: (data, variables) => { |
| 120 | + variables.onSuccess && variables.onSuccess(); |
| 121 | + setCorrelatedStore((store) => setCorrelationId(store, data.data.id)); |
| 122 | + setCorrelatedStore((store) => setActiveCorrelation(store, data.data)); |
| 123 | + fetchCorrelations(); |
| 124 | + notifySuccess({ message: 'Correlation updated successfully' }); |
| 125 | + }, |
| 126 | + onError: (data: AxiosError) => { |
| 127 | + if (isAxiosError(data) && data.response) { |
| 128 | + const error = data.response?.data as string; |
| 129 | + typeof error === 'string' && notifyError({ message: error }); |
| 130 | + } else if (data.message && typeof data.message === 'string') { |
| 131 | + notifyError({ message: data.message }); |
| 132 | + } |
| 133 | + }, |
| 134 | + }, |
| 135 | + ); |
| 136 | + |
| 137 | + return { |
| 138 | + fetchCorrelationsError, |
| 139 | + fetchCorrelationsSuccess, |
| 140 | + fetchCorrelationsLoading, |
| 141 | + fetchCorrelations, |
| 142 | + |
| 143 | + deleteSavedCorrelationMutation, |
| 144 | + isDeleting, |
| 145 | + |
| 146 | + fetchCorrelationIdError, |
| 147 | + fetchCorrelationIdSuccess, |
| 148 | + fetchCorrelationIdLoading, |
| 149 | + getCorrelationByIdMutation, |
| 150 | + |
| 151 | + saveCorrelationMutation, |
| 152 | + isCorrelationSaving, |
| 153 | + |
| 154 | + updateCorrelationMutation, |
| 155 | + isCorrelationUpdating, |
| 156 | + }; |
| 157 | +}; |
0 commit comments