2020-10-30 06:52:52 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2021-07-14 20:38:57 +00:00
|
|
|
import { createElement, useState, useEffect } from '@wordpress/element';
|
2020-10-30 06:52:52 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2020-11-04 00:48:56 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { compose } from '@wordpress/compose';
|
|
|
|
import { withDispatch } from '@wordpress/data';
|
2020-10-30 06:52:52 +00:00
|
|
|
|
2020-11-25 02:34:54 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import CustomerFeedbackModal from './customer-feedback-modal';
|
|
|
|
|
2020-12-01 22:36:13 +00:00
|
|
|
const noop = () => {};
|
|
|
|
|
2020-10-30 06:52:52 +00:00
|
|
|
/**
|
|
|
|
* Use `CustomerEffortScore` to gather a customer effort score.
|
|
|
|
*
|
|
|
|
* NOTE: This should live in @woocommerce/customer-effort-score to allow
|
|
|
|
* reuse.
|
|
|
|
*
|
2022-03-18 11:45:14 +00:00
|
|
|
* @param {Object} props Component props.
|
2020-11-18 20:53:52 +00:00
|
|
|
* @param {Function} props.recordScoreCallback Function to call when the score should be recorded.
|
2022-03-18 11:45:14 +00:00
|
|
|
* @param {string} props.label The label displayed in the modal.
|
2020-11-18 20:53:52 +00:00
|
|
|
* @param {Function} props.createNotice Create a notice (snackbar).
|
|
|
|
* @param {Function} props.onNoticeShownCallback Function to call when the notice is shown.
|
|
|
|
* @param {Function} props.onNoticeDismissedCallback Function to call when the notice is dismissed.
|
|
|
|
* @param {Function} props.onModalShownCallback Function to call when the modal is shown.
|
2022-03-18 11:45:14 +00:00
|
|
|
* @param {Object} props.icon Icon (React component) to be shown on the notice.
|
2020-10-30 06:52:52 +00:00
|
|
|
*/
|
2021-01-28 03:35:38 +00:00
|
|
|
export function CustomerEffortScore( {
|
2020-11-18 20:53:52 +00:00
|
|
|
recordScoreCallback,
|
2020-10-30 06:52:52 +00:00
|
|
|
label,
|
2020-11-04 00:48:56 +00:00
|
|
|
createNotice,
|
2020-11-18 20:53:52 +00:00
|
|
|
onNoticeShownCallback = noop,
|
|
|
|
onNoticeDismissedCallback = noop,
|
|
|
|
onModalShownCallback = noop,
|
2020-11-13 00:53:28 +00:00
|
|
|
icon,
|
2020-10-30 06:52:52 +00:00
|
|
|
} ) {
|
2020-11-04 00:48:56 +00:00
|
|
|
const [ shouldCreateNotice, setShouldCreateNotice ] = useState( true );
|
|
|
|
const [ visible, setVisible ] = useState( false );
|
|
|
|
|
2020-12-01 22:36:13 +00:00
|
|
|
useEffect( () => {
|
|
|
|
if ( ! shouldCreateNotice ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-04 00:48:56 +00:00
|
|
|
createNotice( 'success', label, {
|
|
|
|
actions: [
|
|
|
|
{
|
2022-03-31 12:36:08 +00:00
|
|
|
label: __( 'Give feedback', 'woocommerce' ),
|
2020-11-04 00:48:56 +00:00
|
|
|
onClick: () => {
|
|
|
|
setVisible( true );
|
2020-11-18 20:53:52 +00:00
|
|
|
onModalShownCallback();
|
2020-11-04 00:48:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2020-11-13 00:53:28 +00:00
|
|
|
icon,
|
2020-11-18 02:15:42 +00:00
|
|
|
explicitDismiss: true,
|
2020-11-18 20:53:52 +00:00
|
|
|
onDismiss: onNoticeDismissedCallback,
|
2020-11-04 00:48:56 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
setShouldCreateNotice( false );
|
|
|
|
|
2020-11-18 20:53:52 +00:00
|
|
|
onNoticeShownCallback();
|
2020-12-01 22:36:13 +00:00
|
|
|
}, [ shouldCreateNotice ] );
|
2020-11-18 20:53:52 +00:00
|
|
|
|
2020-12-01 22:36:13 +00:00
|
|
|
if ( shouldCreateNotice ) {
|
2020-11-04 00:48:56 +00:00
|
|
|
return null;
|
|
|
|
}
|
2020-10-30 06:52:52 +00:00
|
|
|
|
|
|
|
if ( ! visible ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2020-11-25 02:34:54 +00:00
|
|
|
<CustomerFeedbackModal
|
|
|
|
label={ label }
|
|
|
|
recordScoreCallback={ recordScoreCallback }
|
|
|
|
/>
|
2020-10-30 06:52:52 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
CustomerEffortScore.propTypes = {
|
|
|
|
/**
|
2020-11-18 20:53:52 +00:00
|
|
|
* The function to call to record the score.
|
2020-10-30 06:52:52 +00:00
|
|
|
*/
|
2020-11-18 20:53:52 +00:00
|
|
|
recordScoreCallback: PropTypes.func.isRequired,
|
2020-10-30 06:52:52 +00:00
|
|
|
/**
|
|
|
|
* The label displayed in the modal.
|
|
|
|
*/
|
|
|
|
label: PropTypes.string.isRequired,
|
2020-11-04 00:48:56 +00:00
|
|
|
/**
|
|
|
|
* Create a notice (snackbar).
|
|
|
|
*/
|
2020-11-13 00:53:28 +00:00
|
|
|
createNotice: PropTypes.func.isRequired,
|
|
|
|
/**
|
2020-11-18 20:53:52 +00:00
|
|
|
* The function to call when the notice is shown.
|
|
|
|
*/
|
|
|
|
onNoticeShownCallback: PropTypes.func,
|
|
|
|
/**
|
|
|
|
* The function to call when the notice is dismissed.
|
|
|
|
*/
|
|
|
|
onNoticeDismissedCallback: PropTypes.func,
|
|
|
|
/**
|
|
|
|
* The function to call when the modal is shown.
|
2020-11-13 00:53:28 +00:00
|
|
|
*/
|
2020-11-18 20:53:52 +00:00
|
|
|
onModalShownCallback: PropTypes.func,
|
2020-11-13 00:53:28 +00:00
|
|
|
/**
|
|
|
|
* Icon (React component) to be displayed.
|
|
|
|
*/
|
|
|
|
icon: PropTypes.element,
|
2020-10-30 06:52:52 +00:00
|
|
|
};
|
|
|
|
|
2020-11-04 00:48:56 +00:00
|
|
|
export default compose(
|
|
|
|
withDispatch( ( dispatch ) => {
|
2020-11-09 07:17:08 +00:00
|
|
|
const { createNotice } = dispatch( 'core/notices2' );
|
2020-11-04 00:48:56 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
createNotice,
|
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( CustomerEffortScore );
|