woocommerce/packages/js/customer-effort-score/src/index.js

125 lines
3.1 KiB
JavaScript
Raw Normal View History

/**
* External dependencies
*/
Allow packages to be built in isolation. (https://github.com/woocommerce/woocommerce-admin/pull/7286) * Use yarn instead of npm. In prep for workspaces, since we're locked to npm < 7. See: https://github.com/woocommerce/woocommerce-admin/pull/7126#issue-661287749 * Initial workspace creation. * Add initial tsc build to @woocommerce/number. * Attempt to build experimental package. * Try currency package. * Define all packages as workspaces. * Use tsconfig common to packages. * Fix currency package build. * Build csv-export with tsc. * Try to build customer-effort-score with tsc. * Fix JSX pragma. * Build data package with tsc. * Build date package with tsc. * Build experimental package with tsc. * Try to build explat package with tsc. * Build navigation package with tsc. * Build notices package with tsc. * Build onboarding package with tsc. * Build components package with tsc. * Swap in package JS build into main script. * Fix experimental package build. * Try per-package css build with components. * Try to run components package tests in isolation. Broken on JSX in test files not being transformed. * Move @woocommerce/wc-admin-settings into a package. * Try to fix components package tests. Fails because we aren't setting up the jest/jest-dom globals. * Move JS test code to reusable (private) package. * Enable incremental TS builds. * Use workspaces to run JS tests. * Use new jest configs for update snapshot scripts. * Fix style builds. * Fix package version in components. * Fix client test debug and watch scripts. * Update yarn lock. * Update test-staged behavior. * Try to fix storybook. * Fix storybook. * Update more npm commands to yarn. * Add changelog. * Fix lint errors. * Update packages readme script references. * Clean up unused gitignore match. * Fix another npm command. * Fix JS builds on watch. * Fix start script. * Fix start scripts for packages. * Use tsc to build packages before tests * yarn -> npm. # Conflicts: # package-lock.json # package.json * Fix linter error. * Remove workspace definitions. * Fix missing Fragment import. * Fix package lock. * Fix missing reference. * Only build commonjs module for js-tests helper. * Remove errant dependency from components. * Remove noop scripts. * Fix package JS build before testing. * Revert noisy formatting changes. * Fix precommit and test scripts. * Fix minimum expected recommended extension count. Japan test case breaks this. * Revert babel config changes. * chore(release): publish - @woocommerce/components@7.2.0 - @woocommerce/csv-export@1.4.0 - @woocommerce/currency@3.2.0 - @woocommerce/customer-effort-score@1.1.0 - @woocommerce/data@1.4.0 - @woocommerce/date@3.1.0 - @woocommerce/dependency-extraction-webpack-plugin@1.7.0 - @woocommerce/eslint-plugin@1.3.0 - @woocommerce/experimental@1.5.0 - @woocommerce/explat@1.1.0 - @woocommerce/js-tests@1.1.0 - @woocommerce/navigation@6.1.0 - @woocommerce/notices@3.1.0 - @woocommerce/number@2.2.0 - @woocommerce/onboarding@1.1.0 - @woocommerce/tracks@1.1.0 - @woocommerce/wc-admin-settings@1.1.0 * Add script for running 'start' in a package. * Remove yarn from gitignore. * Update package changelogs, prep versions for release. * Try to fix E2E tests after main merge. * Some cleanup. * Add changelog. Co-authored-by: Paul Sealock <psealock@gmail.com>
2021-07-14 20:38:57 +00:00
import { createElement, useState, useEffect } from '@wordpress/element';
import PropTypes from 'prop-types';
import { __ } from '@wordpress/i18n';
import { compose } from '@wordpress/compose';
import { withDispatch } from '@wordpress/data';
Update Customer Effort Score Modal (https://github.com/woocommerce/woocommerce-admin/pull/5515) * Add Customer Effort Score modal * Add style for selected emoji * Update modal layout to match design * Update colors to use color studio variables * Refactor SCSS to reduce nesting * Refactor render logic to reduce nesting * Add translation to strings * Add keyboard support to emoji buttons * Fix button misalignment * Fix missing white space around button text * Refactor emoji button layout with flexbox Flexbox makes more sense to use than a grid since the buttons need only a single row or column layout. * Refactor merge conflict resolution Tidies up the diff by - Using naming convention of “visible” instead of “open”. - Restoring previous import order. * Add comments field * Update initial score value to undefined * Refactor SASS selectors * Update sendScore with comments data * Fix comments field to display on low scores only * Refactor by extracting modal to own component * Add input validation * Add tests * Fix variable name * Update Feedback component documentation * Fix uneven button height * Restore previous prop names This ensures naming consistency with the CustomerEffortScore component. * Update variable name for consistency * Don't close modal if click outside it * Rename Feedback component to CustomerFeedbackModal * Update error message text * Fix import name * Fix formatting * Update test to use mockRecordScoreCallback prop name * Convert score to int before recording * Clear error message when score has been selected * Add transitions when comments field is shown/hidden * Wrapped comments field UI text for localization * Increase contrast ratio for focused effort score labels Co-authored-by: Matt Sherman <matt@jam123.com> Co-authored-by: Rebecca Scott <me@becdetat.com>
2020-11-25 02:34:54 +00:00
/**
* Internal dependencies
*/
import CustomerFeedbackModal from './customer-feedback-modal';
const noop = () => {};
/**
* Use `CustomerEffortScore` to gather a customer effort score.
*
* NOTE: This should live in @woocommerce/customer-effort-score to allow
* reuse.
*
* @param {Object} props Component props.
* @param {Function} props.recordScoreCallback Function to call when the score should be recorded.
* @param {string} props.label The label displayed in the modal.
* @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.
* @param {Object} props.icon Icon (React component) to be shown on the notice.
*/
export function CustomerEffortScore( {
recordScoreCallback,
label,
createNotice,
onNoticeShownCallback = noop,
onNoticeDismissedCallback = noop,
onModalShownCallback = noop,
icon,
} ) {
const [ shouldCreateNotice, setShouldCreateNotice ] = useState( true );
const [ visible, setVisible ] = useState( false );
useEffect( () => {
if ( ! shouldCreateNotice ) {
return;
}
createNotice( 'success', label, {
actions: [
{
label: __( 'Give feedback', 'woocommerce-admin' ),
onClick: () => {
setVisible( true );
onModalShownCallback();
},
},
],
icon,
explicitDismiss: true,
onDismiss: onNoticeDismissedCallback,
} );
setShouldCreateNotice( false );
onNoticeShownCallback();
}, [ shouldCreateNotice ] );
if ( shouldCreateNotice ) {
return null;
}
if ( ! visible ) {
return null;
}
return (
Update Customer Effort Score Modal (https://github.com/woocommerce/woocommerce-admin/pull/5515) * Add Customer Effort Score modal * Add style for selected emoji * Update modal layout to match design * Update colors to use color studio variables * Refactor SCSS to reduce nesting * Refactor render logic to reduce nesting * Add translation to strings * Add keyboard support to emoji buttons * Fix button misalignment * Fix missing white space around button text * Refactor emoji button layout with flexbox Flexbox makes more sense to use than a grid since the buttons need only a single row or column layout. * Refactor merge conflict resolution Tidies up the diff by - Using naming convention of “visible” instead of “open”. - Restoring previous import order. * Add comments field * Update initial score value to undefined * Refactor SASS selectors * Update sendScore with comments data * Fix comments field to display on low scores only * Refactor by extracting modal to own component * Add input validation * Add tests * Fix variable name * Update Feedback component documentation * Fix uneven button height * Restore previous prop names This ensures naming consistency with the CustomerEffortScore component. * Update variable name for consistency * Don't close modal if click outside it * Rename Feedback component to CustomerFeedbackModal * Update error message text * Fix import name * Fix formatting * Update test to use mockRecordScoreCallback prop name * Convert score to int before recording * Clear error message when score has been selected * Add transitions when comments field is shown/hidden * Wrapped comments field UI text for localization * Increase contrast ratio for focused effort score labels Co-authored-by: Matt Sherman <matt@jam123.com> Co-authored-by: Rebecca Scott <me@becdetat.com>
2020-11-25 02:34:54 +00:00
<CustomerFeedbackModal
label={ label }
recordScoreCallback={ recordScoreCallback }
/>
);
}
CustomerEffortScore.propTypes = {
/**
* The function to call to record the score.
*/
recordScoreCallback: PropTypes.func.isRequired,
/**
* The label displayed in the modal.
*/
label: PropTypes.string.isRequired,
/**
* Create a notice (snackbar).
*/
createNotice: PropTypes.func.isRequired,
/**
* 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.
*/
onModalShownCallback: PropTypes.func,
/**
* Icon (React component) to be displayed.
*/
icon: PropTypes.element,
};
export default compose(
withDispatch( ( dispatch ) => {
const { createNotice } = dispatch( 'core/notices2' );
return {
createNotice,
};
} )
)( CustomerEffortScore );