From 2ba5c258d51e6afc7b1f2a1e8474ad7fb3a26d0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Gomes?= Date: Tue, 23 Oct 2018 17:37:53 +0100 Subject: [PATCH 1/6] Normalize file extensions in WpcomChecklist --- .../wpcom-checklist/checklist-banner/{index.js => index.jsx} | 0 .../wpcom-checklist/checklist-banner/{task.js => task.jsx} | 0 client/my-sites/checklist/wpcom-checklist/{index.js => index.jsx} | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename client/my-sites/checklist/wpcom-checklist/checklist-banner/{index.js => index.jsx} (100%) rename client/my-sites/checklist/wpcom-checklist/checklist-banner/{task.js => task.jsx} (100%) rename client/my-sites/checklist/wpcom-checklist/{index.js => index.jsx} (100%) diff --git a/client/my-sites/checklist/wpcom-checklist/checklist-banner/index.js b/client/my-sites/checklist/wpcom-checklist/checklist-banner/index.jsx similarity index 100% rename from client/my-sites/checklist/wpcom-checklist/checklist-banner/index.js rename to client/my-sites/checklist/wpcom-checklist/checklist-banner/index.jsx diff --git a/client/my-sites/checklist/wpcom-checklist/checklist-banner/task.js b/client/my-sites/checklist/wpcom-checklist/checklist-banner/task.jsx similarity index 100% rename from client/my-sites/checklist/wpcom-checklist/checklist-banner/task.js rename to client/my-sites/checklist/wpcom-checklist/checklist-banner/task.jsx diff --git a/client/my-sites/checklist/wpcom-checklist/index.js b/client/my-sites/checklist/wpcom-checklist/index.jsx similarity index 100% rename from client/my-sites/checklist/wpcom-checklist/index.js rename to client/my-sites/checklist/wpcom-checklist/index.jsx From f8052cbcabfbe62e58c9e0457951f2de5b7ffff7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Gomes?= Date: Tue, 23 Oct 2018 18:35:00 +0100 Subject: [PATCH 2/6] Replace unnecessary lodash method usage with native methods --- client/my-sites/checklist/main.jsx | 9 +++------ .../wpcom-checklist/checklist-banner/index.jsx | 10 +++------- .../wpcom-checklist/checklist-navigation/index.jsx | 7 +------ .../wpcom-checklist/checklist-notification/index.jsx | 9 ++------- 4 files changed, 9 insertions(+), 26 deletions(-) diff --git a/client/my-sites/checklist/main.jsx b/client/my-sites/checklist/main.jsx index 1d55438801aa..90d8d2d1f373 100644 --- a/client/my-sites/checklist/main.jsx +++ b/client/my-sites/checklist/main.jsx @@ -6,7 +6,6 @@ import page from 'page'; import React, { PureComponent } from 'react'; import { connect } from 'react-redux'; import { localize } from 'i18n-calypso'; -import { some } from 'lodash'; /** * Internal dependencies @@ -60,11 +59,9 @@ class ChecklistMain extends PureComponent { this.props.siteSlug && false === this.props.isAtomic && this.props.isJetpack && - some( [ - this.props.siteSlug !== prevProps.siteSlug, - this.props.isAtomic !== prevProps.isAtomic, - this.props.isJetpack !== prevProps.isJetpack, - ] ) + ( this.props.siteSlug !== prevProps.siteSlug || + this.props.isAtomic !== prevProps.isAtomic || + this.props.isJetpack !== prevProps.isJetpack ) ) { page.redirect( `/plans/my-plan/${ this.props.siteSlug }` ); } diff --git a/client/my-sites/checklist/wpcom-checklist/checklist-banner/index.jsx b/client/my-sites/checklist/wpcom-checklist/checklist-banner/index.jsx index c4f851df0f93..04cfc546caa4 100644 --- a/client/my-sites/checklist/wpcom-checklist/checklist-banner/index.jsx +++ b/client/my-sites/checklist/wpcom-checklist/checklist-banner/index.jsx @@ -7,7 +7,7 @@ import PropTypes from 'prop-types'; import React, { Children, Component } from 'react'; import store from 'store'; import { connect } from 'react-redux'; -import { find, get, reduce } from 'lodash'; +import { get } from 'lodash'; import { localize } from 'i18n-calypso'; /** @@ -73,11 +73,7 @@ export class ChecklistBanner extends Component { const childrenArray = Children.toArray( this.props.children ); const total = childrenArray.length; - const completeCount = reduce( - childrenArray, - ( sum, child ) => ( !! child.props.completed ? sum + 1 : sum ), - 0 - ); + const completeCount = childrenArray.filter( child => child.props.completed ).length; const isFinished = completeCount >= total; const completePercentage = Math.round( ! total ? 0 : ( completeCount / total ) * 100 ); @@ -137,7 +133,7 @@ export class ChecklistBanner extends Component { ) : ( - find( childrenArray, child => ! child.props.completed ) + childrenArray.find( child => ! child.props.completed ) ) } ); diff --git a/client/my-sites/checklist/wpcom-checklist/checklist-navigation/index.jsx b/client/my-sites/checklist/wpcom-checklist/checklist-navigation/index.jsx index 54aab34139ef..ca6ab0fddd73 100644 --- a/client/my-sites/checklist/wpcom-checklist/checklist-navigation/index.jsx +++ b/client/my-sites/checklist/wpcom-checklist/checklist-navigation/index.jsx @@ -5,7 +5,6 @@ import PropTypes from 'prop-types'; import React, { Children, Component } from 'react'; import { connect } from 'react-redux'; -import { find, reduce } from 'lodash'; import classNames from 'classnames'; import { localize } from 'i18n-calypso'; @@ -55,11 +54,7 @@ export class ChecklistNavigation extends Component { }; const childrenArray = Children.toArray( children ); const total = childrenArray.length; - const completeCount = reduce( - childrenArray, - ( sum, child ) => ( !! child.props.completed ? sum + 1 : sum ), - 0 - ); + const completeCount = childrenArray.filter( child => child.props.completed ).length; const isFinished = completeCount >= total; const checklistLink = '/checklist/' + siteSlug; diff --git a/client/my-sites/checklist/wpcom-checklist/checklist-notification/index.jsx b/client/my-sites/checklist/wpcom-checklist/checklist-notification/index.jsx index 81387adfacf9..236b21781b93 100644 --- a/client/my-sites/checklist/wpcom-checklist/checklist-notification/index.jsx +++ b/client/my-sites/checklist/wpcom-checklist/checklist-notification/index.jsx @@ -3,7 +3,6 @@ * External dependencies */ import { Children, Component } from 'react'; -import { find, reduce } from 'lodash'; export class ChecklistNotification extends Component { componentDidMount() { @@ -22,13 +21,9 @@ export class ChecklistNotification extends Component { // Therefore, we're accessing the child Tasks directly for accuracy const childrenArray = Children.toArray( children ); - const task = find( childrenArray, child => ! child.props.completed ); + const task = childrenArray.find( child => ! child.props.completed ); const total = childrenArray.length; - const completeCount = reduce( - childrenArray, - ( sum, child ) => ( !! child.props.completed ? sum + 1 : sum ), - 0 - ); + const completeCount = childrenArray.filter( child => child.props.completed ).length; const isFinished = completeCount >= total; this.props.setNotification( false ); From 0df110e0079b9760e046de1923ade86658d69f5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Gomes?= Date: Wed, 24 Oct 2018 17:08:28 +0100 Subject: [PATCH 3/6] Move task list logic outside of WpcomChecklist render tree. Task list logic was stuck inside the WpcomChecklist render tree, forcing checklist instances to look at the list of children to determine completion status. This also made it difficult to determine whether the checklist should be rendered without actually rendering it. This commit refactors the task list into a class that's instanced by WpcomChecklist and attached as a prop to the checklist component in its render tree. This instance serves as the single source of truth on the list of tasks, the completion status of each task, and the overall completion status for the task list. --- .../checklist-banner/index.jsx | 14 +- .../checklist-navigation/index.jsx | 21 +- .../checklist-notification/index.jsx | 23 +- .../checklist/wpcom-checklist/index.jsx | 483 +++++++++++------- .../wpcom-checklist/wpcom-task-list.js | 59 +++ 5 files changed, 367 insertions(+), 233 deletions(-) create mode 100644 client/my-sites/checklist/wpcom-checklist/wpcom-task-list.js diff --git a/client/my-sites/checklist/wpcom-checklist/checklist-banner/index.jsx b/client/my-sites/checklist/wpcom-checklist/checklist-banner/index.jsx index 04cfc546caa4..1bb1a97149d1 100644 --- a/client/my-sites/checklist/wpcom-checklist/checklist-banner/index.jsx +++ b/client/my-sites/checklist/wpcom-checklist/checklist-banner/index.jsx @@ -65,16 +65,18 @@ export class ChecklistBanner extends Component { } render() { - const { translate } = this.props; - if ( ! this.canShow() ) { return null; } + const { translate, taskList } = this.props; + const tasks = taskList.getAll(); + const childrenArray = Children.toArray( this.props.children ); - const total = childrenArray.length; - const completeCount = childrenArray.filter( child => child.props.completed ).length; - const isFinished = completeCount >= total; + const total = tasks.length; + const completeCount = tasks.filter( task => task.isCompleted ).length; + const isFinished = taskList.areAllTasksCompleted(); + const firstIncomplete = taskList.getFirstIncompleteTask(); const completePercentage = Math.round( ! total ? 0 : ( completeCount / total ) * 100 ); return ( @@ -133,7 +135,7 @@ export class ChecklistBanner extends Component { ) : ( - childrenArray.find( child => ! child.props.completed ) + childrenArray.find( child => child.props.id === firstIncomplete.id ) ) } ); diff --git a/client/my-sites/checklist/wpcom-checklist/checklist-navigation/index.jsx b/client/my-sites/checklist/wpcom-checklist/checklist-navigation/index.jsx index ca6ab0fddd73..c0d480e39a0a 100644 --- a/client/my-sites/checklist/wpcom-checklist/checklist-navigation/index.jsx +++ b/client/my-sites/checklist/wpcom-checklist/checklist-navigation/index.jsx @@ -3,7 +3,7 @@ * External dependencies */ import PropTypes from 'prop-types'; -import React, { Children, Component } from 'react'; +import React, { Component } from 'react'; import { connect } from 'react-redux'; import classNames from 'classnames'; import { localize } from 'i18n-calypso'; @@ -30,15 +30,10 @@ export class ChecklistNavigation extends Component { }; handleClick = () => { - // The redux state for getSiteChecklist() is injected from an API response - // so the selector's results and the task list will not always match - // Therefore, we're accessing the child Tasks directly for accuracy - - const childrenArray = Children.toArray( this.props.children ); - const task = find( childrenArray, child => ! child.props.completed ); + const task = this.props.taskList.getFirstIncompleteTask(); if ( task ) { - this.props.setStoredTask( task.key ); + this.props.setStoredTask( task.id ); } this.props.setNotification( false ); this.props.closePopover(); @@ -46,16 +41,16 @@ export class ChecklistNavigation extends Component { }; render() { - const { siteSlug, translate, showNotification, children, canShowChecklist } = this.props; + const { siteSlug, translate, showNotification, canShowChecklist, taskList } = this.props; + const tasks = taskList.getAll(); const buttonClasses = { 'has-notification': showNotification, 'checklist-navigation__count': true, }; - const childrenArray = Children.toArray( children ); - const total = childrenArray.length; - const completeCount = childrenArray.filter( child => child.props.completed ).length; - const isFinished = completeCount >= total; + const total = tasks.length; + const completeCount = tasks.filter( task => task.isCompleted ).length; + const isFinished = taskList.areAllTasksCompleted(); const checklistLink = '/checklist/' + siteSlug; if ( ! canShowChecklist || isFinished ) { diff --git a/client/my-sites/checklist/wpcom-checklist/checklist-notification/index.jsx b/client/my-sites/checklist/wpcom-checklist/checklist-notification/index.jsx index 236b21781b93..dc142b63f25c 100644 --- a/client/my-sites/checklist/wpcom-checklist/checklist-notification/index.jsx +++ b/client/my-sites/checklist/wpcom-checklist/checklist-notification/index.jsx @@ -2,7 +2,7 @@ /** * External dependencies */ -import { Children, Component } from 'react'; +import { Component } from 'react'; export class ChecklistNotification extends Component { componentDidMount() { @@ -14,27 +14,16 @@ export class ChecklistNotification extends Component { } shouldShowNotification = () => { - const { storedTask, canShowChecklist, children } = this.props; - - // The redux state for getSiteChecklist() is injected from an API response - // so the selector's results and the task list will not always match - // Therefore, we're accessing the child Tasks directly for accuracy - - const childrenArray = Children.toArray( children ); - const task = childrenArray.find( child => ! child.props.completed ); - const total = childrenArray.length; - const completeCount = childrenArray.filter( child => child.props.completed ).length; - const isFinished = completeCount >= total; - - this.props.setNotification( false ); + const { storedTask, canShowChecklist, taskList } = this.props; if ( - task && - ( storedTask !== task.key || storedTask === null ) && - ! isFinished && + ! taskList.areAllTasksCompleted() && + ( storedTask !== taskList.getFirstIncompleteTask().id || storedTask === null ) && canShowChecklist ) { this.props.setNotification( true ); + } else { + this.props.setNotification( false ); } }; diff --git a/client/my-sites/checklist/wpcom-checklist/index.jsx b/client/my-sites/checklist/wpcom-checklist/index.jsx index 9bed06daff46..2721697b2bfc 100644 --- a/client/my-sites/checklist/wpcom-checklist/index.jsx +++ b/client/my-sites/checklist/wpcom-checklist/index.jsx @@ -13,6 +13,7 @@ import { localize } from 'i18n-calypso'; /** * Internal dependencies */ +import WpcomTaskList from './wpcom-task-list'; import Checklist from 'components/checklist'; import ChecklistBanner from './checklist-banner'; import ChecklistBannerTask from './checklist-banner/task'; @@ -60,10 +61,6 @@ class WpcomChecklist extends PureComponent { error: null, }; - isComplete( taskId ) { - return get( this.props.taskStatuses, [ taskId, 'completed' ], false ); - } - canShow = () => { if ( ! this.props.isEligibleForDotcomChecklist || ! this.props.isSiteSection ) { return false; @@ -72,7 +69,7 @@ class WpcomChecklist extends PureComponent { return true; }; - handleTaskStart = ( { taskId, tourId, url } ) => () => { + handleTaskStart = ( { task, tourId, url } ) => () => { if ( ! tourId && ! url ) { return; } @@ -82,10 +79,10 @@ class WpcomChecklist extends PureComponent { this.props.recordTracksEvent( 'calypso_checklist_task_start', { checklist_name: 'new_blog', location, - step_name: taskId, + step_name: task.id, } ); - if ( tourId && ! this.isComplete( taskId ) && isDesktop() ) { + if ( tourId && ! task.isCompleted && isDesktop() ) { this.props.requestGuidedTour( tourId ); } @@ -103,9 +100,10 @@ class WpcomChecklist extends PureComponent { } }; - handleTaskStartThenDismiss = arg => () => { - this.handleTaskStart( arg )(); - this.handleTaskDismiss( arg.taskId )(); + handleTaskStartThenDismiss = args => () => { + const { task } = args; + this.handleTaskStart( args )(); + this.handleTaskDismiss( task.id )(); }; handleSendVerificationEmail = e => { @@ -149,10 +147,7 @@ class WpcomChecklist extends PureComponent { const { designType, siteId, - siteSlug, taskStatuses, - taskUrls, - translate, viewMode, updateCompletion, setNotification, @@ -163,7 +158,7 @@ class WpcomChecklist extends PureComponent { } = this.props; const canShowChecklist = this.canShow(); - const TaskComponent = 'banner' === viewMode ? ChecklistBannerTask : Task; + const taskList = new WpcomTaskList( taskStatuses, { designType } ); let ChecklistComponent = Checklist; @@ -192,194 +187,288 @@ class WpcomChecklist extends PureComponent { setNotification={ setNotification } setStoredTask={ setStoredTask } storedTask={ storedTask } + taskList={ taskList } > - , - changeButton: , - }, - } - ) } - duration={ translate( '%d minute', '%d minutes', { count: 1, args: [ 1 ] } ) } - onClick={ this.handleSendVerificationEmail } - siteSlug={ siteSlug } - title={ translate( 'Confirm your email address' ) } - buttonText={ this.verificationTaskButtonText() } - /> - - - - - - - { 'blog' === designType && ( - - ) } - - { 'blog' === designType && ( - - ) } - - + { taskList.getAll().map( task => this.renderTask( task ) ) } ); } + + renderTask( task ) { + const { siteSlug, viewMode } = this.props; + const TaskComponent = 'banner' === viewMode ? ChecklistBannerTask : Task; + + const taskFunctions = { + email_verified: this.renderEmailVerifiedTask, + site_created: this.renderSiteCreatedTask, + address_picked: this.renderAddressPickedTask, + blogname_set: this.renderBlogNameSetTask, + site_icon_set: this.renderSiteIconSetTask, + blogdescription_set: this.renderBlogDescriptionSetTask, + avatar_uploaded: this.renderAvatarUploadedTask, + contact_page_updated: this.renderContactPageUpdatedTask, + post_published: this.renderPostPublishedTask, + custom_domain_registered: this.renderCustomDomainRegisteredTask, + mobile_app_installed: this.renderMobileAppInstalledTask, + }; + + const baseProps = { + id: task.id, + key: task.id, + completed: task.isCompleted, + siteSlug, + }; + + if ( taskFunctions[ task.id ] ) { + return taskFunctions[ task.id ]( TaskComponent, baseProps, task ); + } + + return null; + } + + renderEmailVerifiedTask = ( TaskComponent, baseProps ) => { + const { translate } = this.props; + + return ( + , + changeButton: , + }, + } + ) } + duration={ translate( '%d minute', '%d minutes', { count: 1, args: [ 1 ] } ) } + onClick={ this.handleSendVerificationEmail } + title={ translate( 'Confirm your email address' ) } + /> + ); + }; + + renderSiteCreatedTask = ( TaskComponent, baseProps ) => { + const { translate } = this.props; + + return ( + + ); + }; + + renderAddressPickedTask = ( TaskComponent, baseProps ) => { + const { translate } = this.props; + + return ( + + ); + }; + + renderBlogNameSetTask = ( TaskComponent, baseProps, task ) => { + const { translate, siteSlug } = this.props; + + return ( + + ); + }; + + renderSiteIconSetTask = ( TaskComponent, baseProps, task ) => { + const { translate, siteSlug } = this.props; + + return ( + + ); + }; + + renderBlogDescriptionSetTask = ( TaskComponent, baseProps, task ) => { + const { translate, siteSlug } = this.props; + + return ( + + ); + }; + + renderAvatarUploadedTask = ( TaskComponent, baseProps, task ) => { + const { translate } = this.props; + + return ( + + ); + }; + + renderContactPageUpdatedTask = ( TaskComponent, baseProps, task ) => { + const { translate, taskUrls } = this.props; + + return ( + + ); + }; + + renderPostPublishedTask = ( TaskComponent, baseProps, task ) => { + const { translate, taskUrls } = this.props; + + return ( + + ); + }; + + renderCustomDomainRegisteredTask = ( TaskComponent, baseProps, task ) => { + const { translate, siteSlug } = this.props; + + return ( + + ); + }; + + renderMobileAppInstalledTask = ( TaskComponent, baseProps, task ) => { + const { translate } = this.props; + + return ( + + ); + }; } function getContactPage( posts ) { diff --git a/client/my-sites/checklist/wpcom-checklist/wpcom-task-list.js b/client/my-sites/checklist/wpcom-checklist/wpcom-task-list.js new file mode 100644 index 000000000000..6c3be2fe4406 --- /dev/null +++ b/client/my-sites/checklist/wpcom-checklist/wpcom-task-list.js @@ -0,0 +1,59 @@ +/** @format */ +/** + * External dependencies + */ +import { get } from 'lodash'; + +export default class WpcomTaskList { + constructor( taskStatuses, conditionalParams = {} ) { + this.tasks = []; + + const isCompleted = taskId => get( taskStatuses, [ taskId, 'completed' ], false ); + + const addTask = ( taskId, completedStatus = undefined ) => + this.tasks.push( { + id: taskId, + isCompleted: completedStatus !== undefined ? completedStatus : isCompleted( taskId ), + } ); + + addTask( 'email_verified' ); + addTask( 'site_created', true ); + addTask( 'address_picked', true ); + addTask( 'blogname_set' ); + addTask( 'site_icon_set' ); + addTask( 'blogdescription_set' ); + + if ( conditionalParams.designType === 'blog' ) { + addTask( 'avatar_uploaded' ); + } + + addTask( 'contact_page_updated' ); + + if ( conditionalParams.designType === 'blog' ) { + addTask( 'post_published' ); + } + + addTask( 'custom_domain_registered' ); + addTask( 'mobile_app_installed' ); + } + + getAll() { + return this.tasks; + } + + get( taskId ) { + return this.tasks.find( task => task.id === taskId ); + } + + has( taskId ) { + return !! this.get( taskId ); + } + + getFirstIncompleteTask() { + return this.tasks.find( task => ! task.isCompleted ); + } + + areAllTasksCompleted() { + return ! this.getFirstIncompleteTask(); + } +} From 331068b0ff6287b70409f1c3fcd498e569ec7bd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Gomes?= Date: Wed, 31 Oct 2018 16:43:53 +0000 Subject: [PATCH 4/6] Remove areAllTasksCompleted method from WPCOM task list API --- .../checklist/wpcom-checklist/checklist-banner/index.jsx | 2 +- .../checklist/wpcom-checklist/checklist-navigation/index.jsx | 2 +- .../wpcom-checklist/checklist-notification/index.jsx | 5 +++-- client/my-sites/checklist/wpcom-checklist/wpcom-task-list.js | 4 ---- 4 files changed, 5 insertions(+), 8 deletions(-) diff --git a/client/my-sites/checklist/wpcom-checklist/checklist-banner/index.jsx b/client/my-sites/checklist/wpcom-checklist/checklist-banner/index.jsx index 1bb1a97149d1..4d409d9d0846 100644 --- a/client/my-sites/checklist/wpcom-checklist/checklist-banner/index.jsx +++ b/client/my-sites/checklist/wpcom-checklist/checklist-banner/index.jsx @@ -75,8 +75,8 @@ export class ChecklistBanner extends Component { const childrenArray = Children.toArray( this.props.children ); const total = tasks.length; const completeCount = tasks.filter( task => task.isCompleted ).length; - const isFinished = taskList.areAllTasksCompleted(); const firstIncomplete = taskList.getFirstIncompleteTask(); + const isFinished = ! firstIncomplete; const completePercentage = Math.round( ! total ? 0 : ( completeCount / total ) * 100 ); return ( diff --git a/client/my-sites/checklist/wpcom-checklist/checklist-navigation/index.jsx b/client/my-sites/checklist/wpcom-checklist/checklist-navigation/index.jsx index c0d480e39a0a..56210d326111 100644 --- a/client/my-sites/checklist/wpcom-checklist/checklist-navigation/index.jsx +++ b/client/my-sites/checklist/wpcom-checklist/checklist-navigation/index.jsx @@ -50,7 +50,7 @@ export class ChecklistNavigation extends Component { }; const total = tasks.length; const completeCount = tasks.filter( task => task.isCompleted ).length; - const isFinished = taskList.areAllTasksCompleted(); + const isFinished = ! taskList.getFirstIncompleteTask(); const checklistLink = '/checklist/' + siteSlug; if ( ! canShowChecklist || isFinished ) { diff --git a/client/my-sites/checklist/wpcom-checklist/checklist-notification/index.jsx b/client/my-sites/checklist/wpcom-checklist/checklist-notification/index.jsx index dc142b63f25c..2d1d620cc42d 100644 --- a/client/my-sites/checklist/wpcom-checklist/checklist-notification/index.jsx +++ b/client/my-sites/checklist/wpcom-checklist/checklist-notification/index.jsx @@ -15,10 +15,11 @@ export class ChecklistNotification extends Component { shouldShowNotification = () => { const { storedTask, canShowChecklist, taskList } = this.props; + const firstIncomplete = taskList.getFirstIncompleteTask(); if ( - ! taskList.areAllTasksCompleted() && - ( storedTask !== taskList.getFirstIncompleteTask().id || storedTask === null ) && + firstIncomplete && + ( storedTask !== firstIncomplete.id || storedTask === null ) && canShowChecklist ) { this.props.setNotification( true ); diff --git a/client/my-sites/checklist/wpcom-checklist/wpcom-task-list.js b/client/my-sites/checklist/wpcom-checklist/wpcom-task-list.js index 6c3be2fe4406..e0720f327a57 100644 --- a/client/my-sites/checklist/wpcom-checklist/wpcom-task-list.js +++ b/client/my-sites/checklist/wpcom-checklist/wpcom-task-list.js @@ -52,8 +52,4 @@ export default class WpcomTaskList { getFirstIncompleteTask() { return this.tasks.find( task => ! task.isCompleted ); } - - areAllTasksCompleted() { - return ! this.getFirstIncompleteTask(); - } } From e935e2f2401065f9a62d141f06d2d936d766ece4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Gomes?= Date: Wed, 31 Oct 2018 16:54:11 +0000 Subject: [PATCH 5/6] Add memoization to WpcomTaskList instantiation in WpcomChecklist --- client/my-sites/checklist/wpcom-checklist/index.jsx | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/client/my-sites/checklist/wpcom-checklist/index.jsx b/client/my-sites/checklist/wpcom-checklist/index.jsx index 2721697b2bfc..3fa25ccb0a17 100644 --- a/client/my-sites/checklist/wpcom-checklist/index.jsx +++ b/client/my-sites/checklist/wpcom-checklist/index.jsx @@ -6,7 +6,7 @@ import page from 'page'; import PropTypes from 'prop-types'; import React, { PureComponent } from 'react'; import { connect } from 'react-redux'; -import { compact, find, get, some } from 'lodash'; +import { compact, find, get, some, memoize } from 'lodash'; import { isDesktop } from 'lib/viewport'; import { localize } from 'i18n-calypso'; @@ -38,6 +38,10 @@ const userLib = userFactory(); const query = { type: 'any', number: 10, order_by: 'ID', order: 'ASC' }; +const getTaskList = memoize( + ( taskStatuses, designType ) => new WpcomTaskList( taskStatuses, { designType } ) +); + class WpcomChecklist extends PureComponent { static propTypes = { createNotice: PropTypes.func.isRequired, @@ -158,7 +162,7 @@ class WpcomChecklist extends PureComponent { } = this.props; const canShowChecklist = this.canShow(); - const taskList = new WpcomTaskList( taskStatuses, { designType } ); + const taskList = getTaskList( taskStatuses, designType ); let ChecklistComponent = Checklist; From 11b12795491a9bb6f5ea1dd7678426f17058027b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Gomes?= Date: Wed, 31 Oct 2018 17:04:01 +0000 Subject: [PATCH 6/6] Add getCompletionStatus method to WpcomTaskList --- .../wpcom-checklist/checklist-banner/index.jsx | 12 +++++------- .../wpcom-checklist/checklist-navigation/index.jsx | 8 +++----- .../checklist/wpcom-checklist/wpcom-task-list.js | 11 +++++++++++ 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/client/my-sites/checklist/wpcom-checklist/checklist-banner/index.jsx b/client/my-sites/checklist/wpcom-checklist/checklist-banner/index.jsx index 4d409d9d0846..5823dccf4f97 100644 --- a/client/my-sites/checklist/wpcom-checklist/checklist-banner/index.jsx +++ b/client/my-sites/checklist/wpcom-checklist/checklist-banner/index.jsx @@ -70,14 +70,12 @@ export class ChecklistBanner extends Component { } const { translate, taskList } = this.props; - const tasks = taskList.getAll(); const childrenArray = Children.toArray( this.props.children ); - const total = tasks.length; - const completeCount = tasks.filter( task => task.isCompleted ).length; + const { total, completed, percentage } = taskList.getCompletionStatus(); + const firstIncomplete = taskList.getFirstIncompleteTask(); const isFinished = ! firstIncomplete; - const completePercentage = Math.round( ! total ? 0 : ( completeCount / total ) * 100 ); return ( @@ -87,7 +85,7 @@ export class ChecklistBanner extends Component { width={ 152 } height={ 152 } lineWidth={ 18 } - percentage={ completePercentage } + percentage={ percentage } metric={ translate( 'completed' ) } colors={ [ '#ffffff', '#47b766' ] } /> @@ -96,7 +94,7 @@ export class ChecklistBanner extends Component { { translate( 'Site setup' ) } { translate( '%(percentage)s%% completed', { - args: { percentage: completePercentage }, + args: { percentage: percentage }, } ) } { isFinished && ( @@ -109,7 +107,7 @@ export class ChecklistBanner extends Component { ) } - + { isFinished ? ( <> diff --git a/client/my-sites/checklist/wpcom-checklist/checklist-navigation/index.jsx b/client/my-sites/checklist/wpcom-checklist/checklist-navigation/index.jsx index 56210d326111..bb7c8239548a 100644 --- a/client/my-sites/checklist/wpcom-checklist/checklist-navigation/index.jsx +++ b/client/my-sites/checklist/wpcom-checklist/checklist-navigation/index.jsx @@ -42,14 +42,12 @@ export class ChecklistNavigation extends Component { render() { const { siteSlug, translate, showNotification, canShowChecklist, taskList } = this.props; - const tasks = taskList.getAll(); const buttonClasses = { 'has-notification': showNotification, 'checklist-navigation__count': true, }; - const total = tasks.length; - const completeCount = tasks.filter( task => task.isCompleted ).length; + const { total, completed } = taskList.getCompletionStatus(); const isFinished = ! taskList.getFirstIncompleteTask(); const checklistLink = '/checklist/' + siteSlug; @@ -73,14 +71,14 @@ export class ChecklistNavigation extends Component { { translate( '%(complete)d/%(total)d', { comment: 'Numerical progress indicator, like 5/9', args: { - complete: completeCount, + complete: completed, total: total, }, } ) }
- +
diff --git a/client/my-sites/checklist/wpcom-checklist/wpcom-task-list.js b/client/my-sites/checklist/wpcom-checklist/wpcom-task-list.js index e0720f327a57..7896ca4a5f13 100644 --- a/client/my-sites/checklist/wpcom-checklist/wpcom-task-list.js +++ b/client/my-sites/checklist/wpcom-checklist/wpcom-task-list.js @@ -52,4 +52,15 @@ export default class WpcomTaskList { getFirstIncompleteTask() { return this.tasks.find( task => ! task.isCompleted ); } + + getCompletionStatus() { + const completed = this.tasks.filter( task => task.isCompleted ).length; + const total = this.tasks.length; + + return { + completed, + total, + percentage: Math.round( ! total ? 0 : ( completed / total ) * 100 ), + }; + } }