|
| 1 | +/* eslint-disable no-console */ |
| 2 | +/* eslint-disable no-unused-expressions */ |
| 3 | +import { useState } from 'react' |
| 4 | +import toast from 'react-hot-toast' |
| 5 | +import 'react-multiple-select-dropdown-lite/dist/index.css' |
| 6 | +import { useNavigate } from 'react-router-dom' |
| 7 | +import { __ } from '../../../Utils/i18nwrap' |
| 8 | +import SnackMsg from '../../Utilities/SnackMsg' |
| 9 | +import Steps from '../../Utilities/Steps' |
| 10 | +import { saveIntegConfig } from '../IntegrationHelpers/IntegrationHelpers' |
| 11 | +import IntegrationStepThree from '../IntegrationHelpers/IntegrationStepThree' |
| 12 | +import ACPTAuthorization from './ACPTAuthorization' |
| 13 | +import { checkMappedFields } from './ACPTCommonFunc' |
| 14 | +import ACPTIntegLayout from './ACPTIntegLayout' |
| 15 | +import { |
| 16 | + cptFields, |
| 17 | + cptLabels, |
| 18 | + modules, |
| 19 | + optionPageFields, |
| 20 | + taxonomyFields, |
| 21 | + taxonomyLabels |
| 22 | +} from './staticData' |
| 23 | + |
| 24 | +function ACPT({ formFields, setFlow, flow, allIntegURL }) { |
| 25 | + const navigate = useNavigate() |
| 26 | + const [isLoading, setIsLoading] = useState(false) |
| 27 | + const [loading, setLoading] = useState({}) |
| 28 | + |
| 29 | + const [step, setStep] = useState(1) |
| 30 | + const [snack, setSnackbar] = useState({ show: false }) |
| 31 | + |
| 32 | + const [acptConf, setAcptConf] = useState({ |
| 33 | + name: 'ACPT', |
| 34 | + type: 'ACPT', |
| 35 | + base_url: window.location.origin, |
| 36 | + api_key: '', |
| 37 | + field_map: [{ formField: '', acptFormField: '' }], |
| 38 | + label_field_map: [{ formField: '', acptFormField: '' }], |
| 39 | + acptFields: [], |
| 40 | + acptLabels: [], |
| 41 | + utilities: {}, |
| 42 | + icon: 'wordpress', |
| 43 | + capability: 'manage_options', |
| 44 | + module: '', |
| 45 | + cptFields, |
| 46 | + cptLabels, |
| 47 | + taxonomyLabels, |
| 48 | + taxonomyFields, |
| 49 | + optionPageFields, |
| 50 | + modules |
| 51 | + }) |
| 52 | + |
| 53 | + const saveConfig = () => { |
| 54 | + setIsLoading(true) |
| 55 | + const resp = saveIntegConfig(flow, setFlow, allIntegURL, acptConf, navigate, '', '', setIsLoading) |
| 56 | + resp.then(res => { |
| 57 | + if (res.success) { |
| 58 | + toast.success(res.data?.msg) |
| 59 | + navigate(allIntegURL) |
| 60 | + } else { |
| 61 | + toast.error(res.data || res) |
| 62 | + } |
| 63 | + }) |
| 64 | + } |
| 65 | + |
| 66 | + const nextPage = pageNo => { |
| 67 | + setTimeout(() => { |
| 68 | + document.getElementById('btcd-settings-wrp').scrollTop = 0 |
| 69 | + }, 300) |
| 70 | + |
| 71 | + if (!checkMappedFields(acptConf)) { |
| 72 | + toast.error(__('Please map mandatory fields', 'bit-integrations')) |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + if ( |
| 77 | + ['create_cpt', 'update_cpt', 'create_option_page', 'update_option_page'].includes( |
| 78 | + acptConf.module |
| 79 | + ) && |
| 80 | + !acptConf?.icon |
| 81 | + ) { |
| 82 | + toast.error(__('Please select Icon', 'bit-integrations')) |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + if ( |
| 87 | + ['create_option_page', 'update_option_page'].includes(acptConf.module) && |
| 88 | + !acptConf?.capability |
| 89 | + ) { |
| 90 | + toast.error(__('Please select Capability', 'bit-integrations')) |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + acptConf.field_map.length > 0 && setStep(pageNo) |
| 95 | + } |
| 96 | + |
| 97 | + return ( |
| 98 | + <div> |
| 99 | + <SnackMsg snack={snack} setSnackbar={setSnackbar} /> |
| 100 | + <div className="txt-center mt-2"> |
| 101 | + <Steps step={3} active={step} /> |
| 102 | + </div> |
| 103 | + |
| 104 | + {/* STEP 1 */} |
| 105 | + <ACPTAuthorization |
| 106 | + acptConf={acptConf} |
| 107 | + setAcptConf={setAcptConf} |
| 108 | + step={step} |
| 109 | + setStep={setStep} |
| 110 | + loading={loading} |
| 111 | + setLoading={setLoading} |
| 112 | + /> |
| 113 | + |
| 114 | + {/* STEP 2 */} |
| 115 | + <div |
| 116 | + className="btcd-stp-page" |
| 117 | + style={{ |
| 118 | + ...(step === 2 && { width: 900, minHeight: '400px', height: 'auto', overflow: 'visible' }) |
| 119 | + }}> |
| 120 | + <ACPTIntegLayout |
| 121 | + formFields={formFields} |
| 122 | + acptConf={acptConf} |
| 123 | + setAcptConf={setAcptConf} |
| 124 | + loading={loading} |
| 125 | + setLoading={setLoading} |
| 126 | + isLoading={isLoading} |
| 127 | + setIsLoading={setIsLoading} |
| 128 | + setSnackbar={setSnackbar} |
| 129 | + /> |
| 130 | + |
| 131 | + {acptConf?.module && ( |
| 132 | + <button |
| 133 | + onClick={() => nextPage(3)} |
| 134 | + disabled={!checkMappedFields(acptConf)} |
| 135 | + className="btn f-right btcd-btn-lg purple sh-sm flx" |
| 136 | + type="button"> |
| 137 | + {__('Next', 'bit-integrations')} |
| 138 | + <div className="btcd-icn icn-arrow_back rev-icn d-in-b" /> |
| 139 | + </button> |
| 140 | + )} |
| 141 | + </div> |
| 142 | + |
| 143 | + {/* STEP 3 */} |
| 144 | + {acptConf?.module && ( |
| 145 | + <IntegrationStepThree |
| 146 | + step={step} |
| 147 | + saveConfig={() => saveConfig()} |
| 148 | + isLoading={isLoading} |
| 149 | + dataConf={acptConf} |
| 150 | + setDataConf={setAcptConf} |
| 151 | + formFields={formFields} |
| 152 | + /> |
| 153 | + )} |
| 154 | + </div> |
| 155 | + ) |
| 156 | +} |
| 157 | + |
| 158 | +export default ACPT |
0 commit comments