HtmlParserButton.js
1 year ago
HtmlParserFooter.js
1 year ago
HtmlParserModal.js
1 year ago
index.js
1 year ago
serialize.js
1 year ago
HtmlParserFooter.js
68 lines
| 1 | import { Button, Flex } from '@wordpress/components'; |
| 2 | import { __ } from '@wordpress/i18n'; |
| 3 | import apiFetch from '@wordpress/api-fetch'; |
| 4 | import { useState } from '@wordpress/element'; |
| 5 | import serialize from './serialize'; |
| 6 | |
| 7 | const { parseHTMLtoBlocks } = JetFormBuilderParser; |
| 8 | |
| 9 | const getPostEditUrl = id => { |
| 10 | const url = new URL( JetFormBuilderAdmin.edit_url ); |
| 11 | url.searchParams.set( 'post', id ); |
| 12 | |
| 13 | return url.href; |
| 14 | }; |
| 15 | |
| 16 | export default function HtmlParserFooter( { clearHTML, rawHTML, setShowModal } ) { |
| 17 | const [ isBusy, setIsBusy ] = useState( false ); |
| 18 | const createForm = async () => { |
| 19 | setIsBusy( true ); |
| 20 | try { |
| 21 | const blocks = parseHTMLtoBlocks( rawHTML ); |
| 22 | |
| 23 | if ( !blocks.length ) { |
| 24 | console.error( __( 'JFB: Could not parse blocks', 'jet-form-builder' ), rawHTML ); |
| 25 | setIsBusy( false ); |
| 26 | return; |
| 27 | } |
| 28 | |
| 29 | const serialized = serialize( blocks ); |
| 30 | |
| 31 | const response = await apiFetch( { |
| 32 | method: 'POST', |
| 33 | path: '/wp/v2/jet-form-builder', |
| 34 | data: { |
| 35 | title: __( 'Imported HTML Form', 'jet-form-builder' ) + ' ' + new Date().toLocaleString('sv-SE').replace('T', ' '), |
| 36 | content: serialized, |
| 37 | status: 'publish', |
| 38 | }, |
| 39 | } ); |
| 40 | |
| 41 | window.location.href = getPostEditUrl( response.id ); |
| 42 | |
| 43 | } catch ( error ) { |
| 44 | console.error( 'Failed to create form:', error ); |
| 45 | setIsBusy( false ); |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | return ( |
| 50 | <Flex justify="space-between"> |
| 51 | <Button |
| 52 | variant="secondary" |
| 53 | onClick={ clearHTML } |
| 54 | > |
| 55 | { __( 'Back', 'jet-form-builder' ) } |
| 56 | </Button> |
| 57 | <Button |
| 58 | variant="primary" |
| 59 | onClick={ createForm } |
| 60 | isBusy={ isBusy } |
| 61 | disabled={ isBusy } |
| 62 | > |
| 63 | { __( 'Create Form', 'jet-form-builder' ) } |
| 64 | </Button> |
| 65 | </Flex> |
| 66 | ); |
| 67 | } |
| 68 |