edit.js
154 lines
| 1 | import './editor.scss'; |
| 2 | import { __ } from '@wordpress/i18n'; |
| 3 | import { SelectControl } from '@wordpress/components'; |
| 4 | import { useBlockProps, InspectorControls } from '@wordpress/block-editor'; |
| 5 | |
| 6 | const { useState, useEffect } = wp.element; |
| 7 | import apiFetch from '@wordpress/api-fetch'; |
| 8 | |
| 9 | let formOptions = []; |
| 10 | let usernames = []; |
| 11 | const controller = |
| 12 | typeof AbortController === 'undefined' ? undefined : new AbortController(); |
| 13 | |
| 14 | export default function Edit( { attributes, setAttributes } ) { |
| 15 | if ( ! attributes.path ) { |
| 16 | setAttributes( { path: '' } ); |
| 17 | } |
| 18 | |
| 19 | const [ formOption, setFormOption ] = useState( attributes.path ); |
| 20 | const [ usernameOption, setUsernameOption] = useState ( 'all' ); |
| 21 | const [ isLoaded, setIsLoaded ] = useState( false ); |
| 22 | const blockProps = useBlockProps(); |
| 23 | |
| 24 | const selectedForm = formOptions.find( ( formOption ) => { |
| 25 | return attributes.path !== '' && formOption.value === attributes.path; |
| 26 | } ); |
| 27 | |
| 28 | useEffect( () => { |
| 29 | apiFetch( { |
| 30 | path: '/?rest_route=/fapi/v1/list-forms/' + encodeURIComponent(usernameOption), |
| 31 | signal: controller?.signal, |
| 32 | } ) |
| 33 | .then( ( forms ) => { |
| 34 | formOptions = forms; |
| 35 | formOptions.unshift( { |
| 36 | label: __( |
| 37 | '-- vyberte prodejní formulář --', |
| 38 | 'fapi-member' |
| 39 | ), |
| 40 | value: '', |
| 41 | } ); |
| 42 | setIsLoaded( true ); |
| 43 | } ) |
| 44 | .catch( ( error ) => { |
| 45 | formOptions = []; |
| 46 | console.error( error ); |
| 47 | // If the browser doesn't support AbortController then the code below will never log. |
| 48 | // However, in most cases this should be fine as it can be considered to be a progressive enhancement. |
| 49 | if ( error.name === 'AbortError' ) { |
| 50 | console.error( 'Request has been aborted' ); |
| 51 | } |
| 52 | }) |
| 53 | }, [usernameOption] ); |
| 54 | |
| 55 | useEffect( () => { |
| 56 | apiFetch( { |
| 57 | path: '/?rest_route=/fapi/v1/list-users', |
| 58 | } ) |
| 59 | .then( ( fapiCredentials ) => { |
| 60 | usernames = JSON.parse(fapiCredentials) |
| 61 | usernames.unshift( { |
| 62 | label: __('(všechny propojené účty)', 'fapi-member'), |
| 63 | value: 'all', |
| 64 | } ); |
| 65 | }) |
| 66 | .catch( ( error ) => { |
| 67 | usernames = []; |
| 68 | console.error( error ); |
| 69 | if ( error.name === 'AbortError' ) { |
| 70 | console.error( 'Request has been aborted' ); |
| 71 | } |
| 72 | } ); |
| 73 | }, ); |
| 74 | |
| 75 | if (!isLoaded){ |
| 76 | return ( |
| 77 | <div { ...blockProps } style={ { 'text-align': 'center' } }> |
| 78 | <InspectorControls key="setting"> |
| 79 | <div id="fapi-form-controls"> |
| 80 | <SelectControl |
| 81 | label={ __( |
| 82 | 'Vyberte účet FAPI', |
| 83 | 'fapi-member' |
| 84 | ) } |
| 85 | value={ usernameOption } |
| 86 | options={ usernames } |
| 87 | /> |
| 88 | <SelectControl |
| 89 | label={ __( |
| 90 | 'Vyberte prodejní formulář', |
| 91 | 'fapi-member' |
| 92 | ) } |
| 93 | value={ formOption } |
| 94 | options={ formOptions } |
| 95 | disabled={ true } |
| 96 | /> |
| 97 | </div> |
| 98 | </InspectorControls> |
| 99 | {__('Načítání dat, počkejte prosím...', 'fapi-member')} |
| 100 | </div> |
| 101 | ) |
| 102 | } |
| 103 | |
| 104 | return ( |
| 105 | <div { ...blockProps } style={ { 'text-align': 'center' } }> |
| 106 | <InspectorControls key="setting"> |
| 107 | <div id="fapi-form-controls"> |
| 108 | <SelectControl |
| 109 | label={ __( |
| 110 | 'Vyberte účet FAPI', |
| 111 | 'fapi-member' |
| 112 | ) } |
| 113 | value={ usernameOption } |
| 114 | options={ usernames } |
| 115 | onChange={ ( value ) => { |
| 116 | setIsLoaded( false ) |
| 117 | setUsernameOption( value ) |
| 118 | } } |
| 119 | /> |
| 120 | <SelectControl |
| 121 | label={ __( |
| 122 | 'Vyberte prodejní formulář', |
| 123 | 'fapi-member' |
| 124 | ) } |
| 125 | value={ formOption } |
| 126 | options={ formOptions } |
| 127 | onChange={ ( value ) => { |
| 128 | setFormOption( value ); |
| 129 | setAttributes( { |
| 130 | path: value, |
| 131 | newPath: value, |
| 132 | } ); |
| 133 | |
| 134 | } } |
| 135 | /> |
| 136 | </div> |
| 137 | </InspectorControls> |
| 138 | { isLoaded } |
| 139 | { selectedForm |
| 140 | ? sprintf( |
| 141 | __( |
| 142 | '<<< Zde bude prodejní formulář "%s" >>>', |
| 143 | 'fapi-member' |
| 144 | ), |
| 145 | selectedForm.label |
| 146 | ) |
| 147 | : __( |
| 148 | '<<< Vyberte prodejní formulář z bočního menu >>>', |
| 149 | 'fapi-member' |
| 150 | ) } |
| 151 | </div> |
| 152 | ); |
| 153 | } |
| 154 |