|  | 
|  | 1 | +/* eslint-disable no-param-reassign */ | 
|  | 2 | + | 
|  | 3 | +import { create } from 'mutative' | 
|  | 4 | +import { useRef, useState } from 'react' | 
|  | 5 | +import 'react-multiple-select-dropdown-lite/dist/index.css' | 
|  | 6 | +import { useRecoilState, useSetRecoilState } from 'recoil' | 
|  | 7 | +import { $actionConf, $formFields, $newFlow } from '../../GlobalStates' | 
|  | 8 | +import CloseIcn from '../../Icons/CloseIcn' | 
|  | 9 | +import bitsFetch from '../../Utils/bitsFetch' | 
|  | 10 | +import { __ } from '../../Utils/i18nwrap' | 
|  | 11 | +import LoaderSm from '../Loaders/LoaderSm' | 
|  | 12 | +import CopyTextTrigger from '../Utilities/CopyTextTrigger' | 
|  | 13 | +import EyeIcn from '../Utilities/EyeIcn' | 
|  | 14 | +import EyeOffIcn from '../Utilities/EyeOffIcn' | 
|  | 15 | +import SnackMsg from '../Utilities/SnackMsg' | 
|  | 16 | +import TreeViewer from '../Utilities/treeViewer/TreeViewer' | 
|  | 17 | +import FieldContainer from '../Utilities/FieldContainer' | 
|  | 18 | + | 
|  | 19 | +function EditCustomTrigger() { | 
|  | 20 | +  const [actionConf, setActionConf] = useRecoilState($actionConf) | 
|  | 21 | +  const [flow, setFlow] = useRecoilState($newFlow) | 
|  | 22 | +  const setFormFields = useSetRecoilState($formFields) | 
|  | 23 | +  const [isLoading, setIsLoading] = useState(false) | 
|  | 24 | +  const [snack, setSnackbar] = useState({ show: false }) | 
|  | 25 | +  const [showResponse, setShowResponse] = useState(false) | 
|  | 26 | +  const [showSelectedFields, setShowSelectedFields] = useState(false) | 
|  | 27 | +  const fetchIntervalRef = useRef(0) | 
|  | 28 | +  let controller = new AbortController() | 
|  | 29 | +  const signal = controller.signal | 
|  | 30 | + | 
|  | 31 | +  const triggerAbeleHook = `do_action( | 
|  | 32 | +    'bit_integrations_custom_trigger', | 
|  | 33 | +    '${flow?.triggered_entity_id}', | 
|  | 34 | +     array() | 
|  | 35 | +     );` | 
|  | 36 | + | 
|  | 37 | +  const setSelectedFieldsData = (value = null, remove = false, index = null) => { | 
|  | 38 | +    if (remove) { | 
|  | 39 | +      index = index ? index : flow.flow_details.fields.findIndex((field) => field.name === value) | 
|  | 40 | + | 
|  | 41 | +      if (index !== -1) { | 
|  | 42 | +        removeSelectedField(index) | 
|  | 43 | +      } | 
|  | 44 | +      return | 
|  | 45 | +    } | 
|  | 46 | +    addSelectedField(value) | 
|  | 47 | +  } | 
|  | 48 | + | 
|  | 49 | +  const addSelectedField = (value) => { | 
|  | 50 | +    setFlow((prevFlow) => | 
|  | 51 | +      create(prevFlow, (draftFlow) => { | 
|  | 52 | +        if (draftFlow.flow_details.fields.findIndex((field) => field.name === value) === -1) { | 
|  | 53 | +          draftFlow.flow_details['fields'].push({ label: value, name: value }) | 
|  | 54 | +        } | 
|  | 55 | +      }) | 
|  | 56 | +    ) | 
|  | 57 | +    setActionConf((prevConf) => | 
|  | 58 | +      create(prevConf, (draftConf) => { | 
|  | 59 | +        if (draftConf.fields.findIndex((field) => field.name === value) === -1) { | 
|  | 60 | +          draftConf['fields'].push({ label: value, name: value }) | 
|  | 61 | +        } | 
|  | 62 | +      }) | 
|  | 63 | +    ) | 
|  | 64 | +    setFormFields((prevFields) => | 
|  | 65 | +      create(prevFields, (draftFields) => { | 
|  | 66 | +        if (draftFields.findIndex((field) => field.name === value) === -1) { | 
|  | 67 | +          draftFields.push({ label: value, name: value }) | 
|  | 68 | +        } | 
|  | 69 | +      }) | 
|  | 70 | +    ) | 
|  | 71 | +  } | 
|  | 72 | + | 
|  | 73 | +  const removeSelectedField = (index) => { | 
|  | 74 | +    setFormFields((prevFields) => | 
|  | 75 | +      create(prevFields, (draftFields) => { | 
|  | 76 | +        index = draftFields.findIndex( | 
|  | 77 | +          (field) => field.name === flow.flow_details.fields[index].name | 
|  | 78 | +        ) | 
|  | 79 | +        draftFields.splice(index, 1) | 
|  | 80 | +      }) | 
|  | 81 | +    ) | 
|  | 82 | +    setFlow((prevFlow) => | 
|  | 83 | +      create(prevFlow, (draftFlow) => { | 
|  | 84 | +        draftFlow.flow_details.fields.splice(index, 1) | 
|  | 85 | +      }) | 
|  | 86 | +    ) | 
|  | 87 | +    setActionConf((prevConf) => | 
|  | 88 | +      create(prevConf, (draftConf) => { | 
|  | 89 | +        draftConf.fields.splice(index, 1) | 
|  | 90 | +      }) | 
|  | 91 | +    ) | 
|  | 92 | +  } | 
|  | 93 | + | 
|  | 94 | +  const removeTestData = (hookID, reset = false) => { | 
|  | 95 | +    bitsFetch({ hook_id: hookID, reset: reset }, 'custom_trigger/test/remove').then((resp) => { | 
|  | 96 | +      delete window.hook_id | 
|  | 97 | +      fetchIntervalRef.current && clearInterval(fetchIntervalRef.current) | 
|  | 98 | +    }) | 
|  | 99 | +  } | 
|  | 100 | + | 
|  | 101 | +  const handleFetch = () => { | 
|  | 102 | +    if (isLoading) { | 
|  | 103 | +      fetchIntervalRef.current && clearInterval(fetchIntervalRef.current) | 
|  | 104 | +      controller.abort() | 
|  | 105 | +      removeTestData(flow?.triggered_entity_id) | 
|  | 106 | +      setIsLoading(false) | 
|  | 107 | +      return | 
|  | 108 | +    } | 
|  | 109 | + | 
|  | 110 | +    setIsLoading(true) | 
|  | 111 | +    setShowResponse(false) | 
|  | 112 | +    setFlow((prevFlow) => | 
|  | 113 | +      create(prevFlow, (draftFlow) => { | 
|  | 114 | +        delete draftFlow.flow_details?.tmp | 
|  | 115 | +        delete draftFlow.flow_details?.rawData | 
|  | 116 | +      }) | 
|  | 117 | +    ) | 
|  | 118 | +    fetchIntervalRef.current = setInterval(() => { | 
|  | 119 | +      bitsFetch({ hook_id: flow?.triggered_entity_id }, 'custom_trigger/test', null, 'post', signal) | 
|  | 120 | +        .then((resp) => { | 
|  | 121 | +          if (resp.success) { | 
|  | 122 | +            controller.abort() | 
|  | 123 | +            clearInterval(fetchIntervalRef.current) | 
|  | 124 | +            setFlow((prevFlow) => | 
|  | 125 | +              create(prevFlow, (draftFlow) => { | 
|  | 126 | +                draftFlow.flow_details.rawData = resp.data.custom_trigger | 
|  | 127 | +                draftFlow.flow_details.fields = [] | 
|  | 128 | + | 
|  | 129 | +                if (draftFlow.flow_details?.body?.data) { | 
|  | 130 | +                  draftFlow.flow_details.body.data = [] | 
|  | 131 | +                } else { | 
|  | 132 | +                  draftFlow.flow_details.field_map = [] | 
|  | 133 | +                } | 
|  | 134 | +              }) | 
|  | 135 | +            ) | 
|  | 136 | +            setActionConf((prevConf) => | 
|  | 137 | +              create(prevConf, (draftConf) => { | 
|  | 138 | +                draftConf.rawData = resp.data.custom_trigger | 
|  | 139 | +                draftConf.fields = [] | 
|  | 140 | + | 
|  | 141 | +                if (draftConf?.body?.data) { | 
|  | 142 | +                  draftConf.body.data = [] | 
|  | 143 | +                } else { | 
|  | 144 | +                  draftConf.field_map = [] | 
|  | 145 | +                } | 
|  | 146 | +              }) | 
|  | 147 | +            ) | 
|  | 148 | + | 
|  | 149 | +            setFormFields([]) | 
|  | 150 | +            setIsLoading(false) | 
|  | 151 | +            setShowResponse(true) | 
|  | 152 | +            setShowSelectedFields(true) | 
|  | 153 | +            removeTestData(flow?.triggered_entity_id, true) | 
|  | 154 | +          } | 
|  | 155 | +        }) | 
|  | 156 | +        .catch((err) => { | 
|  | 157 | +          if (err.name === 'AbortError') { | 
|  | 158 | +            console.log(__('AbortError: Fetch request aborted', 'bit-integrations')) | 
|  | 159 | +          } | 
|  | 160 | +        }) | 
|  | 161 | +    }, 1500) | 
|  | 162 | +  } | 
|  | 163 | + | 
|  | 164 | +  const showResponseTable = () => { | 
|  | 165 | +    setShowResponse((prevState) => !prevState) | 
|  | 166 | +  } | 
|  | 167 | + | 
|  | 168 | +  const onUpdateField = (value, index, key) => { | 
|  | 169 | +    setFlow((prevFlow) => | 
|  | 170 | +      create(prevFlow, (draftFlow) => { | 
|  | 171 | +        draftFlow.flow_details.fields[index][key] = value | 
|  | 172 | +      }) | 
|  | 173 | +    ) | 
|  | 174 | +    setActionConf((prevConf) => | 
|  | 175 | +      create(prevConf, (draftConf) => { | 
|  | 176 | +        draftConf.fields[index][key] = value | 
|  | 177 | +      }) | 
|  | 178 | +    ) | 
|  | 179 | +    setFormFields((prevFields) => | 
|  | 180 | +      create(prevFields, (draftFields) => { | 
|  | 181 | +        draftFields[index][key] = value | 
|  | 182 | +      }) | 
|  | 183 | +    ) | 
|  | 184 | +  } | 
|  | 185 | + | 
|  | 186 | +  return ( | 
|  | 187 | +    <div className="trigger-custom-width"> | 
|  | 188 | +      <SnackMsg snack={snack} setSnackbar={setSnackbar} /> | 
|  | 189 | +      <div className="mt-3"> | 
|  | 190 | +        <b>{__('Custom action trigger:', 'bit-integrations')}</b> | 
|  | 191 | +      </div> | 
|  | 192 | +      <CopyTextTrigger | 
|  | 193 | +        value={triggerAbeleHook} | 
|  | 194 | +        className="flx mt-2" | 
|  | 195 | +        setSnackbar={setSnackbar} | 
|  | 196 | +        readOnly | 
|  | 197 | +      /> | 
|  | 198 | + | 
|  | 199 | +      <div className="flx flx-between"> | 
|  | 200 | +        <button | 
|  | 201 | +          onClick={handleFetch} | 
|  | 202 | +          className={`btn btcd-btn-lg sh-sm flx ${isLoading ? 'red' : 'purple'}`} | 
|  | 203 | +          type="button" | 
|  | 204 | +          disabled={isLoading}> | 
|  | 205 | +          {isLoading | 
|  | 206 | +            ? __('Stop', 'bit-integrations') | 
|  | 207 | +            : flow.flow_details?.rawData | 
|  | 208 | +              ? __('Fetched ✔', 'bit-integrations') | 
|  | 209 | +              : __('Fetch', 'bit-integrations')} | 
|  | 210 | +          {isLoading && <LoaderSm size="20" clr="#022217" className="ml-2" />} | 
|  | 211 | +        </button> | 
|  | 212 | +      </div> | 
|  | 213 | + | 
|  | 214 | +      {flow.flow_details?.rawData && ( | 
|  | 215 | +        <> | 
|  | 216 | +          <div className="my-3"> | 
|  | 217 | +            <b>{__('Selected Fields:', 'bit-integrations')}</b> | 
|  | 218 | +          </div> | 
|  | 219 | + | 
|  | 220 | +          {showSelectedFields && ( | 
|  | 221 | +            <FieldContainer | 
|  | 222 | +              data={flow.flow_details.fields} | 
|  | 223 | +              onUpdateField={onUpdateField} | 
|  | 224 | +              onRemoveField={removeSelectedField} | 
|  | 225 | +            /> | 
|  | 226 | +          )} | 
|  | 227 | +          <button | 
|  | 228 | +            onClick={() => setShowSelectedFields((prev) => !prev)} | 
|  | 229 | +            className="btn btcd-btn-md sh-sm flx gray"> | 
|  | 230 | +            <span className="txt-actionHook-resbtn font-inter-500"> | 
|  | 231 | +              {showSelectedFields ? 'Hide Selected Fields' : 'View Selected Fields'} | 
|  | 232 | +            </span> | 
|  | 233 | +              | 
|  | 234 | +            {!showSelectedFields ? ( | 
|  | 235 | +              <EyeIcn width="20" height="20" strokeColor="#000000" /> | 
|  | 236 | +            ) : ( | 
|  | 237 | +              <EyeOffIcn width="20" height="20" strokeColor="#000000" /> | 
|  | 238 | +            )} | 
|  | 239 | +          </button> | 
|  | 240 | +        </> | 
|  | 241 | +      )} | 
|  | 242 | + | 
|  | 243 | +      {flow.flow_details?.rawData && ( | 
|  | 244 | +        <> | 
|  | 245 | +          <div className="mt-5"> | 
|  | 246 | +            <b>{__('Select Fields:', 'bit-integrations')}</b> | 
|  | 247 | +          </div> | 
|  | 248 | + | 
|  | 249 | +          {showResponse && ( | 
|  | 250 | +            <TreeViewer data={flow?.flow_details?.rawData} onChange={setSelectedFieldsData} /> | 
|  | 251 | +          )} | 
|  | 252 | +        </> | 
|  | 253 | +      )} | 
|  | 254 | + | 
|  | 255 | +      {flow.flow_details?.rawData && ( | 
|  | 256 | +        <div className="flx flx-between"> | 
|  | 257 | +          <button onClick={showResponseTable} className="btn btcd-btn-md sh-sm flx gray"> | 
|  | 258 | +            <span className="txt-actionHook-resbtn font-inter-500"> | 
|  | 259 | +              {showResponse | 
|  | 260 | +                ? __('Hide Response', 'bit-integrations') | 
|  | 261 | +                : __('View Response', 'bit-integrations')} | 
|  | 262 | +            </span> | 
|  | 263 | +              | 
|  | 264 | +            {!showResponse ? ( | 
|  | 265 | +              <EyeIcn width="20" height="20" strokeColor="#000000" /> | 
|  | 266 | +            ) : ( | 
|  | 267 | +              <EyeOffIcn width="20" height="20" strokeColor="#000000" /> | 
|  | 268 | +            )} | 
|  | 269 | +          </button> | 
|  | 270 | +        </div> | 
|  | 271 | +      )} | 
|  | 272 | +    </div> | 
|  | 273 | +  ) | 
|  | 274 | +} | 
|  | 275 | + | 
|  | 276 | +export default EditCustomTrigger | 
0 commit comments