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