block.js
79 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 FormGridPreview from './components/preview'; |
| 20 | |
| 21 | /** |
| 22 | * Render Block UI For Editor |
| 23 | * |
| 24 | * @class GiveDonationFormGrid |
| 25 | * @extends {Component} |
| 26 | */ |
| 27 | class GiveDonationFormGrid extends Component { |
| 28 | constructor( props ) { |
| 29 | super( ...props ); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Render block UI |
| 34 | * |
| 35 | * @returns {object} JSX Object |
| 36 | * @memberof GiveDonationFormGrid |
| 37 | */ |
| 38 | render() { |
| 39 | const props = this.props, |
| 40 | { latestForms } = props, |
| 41 | { isLoading } = latestForms; |
| 42 | |
| 43 | // Render block UI |
| 44 | let blockUI; |
| 45 | |
| 46 | if ( isLoading || isUndefined( latestForms.data ) ) { |
| 47 | blockUI = <GiveBlankSlate title={ __( 'Loading...' ) } isLoader={ true } />; |
| 48 | } else if ( isEmpty( latestForms.data ) ) { |
| 49 | blockUI = <NoForms />; |
| 50 | } else { |
| 51 | blockUI = <FormGridPreview |
| 52 | html={ latestForms.data } |
| 53 | { ... { ...props } } />; |
| 54 | } |
| 55 | |
| 56 | return ( <div className={ props.className } key="GiveDonationFormGridBlockUI">{ blockUI }</div> ); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Export component attaching withAPIdata |
| 62 | */ |
| 63 | export default withAPIData( ( props ) => { |
| 64 | const { columns, showGoal, showExcerpt, showFeaturedImage, displayType } = props.attributes; |
| 65 | |
| 66 | const parameters = stringify( pickBy( { |
| 67 | columns: columns, |
| 68 | show_goal: showGoal, |
| 69 | show_excerpt: showExcerpt, |
| 70 | show_featured_image: showFeaturedImage, |
| 71 | display_type: displayType, |
| 72 | }, value => ! isUndefined( value ) |
| 73 | ) ); |
| 74 | |
| 75 | return { |
| 76 | latestForms: `/${ giveApiSettings.rest_base }/form-grid/?${ parameters }`, |
| 77 | }; |
| 78 | } )( GiveDonationFormGrid ); |
| 79 |