Skip to content

Commit

Permalink
Add a beta feedback form to the Site Editor (#56667)
Browse files Browse the repository at this point in the history
* initial copy pasta

* pass configData to editor iframe

* have a portMessage trigger the feedback sender

* it works! kind of...

* connect necessary selectors

* update confimation notice

* fix post editor error

* actually fix post editor

* restructure files

* make feedback function more general

* update testing comment goo

* use imported package

* remove testing goo

* update copies
  • Loading branch information
Addison-Stavlo committed Oct 7, 2021
1 parent 18b176c commit 48fe031
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/* global MessageChannel */
import { TextareaControl, Button, PanelBody, Notice } from '@wordpress/components';
import { useState, useEffect } from '@wordpress/element';
import { __, _x } from '@wordpress/i18n';

export default function FseBetaFeedbackForm( { calypsoPort } ) {
const [ confirmation, setConfirmation ] = useState( null );
const [ isSubmitting, setIsSubmitting ] = useState( false );
const [ message, setMessage ] = useState( '' );

useEffect( () => {
// Reset confirmation if new message is input.
if ( confirmation && ! confirmation.isError && message.length ) {
setConfirmation( null );
}
}, [ confirmation, message ] );

const submitFeedback = () => {
setIsSubmitting( true );

const { port1, port2 } = new MessageChannel();
calypsoPort.postMessage( { action: 'sendSiteEditorBetaFeedback', payload: message }, [
port2,
] );

port1.onmessage = ( { data } ) => {
setIsSubmitting( false );

if ( 'success' === data ) {
setMessage( '' );
setConfirmation( {
title: __( 'Got it!' ),
message: __( 'Thank you for helping us make WordPress.com awesome.' ),
isError: false,
} );
} else if ( 'error' === data ) {
setConfirmation( {
title: __( 'Error!' ),
message: __(
"We're sorry, there was an error in sending your feedback. Please try again later."
),
isError: true,
} );
}

port1.close();
};
};

return (
<>
<PanelBody>
<TextareaControl
disabled={ isSubmitting }
onChange={ setMessage }
placeholder={ __(
'How can we improve your site editing experience?',
'full-site-editing'
) }
value={ message }
label={ __( 'Leave feedback (optional)', 'full-site-editing' ) }
rows={ 12 }
/>
<Button
disabled={ isSubmitting || ! message }
type="button"
isPrimary
onClick={ submitFeedback }
>
{ isSubmitting
? __( 'Sending…', 'full-site-editing' )
: _x( 'Send', 'verb: imperative', 'full-site-editing' ) }
</Button>
</PanelBody>
{ confirmation && (
<Notice status={ confirmation?.isError ? 'error' : 'success' } isDismissible={ false }>
<h1>{ confirmation.title }</h1>
<p>{ confirmation.message }</p>
</Notice>
) }
</>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { dispatch, select, subscribe, use } from '@wordpress/data';
import { __experimentalMainDashboardButton as MainDashboardButton } from '@wordpress/edit-post';
import { addAction, addFilter, doAction, removeAction } from '@wordpress/hooks';
import { __ } from '@wordpress/i18n';
import { wordpress } from '@wordpress/icons';
import { comment, wordpress } from '@wordpress/icons';
import { registerPlugin } from '@wordpress/plugins';
import { addQueryArgs, getQueryArg } from '@wordpress/url';
import debugFactory from 'debug';
Expand All @@ -19,6 +19,7 @@ import { Component, useEffect, useState } from 'react';
import tinymce from 'tinymce/tinymce';
import { STORE_KEY as NAV_SIDEBAR_STORE_KEY } from '../../../../editing-toolkit/editing-toolkit-plugin/wpcom-block-editor-nav-sidebar/src/constants';
import { inIframe, isEditorReadyWithBlocks, sendMessage, getPages } from '../../utils';
import FeedbackForm from './fse-beta/feedback-form';
/**
* Conditional dependency. We cannot use the standard 'import' since this package is
* not available in the post editor and causes WSOD in that case. Instead, we can
Expand Down Expand Up @@ -1077,6 +1078,25 @@ async function preselectParentPage() {
}
}

function handleSiteEditorFeedbackPlugin( calypsoPort ) {
const PluginSidebar = editSitePackage?.PluginSidebar;
if ( PluginSidebar ) {
registerPlugin( 'a8c-fse-beta-feedback-plugin', {
render: () => (
<PluginSidebar
name="a8c-fse-beta-feedback-plugin"
title={ __( 'Site Editor Beta Feedback', 'full-site-editing' ) }
// eslint-disable-next-line wpcalypso/jsx-classname-namespace
className="a8c-site-editor-feedback-plugin"
icon={ comment }
>
<FeedbackForm calypsoPort={ calypsoPort } />
</PluginSidebar>
),
} );
}
}

function handleCheckoutModalOpened( calypsoPort, data ) {
const { port1, port2 } = new MessageChannel();

Expand Down Expand Up @@ -1244,6 +1264,8 @@ function initPort( message ) {
handleCheckoutModal( calypsoPort );

handleInlineHelpButton( calypsoPort );

handleSiteEditorFeedbackPlugin( calypsoPort );
}

window.removeEventListener( 'message', initPort, false );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,7 @@ body.is-fullscreen-mode {
.wpcom-block-editor__close-button {
height: $header-height !important;
}

.a8c-site-editor-feedback-plugin .components-panel__body {
border-bottom: none;
}
15 changes: 14 additions & 1 deletion client/gutenberg/editor/calypsoify-iframe.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { protectForm, ProtectedFormProps } from 'calypso/lib/protect-form';
import { addQueryArgs } from 'calypso/lib/route';
import wpcom from 'calypso/lib/wp';
import EditorDocumentHead from 'calypso/post-editor/editor-document-head';
import { getCurrentUserLocale } from 'calypso/state/current-user/selectors';
import { setEditorIframeLoaded, startEditingPost } from 'calypso/state/editor/actions';
import { getEditorPostId } from 'calypso/state/editor/selectors';
import { selectMediaItems } from 'calypso/state/media/actions';
Expand All @@ -48,11 +49,11 @@ import {
} from 'calypso/state/sites/selectors';
import { getSelectedSiteId, getSelectedSiteSlug } from 'calypso/state/ui/selectors';
import * as T from 'calypso/types';
import { sendSiteEditorBetaFeedback } from '../../lib/fse-beta/send-site-editor-beta-feedback';
import Iframe from './iframe';
import { getEnabledFilters, getDisabledDataSources, mediaCalypsoToGutenberg } from './media-utils';
import { Placeholder } from './placeholder';
import type { RequestCart } from '@automattic/shopping-cart';

import './style.scss';

/* eslint-disable @typescript-eslint/no-explicit-any */
Expand Down Expand Up @@ -122,6 +123,7 @@ enum EditorActions {
GetNavSidebarLabels = 'getNavSidebarLabels',
GetCalypsoUrlInfo = 'getCalypsoUrlInfo',
TrackPerformance = 'trackPerformance',
SendSiteEditorBetaFeedback = 'sendSiteEditorBetaFeedback',
}

type ComponentProps = Props &
Expand Down Expand Up @@ -490,6 +492,16 @@ class CalypsoifyIframe extends Component< ComponentProps, State > {
} );
}
}

