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