|
| 1 | +/** |
| 2 | + * WordPress dependencies |
| 3 | + */ |
| 4 | +import { |
| 5 | + useBlockProps, |
| 6 | + BlockControls, |
| 7 | + InspectorControls, |
| 8 | + RichText, |
| 9 | +} from '@wordpress/block-editor'; |
| 10 | +import { select } from '@wordpress/data'; |
| 11 | +import { |
| 12 | + Placeholder, |
| 13 | + ToolbarGroup, |
| 14 | + Spinner, |
| 15 | + PanelBody, |
| 16 | + Button, |
| 17 | +} from '@wordpress/components'; |
| 18 | +import { useEffect, useState } from '@wordpress/element'; |
| 19 | +import { postList, paragraph } from '@wordpress/icons'; |
| 20 | +import { __ } from '@wordpress/i18n'; |
| 21 | +import apiFetch from '@wordpress/api-fetch'; |
| 22 | + |
| 23 | +/** |
| 24 | + * Internal dependencies |
| 25 | + */ |
| 26 | +import { ReactComponent as icon } from '../../../../assets/img/block-icon.svg'; |
| 27 | + |
| 28 | +const BlockEdit = ( props ) => { |
| 29 | + const [ isLoading, setIsLoading ] = useState( false ); |
| 30 | + const [ run, setRun ] = useState( false ); |
| 31 | + const { attributes, setAttributes } = props; |
| 32 | + const { render, takeaways, title } = attributes; |
| 33 | + const blockProps = useBlockProps(); |
| 34 | + |
| 35 | + useEffect( () => { |
| 36 | + if ( ( ! isLoading && takeaways.length === 0 ) || run ) { |
| 37 | + const postId = select( 'core/editor' ).getCurrentPostId(); |
| 38 | + const postContent = |
| 39 | + select( 'core/editor' ).getEditedPostAttribute( 'content' ); |
| 40 | + const postTitle = |
| 41 | + select( 'core/editor' ).getEditedPostAttribute( 'title' ); |
| 42 | + |
| 43 | + setRun( false ); |
| 44 | + setIsLoading( true ); |
| 45 | + |
| 46 | + apiFetch( { |
| 47 | + path: '/classifai/v1/key-takeaways/', |
| 48 | + method: 'POST', |
| 49 | + data: { |
| 50 | + id: postId, |
| 51 | + content: postContent, |
| 52 | + title: postTitle, |
| 53 | + render, |
| 54 | + }, |
| 55 | + } ).then( |
| 56 | + async ( res ) => { |
| 57 | + // Ensure takeaways is always an array. |
| 58 | + if ( ! Array.isArray( res ) ) { |
| 59 | + res = [ res ]; |
| 60 | + } |
| 61 | + |
| 62 | + setAttributes( { takeaways: res } ); |
| 63 | + setIsLoading( false ); |
| 64 | + }, |
| 65 | + ( err ) => { |
| 66 | + setAttributes( { |
| 67 | + takeaways: [ `Error: ${ err?.message }` ], |
| 68 | + } ); |
| 69 | + setIsLoading( false ); |
| 70 | + } |
| 71 | + ); |
| 72 | + } |
| 73 | + }, [ run ] ); |
| 74 | + |
| 75 | + const renderControls = [ |
| 76 | + { |
| 77 | + icon: postList, |
| 78 | + title: __( 'List view', 'classifai' ), |
| 79 | + onClick: () => setAttributes( { render: 'list' } ), |
| 80 | + isActive: render === 'list', |
| 81 | + }, |
| 82 | + { |
| 83 | + icon: paragraph, |
| 84 | + title: __( 'Paragraph view', 'classifai' ), |
| 85 | + onClick: () => setAttributes( { render: 'paragraph' } ), |
| 86 | + isActive: render === 'paragraph', |
| 87 | + }, |
| 88 | + ]; |
| 89 | + |
| 90 | + const editTakeaways = ( index, value ) => { |
| 91 | + const newTakeaways = [ ...takeaways ]; |
| 92 | + |
| 93 | + if ( ! value ) { |
| 94 | + newTakeaways.splice( index, 1 ); |
| 95 | + } else { |
| 96 | + newTakeaways[ index ] = value; |
| 97 | + } |
| 98 | + |
| 99 | + setAttributes( { |
| 100 | + takeaways: newTakeaways, |
| 101 | + } ); |
| 102 | + }; |
| 103 | + |
| 104 | + return ( |
| 105 | + <> |
| 106 | + <BlockControls> |
| 107 | + <ToolbarGroup controls={ renderControls } /> |
| 108 | + </BlockControls> |
| 109 | + <InspectorControls> |
| 110 | + <PanelBody title={ __( 'Settings', 'classifai' ) }> |
| 111 | + <Button |
| 112 | + label={ __( 'Re-generate key takeaways', 'classifai' ) } |
| 113 | + text={ __( 'Refresh results', 'classifai' ) } |
| 114 | + variant={ 'secondary' } |
| 115 | + onClick={ () => setRun( true ) } |
| 116 | + isBusy={ isLoading } |
| 117 | + /> |
| 118 | + </PanelBody> |
| 119 | + </InspectorControls> |
| 120 | + |
| 121 | + { isLoading && ( |
| 122 | + <Placeholder |
| 123 | + icon={ icon } |
| 124 | + label={ __( 'Generating Key Takeaways', 'classifai' ) } |
| 125 | + > |
| 126 | + <Spinner |
| 127 | + style={ { |
| 128 | + height: 'calc(4px * 10)', |
| 129 | + width: 'calc(4px * 10)', |
| 130 | + } } |
| 131 | + /> |
| 132 | + </Placeholder> |
| 133 | + ) } |
| 134 | + |
| 135 | + { ! isLoading && ( |
| 136 | + <div { ...blockProps }> |
| 137 | + <RichText |
| 138 | + tagName="h2" |
| 139 | + className="wp-block-heading wp-block-classifai-key-takeaways__title" |
| 140 | + value={ title } |
| 141 | + onChange={ ( value ) => |
| 142 | + setAttributes( { title: value } ) |
| 143 | + } |
| 144 | + placeholder="Key Takeaways" |
| 145 | + /> |
| 146 | + <div |
| 147 | + className="wp-block-classifai-key-takeways__content" |
| 148 | + style={ { fontStyle: 'italic' } } |
| 149 | + > |
| 150 | + { render === 'list' && ( |
| 151 | + <ul> |
| 152 | + { takeaways.map( ( takeaway, index ) => ( |
| 153 | + <RichText |
| 154 | + tagName="li" |
| 155 | + value={ takeaway } |
| 156 | + key={ index } |
| 157 | + onChange={ ( value ) => |
| 158 | + editTakeaways( index, value ) |
| 159 | + } |
| 160 | + /> |
| 161 | + ) ) } |
| 162 | + </ul> |
| 163 | + ) } |
| 164 | + { render === 'paragraph' && ( |
| 165 | + <> |
| 166 | + { takeaways.map( ( takeaway, index ) => ( |
| 167 | + <RichText |
| 168 | + tagName="p" |
| 169 | + value={ takeaway } |
| 170 | + key={ index } |
| 171 | + onChange={ ( value ) => |
| 172 | + editTakeaways( index, value ) |
| 173 | + } |
| 174 | + /> |
| 175 | + ) ) } |
| 176 | + </> |
| 177 | + ) } |
| 178 | + </div> |
| 179 | + </div> |
| 180 | + ) } |
| 181 | + </> |
| 182 | + ); |
| 183 | +}; |
| 184 | + |
| 185 | +export default BlockEdit; |
0 commit comments