| 1 |
import React, { useState, useEffect, Fragment } from 'react'; |
| 2 |
import { portalId, refreshToken } from '../../constants/leadinConfig'; |
| 3 |
import ElementorBanner from '../Common/ElementorBanner'; |
| 4 |
import UISpinner from '../../shared/UIComponents/UISpinner'; |
| 5 |
import { __ } from '@wordpress/i18n'; |
| 6 |
import { |
| 7 |
BackgroudAppContext, |
| 8 |
useBackgroundAppContext, |
| 9 |
} from '../../iframe/useBackgroundApp'; |
| 10 |
import useForms from './hooks/useForms'; |
| 11 |
import { getOrCreateBackgroundApp } from '../../utils/backgroundAppUtils'; |
| 12 |
|
| 13 |
interface IElementorFormSelectProps { |
| 14 |
formId: string; |
| 15 |
setAttributes: Function; |
| 16 |
} |
| 17 |
|
| 18 |
function ElementorFormSelect({ |
| 19 |
formId, |
| 20 |
setAttributes, |
| 21 |
}: IElementorFormSelectProps) { |
| 22 |
const { hasError, forms, loading } = useForms(); |
| 23 |
|
| 24 |
return loading ? ( |
| 25 |
<div> |
| 26 |
<UISpinner /> |
| 27 |
</div> |
| 28 |
) : hasError ? ( |
| 29 |
<ElementorBanner type="danger"> |
| 30 |
{__('Please refresh your forms or try again in a few minutes', 'leadin')} |
| 31 |
</ElementorBanner> |
| 32 |
) : ( |
| 33 |
<select |
| 34 |
value={formId} |
| 35 |
onChange={event => { |
| 36 |
const selectedForm = forms.find( |
| 37 |
form => form.value === event.target.value |
| 38 |
); |
| 39 |
if (selectedForm) { |
| 40 |
setAttributes({ |
| 41 |
portalId, |
| 42 |
formId: selectedForm.value, |
| 43 |
formName: selectedForm.label, |
| 44 |
}); |
| 45 |
} |
| 46 |
}} |
| 47 |
> |
| 48 |
<option value="" disabled={true} selected={true}> |
| 49 |
{__('Search for a form', 'leadin')} |
| 50 |
</option> |
| 51 |
{forms.map(form => ( |
| 52 |
<option key={form.value} value={form.value}> |
| 53 |
{form.label} |
| 54 |
</option> |
| 55 |
))} |
| 56 |
</select> |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
function ElementorFormSelectWrapper(props: IElementorFormSelectProps) { |
| 61 |
const isBackgroundAppReady = useBackgroundAppContext(); |
| 62 |
|
| 63 |
return ( |
| 64 |
<Fragment> |
| 65 |
{!isBackgroundAppReady ? ( |
| 66 |
<div> |
| 67 |
<UISpinner /> |
| 68 |
</div> |
| 69 |
) : ( |
| 70 |
<ElementorFormSelect {...props} /> |
| 71 |
)} |
| 72 |
</Fragment> |
| 73 |
); |
| 74 |
} |
| 75 |
|
| 76 |
export default function ElementorFormSelectContainer( |
| 77 |
props: IElementorFormSelectProps |
| 78 |
) { |
| 79 |
return ( |
| 80 |
<BackgroudAppContext.Provider |
| 81 |
value={refreshToken && getOrCreateBackgroundApp(refreshToken)} |
| 82 |
> |
| 83 |
<ElementorFormSelectWrapper {...props} /> |
| 84 |
</BackgroudAppContext.Provider> |
| 85 |
); |
| 86 |
} |
| 87 |
|