From a914fbceacb7c7ce8424b2d71867dc8b6807a2e8 Mon Sep 17 00:00:00 2001 From: mirefly Date: Sat, 12 Jan 2019 16:34:47 -0700 Subject: [PATCH 1/3] Refactor ReportAbuseButton to use DismissibleTextForm --- src/amo/components/ReportAbuseButton/index.js | 76 +---- .../components/ReportAbuseButton/styles.scss | 29 -- src/core/reducers/abuse.js | 64 ---- .../amo/components/TestReportAbuseButton.js | 309 +++++------------- tests/unit/core/reducers/test_abuse.js | 32 -- 5 files changed, 90 insertions(+), 420 deletions(-) diff --git a/src/amo/components/ReportAbuseButton/index.js b/src/amo/components/ReportAbuseButton/index.js index 9844f0852b2..6b99ce84d52 100644 --- a/src/amo/components/ReportAbuseButton/index.js +++ b/src/amo/components/ReportAbuseButton/index.js @@ -3,7 +3,6 @@ import makeClassName from 'classnames'; import { oneLine } from 'common-tags'; import * as React from 'react'; import { connect } from 'react-redux'; -import Textarea from 'react-textarea-autosize'; import { compose } from 'redux'; import { withErrorHandler } from 'core/errorHandler'; @@ -11,14 +10,14 @@ import type { ErrorHandlerType } from 'core/errorHandler'; import translate from 'core/i18n/translate'; import log from 'core/logger'; import { - disableAbuseButtonUI, - enableAbuseButtonUI, hideAddonAbuseReportUI, sendAddonAbuseReport, showAddonAbuseReportUI, } from 'core/reducers/abuse'; -import { sanitizeHTML } from 'core/utils'; +import { normalizeFileNameId, sanitizeHTML } from 'core/utils'; import Button from 'ui/components/Button'; +import DismissibleTextForm from 'ui/components/DismissibleTextForm'; +import type { OnSubmitParams } from 'ui/components/DismissibleTextForm'; import type { AppState } from 'amo/store'; import type { AddonAbuseState } from 'core/reducers/abuse'; import type { DispatchFunc } from 'core/types/redux'; @@ -41,11 +40,7 @@ type InternalProps = {| |}; export class ReportAbuseButtonBase extends React.Component { - textarea: React.ElementRef; - - dismissReportUI = (event: SyntheticEvent) => { - event.preventDefault(); - + dismissReportUI = () => { const { addon, dispatch, loading } = this.props; if (loading) { @@ -58,12 +53,10 @@ export class ReportAbuseButtonBase extends React.Component { dispatch(hideAddonAbuseReportUI({ addon })); }; - sendReport = (event: SyntheticEvent) => { - event.preventDefault(); - + sendReport = ({ text }: OnSubmitParams) => { // The button isn't clickable if there is no content, but just in case: // we verify there's a message to send. - if (!this.textarea.value.length) { + if (!text.trim().length) { log.debug(oneLine`User managed to click submit button while textarea was empty. Ignoring this onClick/sendReport event.`); return; @@ -75,7 +68,7 @@ export class ReportAbuseButtonBase extends React.Component { sendAddonAbuseReport({ addonSlug: addon.slug, errorHandlerId: errorHandler.id, - message: this.textarea.value, + message: text, }), ); }; @@ -86,20 +79,6 @@ export class ReportAbuseButtonBase extends React.Component { const { addon, dispatch } = this.props; dispatch(showAddonAbuseReportUI({ addon })); - this.textarea.focus(); - }; - - textareaChange = () => { - const { abuseReport, addon, dispatch } = this.props; - - // Don't dispatch the UI update if the button is already visible. - // We also test for `value.trim()` so the user can't submit an - // empty report full of spaces. - if (this.textarea.value.trim().length && !abuseReport.buttonEnabled) { - dispatch(enableAbuseButtonUI({ addon })); - } else if (!this.textarea.value.trim().length) { - dispatch(disableAbuseButtonUI({ addon })); - } }; render() { @@ -133,8 +112,6 @@ export class ReportAbuseButtonBase extends React.Component { ); } - const sendButtonIsDisabled = loading || !abuseReport.buttonEnabled; - const prefaceText = i18n.sprintf( i18n.gettext( `If you think this add-on violates @@ -190,40 +167,19 @@ export class ReportAbuseButtonBase extends React.Component { {errorHandler.renderErrorIfPresent()} -