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