index.js
78 lines
| 1 | /** |
| 2 | * External dependencies |
| 3 | */ |
| 4 | import { isUndefined } from 'lodash'; |
| 5 | |
| 6 | /** |
| 7 | * WordPress dependencies |
| 8 | */ |
| 9 | const { __ } = wp.i18n; |
| 10 | const { SelectControl, Button } = wp.components; |
| 11 | |
| 12 | /** |
| 13 | * Internal dependencies |
| 14 | */ |
| 15 | import GiveBlankSlate from '../blank-slate'; |
| 16 | |
| 17 | /** |
| 18 | * Render form select UI |
| 19 | */ |
| 20 | const giveFormOptionsDefault = { value: '0', label: __( '-- Select Form --' ) }; |
| 21 | |
| 22 | const FormSelect = ( props ) => { |
| 23 | // Event(s) |
| 24 | const getFormOptions = () => { |
| 25 | // Add API Data To Select Options |
| 26 | |
| 27 | let formOptions = []; |
| 28 | |
| 29 | if ( ! isUndefined( props.forms.data ) ) { |
| 30 | formOptions = props.forms.data.map( |
| 31 | ( form ) => { |
| 32 | return { |
| 33 | value: form.id, |
| 34 | label: form.title.rendered === '' ? `${ form.id } : ${ __( 'No form title' ) }` : form.title.rendered, |
| 35 | }; |
| 36 | } |
| 37 | ); |
| 38 | } |
| 39 | // Add Default option |
| 40 | formOptions.unshift( giveFormOptionsDefault ); |
| 41 | |
| 42 | return formOptions; |
| 43 | }; |
| 44 | |
| 45 | const setFormIdTo = id => { |
| 46 | props.setAttributes( { id } ); |
| 47 | }; |
| 48 | |
| 49 | const resetFormIdTo = () => { |
| 50 | props.setAttributes( { id: props.attributes.prevId } ); |
| 51 | props.setAttributes( { prevId: 0 } ); |
| 52 | }; |
| 53 | |
| 54 | return ( |
| 55 | <GiveBlankSlate title={ __( 'Give Donation form' ) }> |
| 56 | <SelectControl |
| 57 | options={ getFormOptions() } |
| 58 | onChange={ setFormIdTo } |
| 59 | /> |
| 60 | |
| 61 | <Button isPrimary |
| 62 | isLarge href={ `${ wpApiSettings.schema.url }/wp-admin/post-new.php?post_type=give_forms` }> |
| 63 | { __( 'Add New Form' ) } |
| 64 | </Button> |
| 65 | |
| 66 | { |
| 67 | props.attributes.prevId && |
| 68 | <Button isLarge |
| 69 | onClick={ resetFormIdTo }> |
| 70 | { __( 'Cancel' ) } |
| 71 | </Button> |
| 72 | } |
| 73 | </GiveBlankSlate> |
| 74 | ); |
| 75 | }; |
| 76 | |
| 77 | export default FormSelect; |
| 78 |