form
7 years ago
scss
8 years ago
block.js
7 years ago
controls.js
7 years ago
inspector.js
7 years ago
block.js
98 lines
| 1 | /** |
| 2 | * External dependencies |
| 3 | */ |
| 4 | import { isEmpty, pickBy, isUndefined } from 'lodash'; |
| 5 | import { stringify } from 'querystringify'; |
| 6 | |
| 7 | /** |
| 8 | * WordPress dependencies |
| 9 | */ |
| 10 | const { __ } = wp.i18n; |
| 11 | const { withAPIData } = wp.components; |
| 12 | const { Component } = wp.element; |
| 13 | |
| 14 | /** |
| 15 | * Internal dependencies |
| 16 | */ |
| 17 | import GiveBlankSlate from '../../components/blank-slate'; |
| 18 | import NoForms from '../../components/no-form'; |
| 19 | import EditForm from '../../components/edit-form'; |
| 20 | import FormPreview from './form/preview'; |
| 21 | import FormSelect from '../../components/form-select'; |
| 22 | |
| 23 | /** |
| 24 | * Render Block UI For Editor |
| 25 | * |
| 26 | * @class GiveForm |
| 27 | * @extends {Component} |
| 28 | */ |
| 29 | class GiveForm extends Component { |
| 30 | constructor( props ) { |
| 31 | super( ...props ); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Render block UI |
| 36 | * |
| 37 | * @returns {object} JSX Object |
| 38 | * @memberof GiveForm |
| 39 | */ |
| 40 | render() { |
| 41 | const props = this.props, |
| 42 | attributes = props.attributes, |
| 43 | { isLoading } = props.form; |
| 44 | |
| 45 | // Render block UI |
| 46 | let blockUI; |
| 47 | |
| 48 | if ( ! attributes.id ) { |
| 49 | if ( isLoading || isUndefined( props.form.data ) ) { |
| 50 | blockUI = <GiveBlankSlate title={ __( 'Loading...' ) } isLoader={ true } />; |
| 51 | } else if ( isEmpty( props.form.data ) ) { |
| 52 | blockUI = <NoForms />; |
| 53 | } else { |
| 54 | blockUI = <FormSelect { ... { ...props } } />; |
| 55 | } |
| 56 | } else if ( isEmpty( props.form.data ) ) { |
| 57 | blockUI = isLoading ? |
| 58 | <GiveBlankSlate title={ __( 'Loading...' ) } isLoader={ true } /> : |
| 59 | <EditForm formId={ attributes.id } { ... { ...props } } />; |
| 60 | } else { |
| 61 | blockUI = <FormPreview |
| 62 | html={ props.form.data } |
| 63 | { ... { ...props } } />; |
| 64 | } |
| 65 | |
| 66 | return ( |
| 67 | <div className={ !! props.isSelected ? `${ props.className } isSelected` : props.className } key="GiveBlockUI"> |
| 68 | { blockUI } |
| 69 | </div> |
| 70 | ); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Export component attaching withAPIdata |
| 76 | */ |
| 77 | export default withAPIData( ( props ) => { |
| 78 | const { showTitle, showGoal, showContent, displayStyle, continueButtonTitle, id } = props.attributes; |
| 79 | |
| 80 | let parameters = { |
| 81 | show_title: showTitle, |
| 82 | show_goal: showGoal, |
| 83 | show_content: showContent, |
| 84 | display_style: displayStyle, |
| 85 | }; |
| 86 | |
| 87 | if ( 'reveal' === displayStyle ) { |
| 88 | parameters.continue_button_title = continueButtonTitle; |
| 89 | } |
| 90 | |
| 91 | parameters = stringify( pickBy( parameters, value => ! isUndefined( value ) ) ); |
| 92 | |
| 93 | return { |
| 94 | form: `/${ giveApiSettings.rest_base }/form/${ id }/?${ parameters }`, |
| 95 | forms: '/wp/v2/give_forms', |
| 96 | }; |
| 97 | } )( GiveForm ); |
| 98 |