index.js
58 lines
| 1 | /** |
| 2 | * External dependencies |
| 3 | */ |
| 4 | import { isUndefined } from 'lodash'; |
| 5 | |
| 6 | /** |
| 7 | * WordPress dependencies |
| 8 | */ |
| 9 | const { __ } = wp.i18n; |
| 10 | const { withSelect } = wp.data; |
| 11 | const { Placeholder, Spinner } = wp.components; |
| 12 | |
| 13 | /** |
| 14 | * Internal dependencies |
| 15 | */ |
| 16 | import './index.scss'; |
| 17 | import GiveBlankSlate from '../blank-slate'; |
| 18 | import NoForms from '../no-form'; |
| 19 | import ChosenSelect from '../chosen-select'; |
| 20 | import { getFormOptions } from '../../utils'; |
| 21 | |
| 22 | const SelectForm = ( { forms, setAttributes } ) => { |
| 23 | const setFormIdTo = id => { |
| 24 | setAttributes( { id: Number( id ) } ); |
| 25 | }; |
| 26 | |
| 27 | // Render Component UI |
| 28 | let componentUI; |
| 29 | |
| 30 | if ( ! forms ) { |
| 31 | componentUI = <Placeholder><Spinner /></Placeholder>; |
| 32 | } else if ( forms && forms.length === 0 ) { |
| 33 | componentUI = <NoForms />; |
| 34 | } else { |
| 35 | componentUI = ( |
| 36 | <GiveBlankSlate title={ __( 'Donation Form' ) }> |
| 37 | <ChosenSelect |
| 38 | className="give-blank-slate__select" |
| 39 | options={ getFormOptions( forms ) } |
| 40 | onChange={ setFormIdTo } |
| 41 | value={ 0 } |
| 42 | /> |
| 43 | </GiveBlankSlate> |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | return componentUI; |
| 48 | }; |
| 49 | |
| 50 | /** |
| 51 | * Export with forms data |
| 52 | */ |
| 53 | export default withSelect( ( select ) => { |
| 54 | return { |
| 55 | forms: select( 'core' ).getEntityRecords( 'postType', 'give_forms', { per_page: 30 } ), |
| 56 | }; |
| 57 | } )( SelectForm ); |
| 58 |