block.js
77 lines
| 1 | /** |
| 2 | * Block dependencies |
| 3 | */ |
| 4 | import isEmpty from 'lodash.isempty'; |
| 5 | import pickBy from 'lodash.pickby'; |
| 6 | import isUndefined from 'lodash.isundefined'; |
| 7 | import GiveBlankSlate from '../../components/blank-slate/index'; |
| 8 | import NoForms from '../../components/no-form/index'; |
| 9 | import FormGridPreview from './components/preview'; |
| 10 | import {stringify} from 'querystringify'; |
| 11 | |
| 12 | /** |
| 13 | * Internal dependencies |
| 14 | */ |
| 15 | const {__} = wp.i18n, |
| 16 | {withAPIData} = wp.components, |
| 17 | {Component} = wp.element; |
| 18 | |
| 19 | /** |
| 20 | * Render Block UI For Editor |
| 21 | * |
| 22 | * @class GiveDonationFormGrid |
| 23 | * @extends {Component} |
| 24 | */ |
| 25 | class GiveDonationFormGrid extends Component { |
| 26 | constructor(props) { |
| 27 | super(...props); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Render block UI |
| 32 | * |
| 33 | * @returns {object} JSX Object |
| 34 | * @memberof GiveDonationFormGrid |
| 35 | */ |
| 36 | render() { |
| 37 | const props = this.props, |
| 38 | {latestForms} = props, |
| 39 | {isLoading} = latestForms; |
| 40 | |
| 41 | // Render block UI |
| 42 | let blockUI; |
| 43 | |
| 44 | if (isLoading || isUndefined(latestForms.data)) { |
| 45 | blockUI = <GiveBlankSlate title={__('Loading...')} isLoader={true}/>; |
| 46 | } else if (isEmpty(latestForms.data)) { |
| 47 | blockUI = <NoForms/>; |
| 48 | } else { |
| 49 | blockUI = <FormGridPreview |
| 50 | html={latestForms.data} |
| 51 | {... {...props}} />; |
| 52 | } |
| 53 | |
| 54 | return (<div className={props.className} key="GiveDonationFormGridBlockUI">{blockUI}</div>); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Export component attaching withAPIdata |
| 60 | */ |
| 61 | export default withAPIData((props) => { |
| 62 | const {columns, showGoal, showExcerpt, showFeaturedImage, displayType} = props.attributes; |
| 63 | |
| 64 | const parameters = stringify(pickBy({ |
| 65 | columns: columns, |
| 66 | show_goal: showGoal, |
| 67 | show_excerpt: showExcerpt, |
| 68 | show_featured_image: showFeaturedImage, |
| 69 | display_type: displayType |
| 70 | }, value => !isUndefined(value) |
| 71 | )); |
| 72 | |
| 73 | return { |
| 74 | latestForms: `/${giveApiSettings.rest_base}/form-grid/?${ parameters }`, |
| 75 | }; |
| 76 | })(GiveDonationFormGrid); |
| 77 |