Skip to content

Commit 424366f

Browse files
committed
refactor: custom fetcher helper
1 parent 9e4d11a commit 424366f

File tree

7 files changed

+121
-138
lines changed

7 files changed

+121
-138
lines changed

frontend-dev/src/Utils/customFormHelper.jsx renamed to frontend-dev/src/Utils/CustomFetcherHelper.jsx

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
11
import { create } from 'mutative'
22
import bitsFetch from './bitsFetch'
33

4-
const removeTestData = (entityId, removeAction, removeMethod = 'POST', key = 'triggered_entity_id') => {
5-
if (!entityId) {
6-
return
4+
export default function CustomFetcherHelper(
5+
isLoadingRef,
6+
entityId,
7+
controller,
8+
setIsLoading,
9+
removeAction,
10+
removeMethod = 'POST',
11+
key = 'triggered_entity_id'
12+
) {
13+
const removeTestData = () => {
14+
if (!entityId) {
15+
return
16+
}
17+
18+
bitsFetch({ [key]: entityId }, removeAction, null, removeMethod)
19+
}
20+
21+
const stopFetching = () => {
22+
controller.abort()
23+
setIsLoading(false)
24+
isLoadingRef.current = false
25+
26+
removeTestData()
727
}
828

9-
bitsFetch({ [key]: entityId }, removeAction, null, removeMethod)
29+
return { stopFetching }
1030
}
1131

1232
const resetActionHookFlowData = setFlow => {
@@ -33,22 +53,6 @@ const startFetching = (
3353
resetFlowData(setFlow, isEdit)
3454
}
3555

36-
const stopFetching = (
37-
controller,
38-
entityId,
39-
isLoadingRef,
40-
removeAction,
41-
removeMethod,
42-
setIsLoading,
43-
key = 'triggered_entity_id'
44-
) => {
45-
controller.abort()
46-
setIsLoading(false)
47-
isLoadingRef.current = false
48-
49-
removeTestData(entityId, removeAction, removeMethod, key)
50-
}
51-
5256
const resetFlowData = (setFlow, isEdit = false) => {
5357
setFlow(prevFlow =>
5458
create(prevFlow, draftFlow => {
@@ -59,4 +63,4 @@ const resetFlowData = (setFlow, isEdit = false) => {
5963
)
6064
}
6165

62-
export { removeTestData, startFetching, stopFetching, resetFlowData, resetActionHookFlowData }
66+
export { startFetching, resetFlowData, resetActionHookFlowData }

frontend-dev/src/components/AllIntegrations/EditActionHook.jsx

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@ import MultiSelect from 'react-multiple-select-dropdown-lite'
66
import 'react-multiple-select-dropdown-lite/dist/index.css'
77
import { useRecoilState, useSetRecoilState } from 'recoil'
88
import { $actionConf, $formFields, $newFlow } from '../../GlobalStates'
9-
import CloseIcn from '../../Icons/CloseIcn'
109
import bitsFetch from '../../Utils/bitsFetch'
11-
import { stopFetching } from '../../Utils/customFormHelper'
10+
import CustomFetcherHelper from '../../Utils/CustomFetcherHelper'
1211
import { extractValueFromPath } from '../../Utils/Helpers'
1312
import { __ } from '../../Utils/i18nwrap'
1413
import LoaderSm from '../Loaders/LoaderSm'
1514
import EyeIcn from '../Utilities/EyeIcn'
1615
import EyeOffIcn from '../Utilities/EyeOffIcn'
17-
import TreeViewer from '../Utilities/treeViewer/TreeViewer'
1816
import FieldContainer from '../Utilities/FieldContainer'
17+
import TreeViewer from '../Utilities/treeViewer/TreeViewer'
1918

2019
function EditActionHook() {
2120
const [actionConf, setActionConf] = useRecoilState($actionConf)
@@ -25,24 +24,22 @@ function EditActionHook() {
2524
const [showSelectedFields, setShowSelectedFields] = useState(false)
2625
const [isLoading, setIsLoading] = useState(false)
2726
const isFetchingRef = useRef(false)
27+
2828
let controller = new AbortController()
2929
const signal = controller.signal
30-
31-
const callStopFetching = () => {
32-
stopFetching(
33-
controller,
34-
flow?.triggered_entity_id,
35-
isFetchingRef,
36-
'action_hook/test/remove',
37-
'post',
38-
setIsLoading,
39-
'hook_id'
40-
)
41-
}
30+
const { stopFetching } = CustomFetcherHelper(
31+
isFetchingRef,
32+
flow?.triggered_entity_id,
33+
controller,
34+
setIsLoading,
35+
'action_hook/test/remove',
36+
'POST',
37+
'hook_id'
38+
)
4239

4340
const handleFetch = () => {
4441
if (isFetchingRef.current) {
45-
callStopFetching()
42+
stopFetching()
4643
return
4744
}
4845

@@ -56,7 +53,7 @@ function EditActionHook() {
5653

5754
try {
5855
if (!isFetchingRef.current || !hookID) {
59-
callStopFetching()
56+
stopFetching()
6057
return
6158
}
6259

@@ -100,7 +97,7 @@ function EditActionHook() {
10097
setShowSelectedFields(true)
10198
}
10299

103-
callStopFetching()
100+
stopFetching()
104101
})
105102
} catch (err) {
106103
console.log(
@@ -202,7 +199,7 @@ function EditActionHook() {
202199

203200
const setHook = val => {
204201
if (flow?.triggered_entity_id) {
205-
callStopFetching()
202+
stopFetching()
206203
}
207204

208205
setFlow(prevFlow =>

frontend-dev/src/components/AllIntegrations/EditCustomFormSubmissionInteg.jsx

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import 'react-multiple-select-dropdown-lite/dist/index.css'
88
import { useRecoilState, useSetRecoilState } from 'recoil'
99
import { $formFields, $newFlow } from '../../GlobalStates'
1010
import bitsFetch from '../../Utils/bitsFetch'
11-
import { stopFetching } from '../../Utils/customFormHelper'
11+
import CustomFetcherHelper from '../../Utils/CustomFetcherHelper'
1212
import { __ } from '../../Utils/i18nwrap'
1313
import LoaderSm from '../Loaders/LoaderSm'
1414

@@ -32,21 +32,18 @@ function EditCustomFormSubmissionInteg({ setSnackbar }) {
3232
const fetchMethod = flow?.flow_details?.fetch?.method || ''
3333
const removeAction = flow?.flow_details?.fetch_remove?.action || ''
3434
const removeMethod = flow?.flow_details?.fetch_remove?.method || ''
35-
36-
const callStopFetching = () => {
37-
stopFetching(
38-
controller,
39-
flow.triggered_entity_id,
40-
isFetchingRef,
41-
removeAction,
42-
removeMethod,
43-
setIsLoading
44-
)
45-
}
35+
const { stopFetching } = CustomFetcherHelper(
36+
isFetchingRef,
37+
flow.triggered_entity_id,
38+
controller,
39+
setIsLoading,
40+
removeAction,
41+
removeMethod
42+
)
4643

4744
const handleFetch = async () => {
4845
if (isFetchingRef.current) {
49-
callStopFetching()
46+
stopFetching()
5047
return
5148
}
5249

@@ -60,7 +57,7 @@ function EditCustomFormSubmissionInteg({ setSnackbar }) {
6057

6158
try {
6259
if (!isFetchingRef.current || !entityId) {
63-
callStopFetching()
60+
stopFetching()
6461
return
6562
}
6663

@@ -87,7 +84,7 @@ function EditCustomFormSubmissionInteg({ setSnackbar }) {
8784
setFormFields(formData)
8885
}
8986

90-
callStopFetching()
87+
stopFetching()
9188
})
9289
} catch (err) {
9390
console.log(
@@ -120,13 +117,13 @@ function EditCustomFormSubmissionInteg({ setSnackbar }) {
120117

121118
useEffect(() => {
122119
return () => {
123-
callStopFetching()
120+
stopFetching()
124121
}
125122
}, [])
126123

127124
const setTriggerEntityId = entityId => {
128125
if (flow?.triggered_entity_id) {
129-
callStopFetching()
126+
stopFetching()
130127
}
131128

132129
setFlow(prevFlow =>

frontend-dev/src/components/AllIntegrations/EditCustomTrigger.jsx

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import 'react-multiple-select-dropdown-lite/dist/index.css'
66
import { useRecoilState, useSetRecoilState } from 'recoil'
77
import { $actionConf, $formFields, $newFlow } from '../../GlobalStates'
88
import bitsFetch from '../../Utils/bitsFetch'
9-
import { stopFetching } from '../../Utils/customFormHelper'
9+
import CustomFetcherHelper from '../../Utils/CustomFetcherHelper'
1010
import { __ } from '../../Utils/i18nwrap'
1111
import LoaderSm from '../Loaders/LoaderSm'
1212
import CopyTextTrigger from '../Utilities/CopyTextTrigger'
@@ -25,27 +25,24 @@ function EditCustomTrigger() {
2525
const [showResponse, setShowResponse] = useState(false)
2626
const [showSelectedFields, setShowSelectedFields] = useState(false)
2727
const isFetchingRef = useRef(false)
28+
2829
let controller = new AbortController()
2930
const signal = controller.signal
31+
const { stopFetching } = CustomFetcherHelper(
32+
isFetchingRef,
33+
flow?.triggered_entity_id,
34+
controller,
35+
setIsLoading,
36+
'custom_trigger/test/remove',
37+
'hook_id'
38+
)
3039

3140
const triggerAbeleHook = `do_action(
3241
'bit_integrations_custom_trigger',
3342
'${flow?.triggered_entity_id}',
3443
array()
3544
);`
3645

37-
const callStopFetching = () => {
38-
stopFetching(
39-
controller,
40-
flow?.triggered_entity_id,
41-
isFetchingRef,
42-
'custom_trigger/test/remove',
43-
'post',
44-
setIsLoading,
45-
'hook_id'
46-
)
47-
}
48-
4946
const setSelectedFieldsData = (value = null, remove = false, index = null) => {
5047
if (remove) {
5148
index = index ? index : flow.flow_details.fields.findIndex(field => field.name === value)
@@ -103,7 +100,7 @@ function EditCustomTrigger() {
103100

104101
const handleFetch = () => {
105102
if (isFetchingRef.current) {
106-
callStopFetching()
103+
stopFetching()
107104
return
108105
}
109106

@@ -117,7 +114,7 @@ function EditCustomTrigger() {
117114

118115
try {
119116
if (!isFetchingRef.current || !hookID) {
120-
callStopFetching()
117+
stopFetching()
121118
return
122119
}
123120

@@ -160,7 +157,7 @@ function EditCustomTrigger() {
160157
setShowSelectedFields(true)
161158
}
162159

163-
callStopFetching()
160+
stopFetching()
164161
})
165162
} catch (err) {
166163
console.log(

frontend-dev/src/components/Triggers/ActionHook.jsx

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import MultiSelect from 'react-multiple-select-dropdown-lite'
77
import 'react-multiple-select-dropdown-lite/dist/index.css'
88
import { useRecoilState, useSetRecoilState } from 'recoil'
99
import { $flowStep, $formFields, $newFlow } from '../../GlobalStates'
10+
import bitsFetch from '../../Utils/bitsFetch'
11+
import CustomFetcherHelper, { resetActionHookFlowData } from '../../Utils/CustomFetcherHelper'
1012
import GetLogo from '../../Utils/GetLogo'
1113
import { extractValueFromPath } from '../../Utils/Helpers'
12-
import hooklist from '../../Utils/StaticData/hooklist'
13-
import bitsFetch from '../../Utils/bitsFetch'
14-
import { resetActionHookFlowData, stopFetching } from '../../Utils/customFormHelper'
1514
import { __ } from '../../Utils/i18nwrap'
15+
import hooklist from '../../Utils/StaticData/hooklist'
1616
import LoaderSm from '../Loaders/LoaderSm'
1717
import ConfirmModal from '../Utilities/ConfirmModal'
1818
import EyeIcn from '../Utilities/EyeIcn'
@@ -36,8 +36,18 @@ const ActionHook = () => {
3636
const [snack, setSnackbar] = useState({ show: false })
3737
const [showResponse, setShowResponse] = useState(false)
3838
const isFetchingRef = useRef(false)
39+
3940
let controller = new AbortController()
4041
const signal = controller.signal
42+
const { stopFetching } = CustomFetcherHelper(
43+
isFetchingRef,
44+
hookID,
45+
controller,
46+
setIsLoading,
47+
'action_hook/test/remove',
48+
'POST',
49+
'hook_id'
50+
)
4151

4252
const setTriggerData = () => {
4353
if (!selectedFields.length) {
@@ -63,18 +73,6 @@ const ActionHook = () => {
6373
setFlowStep(2)
6474
}
6575

66-
const callStopFetching = () => {
67-
stopFetching(
68-
controller,
69-
hookID,
70-
isFetchingRef,
71-
'action_hook/test/remove',
72-
'post',
73-
setIsLoading,
74-
'hook_id'
75-
)
76-
}
77-
7876
const setSelectedFieldsData = (value = null, remove = false, index = null) => {
7977
if (remove) {
8078
index = index ? index : selectedFields.findIndex(field => field.name === value)
@@ -117,7 +115,7 @@ const ActionHook = () => {
117115
}
118116

119117
return () => {
120-
callStopFetching()
118+
stopFetching()
121119
}
122120
}, [])
123121

@@ -132,7 +130,7 @@ const ActionHook = () => {
132130

133131
const handleFetch = () => {
134132
if (isFetchingRef.current) {
135-
callStopFetching()
133+
stopFetching()
136134
return
137135
}
138136

@@ -143,7 +141,7 @@ const ActionHook = () => {
143141
const fetchSequentially = () => {
144142
try {
145143
if (!isFetchingRef.current || !hookID) {
146-
callStopFetching()
144+
stopFetching()
147145
return
148146
}
149147

@@ -166,7 +164,7 @@ const ActionHook = () => {
166164
setShowResponse(true)
167165
}
168166

169-
callStopFetching()
167+
stopFetching()
170168
})
171169
} catch (err) {
172170
console.log(
@@ -191,7 +189,7 @@ const ActionHook = () => {
191189
const isHook = name === 'hook'
192190

193191
if (hookID) {
194-
callStopFetching()
192+
stopFetching()
195193
}
196194

197195
if (isCustom || (isHook && val === 'custom')) {

0 commit comments

Comments
 (0)