components
2 years ago
hooks
2 years ago
images
2 years ago
styles
2 years ago
types
2 years ago
Edit.tsx
2 years ago
Edit.tsx
175 lines
| 1 | import {__} from '@wordpress/i18n'; |
| 2 | import {InspectorControls, useBlockProps} from '@wordpress/block-editor'; |
| 3 | import {ExternalLink, PanelBody, PanelRow, SelectControl, TextControl} from '@wordpress/components'; |
| 4 | import {Fragment, useCallback, useEffect, useState} from '@wordpress/element'; |
| 5 | import useFormOptions from './hooks/useFormOptions'; |
| 6 | import ConfirmButton from './components/ConfirmButton'; |
| 7 | import Logo from './components/Logo'; |
| 8 | import {BlockEditProps} from '@wordpress/blocks'; |
| 9 | import ReactSelect from 'react-select'; |
| 10 | import BlockPreview from './components/BlockPreview'; |
| 11 | |
| 12 | import './styles/index.scss'; |
| 13 | |
| 14 | /** |
| 15 | * @since 3.0.0 |
| 16 | */ |
| 17 | export default function Edit({clientId, attributes, setAttributes}: BlockEditProps<any>) { |
| 18 | const {formId, blockId, formFormat, openFormButton} = attributes; |
| 19 | const {formOptions, isResolving} = useFormOptions(); |
| 20 | const [showPreview, setShowPreview] = useState<boolean>(!!formId); |
| 21 | |
| 22 | const showOpenFormButton = formFormat === 'reveal' || formFormat === 'modal'; |
| 23 | |
| 24 | useEffect(() => { |
| 25 | if (!blockId) { |
| 26 | setAttributes({blockId: clientId}); |
| 27 | } |
| 28 | }, []); |
| 29 | |
| 30 | const getDefaultFormId = useCallback(() => { |
| 31 | if (!isResolving && formOptions.length > 0) { |
| 32 | return formId && formOptions?.find(({value}) => value === formId); |
| 33 | } |
| 34 | }, [isResolving, formId, JSON.stringify(formOptions)]); |
| 35 | |
| 36 | return ( |
| 37 | <Fragment> |
| 38 | {/*block controls*/} |
| 39 | <InspectorControls> |
| 40 | <PanelBody title={__('Form Settings', 'give')} initialOpen={true}> |
| 41 | <PanelRow> |
| 42 | {!isResolving && formOptions.length === 0 ? ( |
| 43 | <p>{__('No forms were found using the GiveWP form builder.', 'give')}</p> |
| 44 | ) : ( |
| 45 | <SelectControl |
| 46 | label={__('Choose a donation form', 'give')} |
| 47 | value={formId ?? ''} |
| 48 | options={[ |
| 49 | // add a disabled selector manually |
| 50 | ...[{value: '', label: __('Select...', 'give'), disabled: true}], |
| 51 | ...formOptions, |
| 52 | ]} |
| 53 | onChange={(newFormId) => { |
| 54 | setAttributes({formId: newFormId}); |
| 55 | }} |
| 56 | /> |
| 57 | )} |
| 58 | </PanelRow> |
| 59 | <PanelRow> |
| 60 | <SelectControl |
| 61 | label={__('Form Format', 'give')} |
| 62 | value={formFormat} |
| 63 | options={[ |
| 64 | { |
| 65 | label: __('Full Form', 'give'), |
| 66 | value: 'full', |
| 67 | }, |
| 68 | { |
| 69 | label: __('Reveal', 'give'), |
| 70 | value: 'reveal', |
| 71 | }, |
| 72 | { |
| 73 | label: __('Modal', 'give'), |
| 74 | value: 'modal', |
| 75 | }, |
| 76 | ]} |
| 77 | onChange={(value) => { |
| 78 | setAttributes({formFormat: value}); |
| 79 | }} |
| 80 | /> |
| 81 | </PanelRow> |
| 82 | {showOpenFormButton && ( |
| 83 | <PanelRow> |
| 84 | <TextControl |
| 85 | label={__('Open Form Button', 'give')} |
| 86 | value={openFormButton} |
| 87 | onChange={(value) => { |
| 88 | setAttributes({openFormButton: value}); |
| 89 | }} |
| 90 | /> |
| 91 | </PanelRow> |
| 92 | )} |
| 93 | <PanelRow> |
| 94 | {formId && ( |
| 95 | <ExternalLink |
| 96 | href={`/wp-admin/edit.php?post_type=give_forms&page=givewp-form-builder&donationFormID=${formId}`} |
| 97 | > |
| 98 | {__('Edit donation form', 'give')} |
| 99 | </ExternalLink> |
| 100 | )} |
| 101 | </PanelRow> |
| 102 | </PanelBody> |
| 103 | </InspectorControls> |
| 104 | |
| 105 | {/*block preview*/} |
| 106 | <div {...useBlockProps()}> |
| 107 | {formId && showPreview ? ( |
| 108 | <BlockPreview |
| 109 | clientId={clientId} |
| 110 | formId={formId} |
| 111 | formFormat={formFormat} |
| 112 | openFormButton={openFormButton} |
| 113 | /> |
| 114 | ) : ( |
| 115 | <div className="givewp-form-block--container"> |
| 116 | <Logo /> |
| 117 | |
| 118 | <div className="givewp-form-block__select--container"> |
| 119 | <label htmlFor="formId" className="givewp-form-block__select--label"> |
| 120 | {__('Choose a donation form', 'give')} |
| 121 | </label> |
| 122 | |
| 123 | <ReactSelect |
| 124 | classNamePrefix="givewp-form-block__select" |
| 125 | name="formId" |
| 126 | inputId="formId" |
| 127 | value={getDefaultFormId()} |
| 128 | placeholder={ |
| 129 | isResolving ? __('Loading Donation Forms...', 'give') : __('Select...', 'give') |
| 130 | } |
| 131 | onChange={(option) => { |
| 132 | if (option) { |
| 133 | setAttributes({formId: option.value}); |
| 134 | } |
| 135 | }} |
| 136 | noOptionsMessage={() => ( |
| 137 | <p>{__('No forms were found using the GiveWP form builder.', 'give')}</p> |
| 138 | )} |
| 139 | options={formOptions} |
| 140 | loadingMessage={() => <>{__('Loading Donation Forms...', 'give')}</>} |
| 141 | isLoading={isResolving} |
| 142 | theme={(theme) => ({ |
| 143 | ...theme, |
| 144 | colors: { |
| 145 | ...theme.colors, |
| 146 | primary: '#27ae60', |
| 147 | }, |
| 148 | })} |
| 149 | styles={{ |
| 150 | input: (provided, state) => ({ |
| 151 | ...provided, |
| 152 | height: '3rem', |
| 153 | }), |
| 154 | option: (provided, state) => ({ |
| 155 | ...provided, |
| 156 | paddingTop: '0.8rem', |
| 157 | paddingBottom: '0.8rem', |
| 158 | fontSize: '1rem', |
| 159 | }), |
| 160 | control: (provided, state) => ({ |
| 161 | ...provided, |
| 162 | fontSize: '1rem', |
| 163 | }), |
| 164 | }} |
| 165 | /> |
| 166 | </div> |
| 167 | |
| 168 | <ConfirmButton formId={formId} enablePreview={() => setShowPreview(true)} /> |
| 169 | </div> |
| 170 | )} |
| 171 | </div> |
| 172 | </Fragment> |
| 173 | ); |
| 174 | } |
| 175 |