if ( EditorActions.SendSiteEditorBetaFeedback === action ) {
sendSiteEditorBetaFeedback(
payload,
this.props.siteUrl,
this.props.currentUserLocale,
() => ports[ 0 ].postMessage( 'success' ),
() => ports[ 0 ].postMessage( 'error' )
);
}
};

handlePostStatusChange = ( status: string ) => {
Expand Down Expand Up @@ -881,6 +893,7 @@ const mapStateToProps = (
closeUrl,
closeLabel,
currentRoute,
currentUserLocale: getCurrentUserLocale( state ),
editedPostId: getEditorPostId( state ),
frameNonce: getSiteOption( state, siteId, 'frame_nonce' ) || '',
iframeUrl,
Expand Down
39 changes: 39 additions & 0 deletions client/lib/fse-beta/send-site-editor-beta-feedback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* External dependencies
*/
import config from '@automattic/calypso-config';
/**
* Internal dependencies
*/
import wpcom from 'calypso/lib/wp';

const noop = () => {};

/**
*
* @param {string} message The user's feedback message to send.
* @param {string} siteUrl The current site Url.
* @param {string} userLocale The user's locale.
* @param {Function} handleSuccess Function to handle response of post request.
* @param {Function} handleError Function to handle error in post request.
*/
export const sendSiteEditorBetaFeedback = (
message,
siteUrl,
userLocale,
handleSuccess = noop,
handleError = noop
) => {
const kayakoMessage = `Site: ${ siteUrl ? siteUrl : 'N/A' }\n\n${ message }`;

wpcom.req
.post( '/help/tickets/kayako/new', {
subject: '[Dotcom FSE Beta]',
message: kayakoMessage,
locale: userLocale,
client: config( 'client_slug' ),
is_chat_overflow: false,
} )
.then( handleSuccess )
.catch( handleError );
};

0 comments on commit 48fe031

Please sign in to comment.