From b92fb6df5a8733af789df190815c92abaef54750 Mon Sep 17 00:00:00 2001 From: Boro Sitnikovski Date: Wed, 23 Oct 2024 19:35:38 +0200 Subject: [PATCH] Refactor NotificationSubscriptions to a functional component --- .../reader-subscriptions/index.jsx | 457 +++++++++--------- 1 file changed, 223 insertions(+), 234 deletions(-) diff --git a/client/me/notification-settings/reader-subscriptions/index.jsx b/client/me/notification-settings/reader-subscriptions/index.jsx index 37e055d0f65f4..77b5d1d4eafa1 100644 --- a/client/me/notification-settings/reader-subscriptions/index.jsx +++ b/client/me/notification-settings/reader-subscriptions/index.jsx @@ -1,8 +1,8 @@ import { Card, FormLabel } from '@automattic/components'; import i18n, { localize } from 'i18n-calypso'; import { flowRight as compose } from 'lodash'; -import { Component } from 'react'; -import { connect } from 'react-redux'; +import { useCallback } from 'react'; +import { useSelector, useDispatch } from 'react-redux'; import QueryReaderTeams from 'calypso/components/data/query-reader-teams'; import FormButton from 'calypso/components/forms/form-button'; import FormCheckbox from 'calypso/components/forms/form-checkbox'; @@ -25,268 +25,257 @@ import { recordGoogleEvent } from 'calypso/state/analytics/actions'; import { getReaderTeams } from 'calypso/state/teams/selectors'; import SubscriptionManagementBackButton from '../subscription-management-back-button'; -class NotificationSubscriptions extends Component { - handleClickEvent( action ) { - return () => this.props.recordGoogleEvent( 'Me', 'Clicked on ' + action ); - } +const NotificationSubscriptions = ( props ) => { + const dispatch = useDispatch(); + const teams = useSelector( getReaderTeams ); + const isAutomattician = isAutomatticTeamMember( teams ); + const { translate } = props; - handleFocusEvent( action ) { - return () => this.props.recordGoogleEvent( 'Me', 'Focused on ' + action ); - } + const handleClickEvent = useCallback( + ( action ) => { + return () => dispatch( recordGoogleEvent( 'Me', 'Clicked on ' + action ) ); + }, + [ dispatch ] + ); - handleCheckboxEvent( action ) { - return ( event ) => { - const eventAction = 'Clicked ' + action + ' checkbox'; - const optionValue = event.target.checked ? 1 : 0; + const handleFocusEvent = useCallback( + ( action ) => { + return () => dispatch( recordGoogleEvent( 'Me', 'Focused on ' + action ) ); + }, + [ dispatch ] + ); - this.props.recordGoogleEvent( 'Me', eventAction, 'checked', optionValue ); - }; - } + const handleCheckboxEvent = useCallback( + ( action ) => { + return ( event ) => { + const eventAction = 'Clicked ' + action + ' checkbox'; + const optionValue = event.target.checked ? 1 : 0; - getDeliveryHourLabel( hour ) { - return this.props.translate( '%(fromHour)s - %(toHour)s', { - context: 'Hour range between which subscriptions are delivered', - args: { - fromHour: this.props.moment().hour( hour ).minute( 0 ).format( 'LT' ), - toHour: this.props - .moment() - .hour( hour + 2 ) - .minute( 0 ) - .format( 'LT' ), - }, - } ); - } + dispatch( recordGoogleEvent( 'Me', eventAction, 'checked', optionValue ) ); + }; + }, + [ dispatch ] + ); - render() { - const { locale, teams } = this.props; - const isAutomattician = isAutomatticTeamMember( teams ); + const getDeliveryHourLabel = useCallback( + ( hour ) => { + return translate( '%(fromHour)s - %(toHour)s', { + context: 'Hour range between which subscriptions are delivered', + args: { + fromHour: props.moment().hour( hour ).minute( 0 ).format( 'LT' ), + toHour: props + .moment() + .hour( hour + 2 ) + .minute( 0 ) + .format( 'LT' ), + }, + } ); + }, + [ props ] + ); - return ( -
- + const { locale } = props; - - + return ( +
+ - + + - + - + - -
- - { this.props.translate( 'Subscriptions delivery' ) } - -

- { this.props.translate( - '{{readerLink}}Use the Reader{{/readerLink}} to adjust delivery settings for your existing subscriptions.', - { - components: { - readerLink: ( - - ), - }, - } - ) } -

+ - - - { this.props.translate( 'Default email delivery' ) } - - - - - - - - + + + { translate( 'Subscriptions delivery' ) } +

+ { translate( + '{{readerLink}}Use the Reader{{/readerLink}} to adjust delivery settings for your existing subscriptions.', + { + components: { + readerLink: ( + + ), + }, + } + ) } +

- - { this.props.translate( 'Jabber subscription delivery' ) } - - - - { this.props.translate( 'Default delivery via Jabber instant message' ) } - - - + + + { translate( 'Default email delivery' ) } + + + + + + + + - - - { this.props.translate( 'Email delivery format' ) } - - - - - - + + { translate( 'Jabber subscription delivery' ) } + + + { translate( 'Default delivery via Jabber instant message' ) } + + - - - { this.props.translate( 'Email delivery window' ) } - - - - - - - - - - + + + { translate( 'Email delivery format' ) } + + + + + + - - - - - - - - - - - - - - + + + { translate( 'Email delivery window' ) } + + + + + + + + + + - - { this.props.translate( - 'When choosing daily or weekly email delivery, which time of day would you prefer?' - ) } - - + + { [ ...Array( 12 ).keys() ].map( ( i ) => ( + + ) ) } + + + { translate( + 'When choosing daily or weekly email delivery, which time of day would you prefer?' + ) } + + + + + + { locale === 'en' || i18n.hasTranslation( 'Pause emails' ) + ? translate( 'Pause emails' ) + : translate( 'Block emails' ) } + + + + + { locale === 'en' || + i18n.hasTranslation( + 'Pause all email updates from sites you’re following on WordPress.com' + ) + ? translate( + 'Pause all email updates from sites you’re following on WordPress.com' + ) + : translate( + 'Block all email updates from blogs you’re following on WordPress.com' + ) } + + + + + { isAutomattician && ( - - { locale === 'en' || i18n.hasTranslation( 'Pause emails' ) - ? this.props.translate( 'Pause emails' ) - : this.props.translate( 'Block emails' ) } - + Auto-follow P2 posts upon commenting (Automatticians only) - { locale === 'en' || - i18n.hasTranslation( - 'Pause all email updates from sites you’re following on WordPress.com' - ) - ? this.props.translate( - 'Pause all email updates from sites you’re following on WordPress.com' - ) - : this.props.translate( - 'Block all email updates from blogs you’re following on WordPress.com' - ) } + Automatically subscribe to notifications for a P2 post whenever you leave a + comment on it. + ) } - { isAutomattician && ( - - Auto-follow P2 posts upon commenting (Automatticians only) - - - - Automatically subscribe to notifications for a P2 post whenever you leave a - comment on it. - - - - ) } - - - { this.props.isUpdatingUserSettings - ? this.props.translate( 'Saving…' ) - : this.props.translate( 'Save notification settings' ) } - - -
-
- ); - } -} - -const mapStateToProps = ( state ) => ( { - teams: getReaderTeams( state ), -} ); - -const mapDispatchToProps = { - recordGoogleEvent, + + { props.isUpdatingUserSettings + ? translate( 'Saving…' ) + : translate( 'Save notification settings' ) } + + + +
+ ); }; export default compose( - connect( mapStateToProps, mapDispatchToProps ), localize, protectForm, withLocalizedMoment,