index.js
105 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 { SelectControl, Button, Placeholder, Spinner } = wp.components; |
| 12 | |
| 13 | /** |
| 14 | * Internal dependencies |
| 15 | */ |
| 16 | import { getSiteUrl } from '../../utils'; |
| 17 | import GiveBlankSlate from '../blank-slate'; |
| 18 | import NoForms from '../no-form'; |
| 19 | import ChosenSelect from '../chosen-select'; |
| 20 | |
| 21 | /** |
| 22 | * Render form select UI |
| 23 | */ |
| 24 | const giveFormOptionsDefault = { value: '0', label: __( '-- Select Form --' ) }; |
| 25 | |
| 26 | const SelectForm = ( { forms, attributes, setAttributes } ) => { |
| 27 | //Attributes |
| 28 | const { prevId } = attributes; |
| 29 | |
| 30 | // Event(s) |
| 31 | const getFormOptions = () => { |
| 32 | // Add API Data To Select Options |
| 33 | |
| 34 | let formOptions = []; |
| 35 | |
| 36 | if ( ! isUndefined( forms ) ) { |
| 37 | formOptions = forms.map( |
| 38 | ( { id, title: { rendered: title } } ) => { |
| 39 | return { |
| 40 | value: id, |
| 41 | label: title === '' ? `${ id } : ${ __( 'No form title' ) }` : title, |
| 42 | }; |
| 43 | } |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | // Add Default option |
| 48 | formOptions.unshift( giveFormOptionsDefault ); |
| 49 | |
| 50 | return formOptions; |
| 51 | }; |
| 52 | |
| 53 | const setFormIdTo = id => { |
| 54 | setAttributes( { id: Number( id ) } ); |
| 55 | }; |
| 56 | |
| 57 | const resetFormIdTo = () => { |
| 58 | setAttributes( { id: Number( prevId ) } ); |
| 59 | setAttributes( { prevId: undefined } ); |
| 60 | }; |
| 61 | |
| 62 | // Render Component UI |
| 63 | let componentUI; |
| 64 | |
| 65 | if ( ! forms ) { |
| 66 | componentUI = <Placeholder><Spinner /></Placeholder>; |
| 67 | } else if ( forms && forms.length === 0 ) { |
| 68 | componentUI = <NoForms />; |
| 69 | } else { |
| 70 | componentUI = ( |
| 71 | <GiveBlankSlate title={ __( 'Donation Form' ) }> |
| 72 | <ChosenSelect |
| 73 | className="give-blank-slate__select" |
| 74 | options={ getFormOptions() } |
| 75 | onChange={ setFormIdTo } |
| 76 | /> |
| 77 | |
| 78 | <Button isPrimary |
| 79 | isLarge href={ `${ getSiteUrl() }/wp-admin/post-new.php?post_type=give_forms` }> |
| 80 | { __( 'Add New Form' ) } |
| 81 | </Button> |
| 82 | |
| 83 | { |
| 84 | prevId && |
| 85 | <Button isLarge |
| 86 | onClick={ resetFormIdTo }> |
| 87 | { __( 'Cancel' ) } |
| 88 | </Button> |
| 89 | } |
| 90 | </GiveBlankSlate> |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | return componentUI; |
| 95 | }; |
| 96 | |
| 97 | /** |
| 98 | * Export with forms data |
| 99 | */ |
| 100 | export default withSelect( ( select ) => { |
| 101 | return { |
| 102 | forms: select( 'core' ).getEntityRecords( 'postType', 'give_forms', { per_page: 30 } ), |
| 103 | }; |
| 104 | } )( SelectForm ); |
| 105 |