Skip to content

Move task list logic outside of WpcomChecklist render tree - #28057

Merged
sgomes merged 6 commits into
masterfrom
update/refactor-wpcom-checklist
Nov 2, 2018
Merged

sgomes merged 6 commits into
masterfrom
update/refactor-wpcom-checklist

Conversation

@sgomes

@sgomes sgomes commented Oct 24, 2018

Copy link
Copy Markdown
Contributor

Changes proposed in this Pull Request

  • Normalize file extensions in WpcomChecklist and friends. Using .jsx for files including JSX and .js for files containing plain JS.
  • Replace unnecessary lodash method usage with native methods.
  • Move task list logic outside of WpcomChecklist render tree.

Each of these changes is presented as a separate commit, and it will likely be easier to look at each commit individually.

The last change is the main one in this PR, and is intended as an architectural revision that improves decoupling between rendering and logic.

Task list logic was stuck inside the WpcomChecklist render tree, forcing checklist instances to look at the list of their children to determine completion status. This also made it difficult to determine whether the checklist should be rendered without actually rendering it (which will be useful in optimization work; see #27926).

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.

Testing instructions

  • Use the various bits of checklist functionality (the /checklist page; the banner in /stats; the inline help integration)
  • All behaviour should remain the same, without any user-visible changes

@sgomes sgomes added Enhancement Changes to an existing feature — removing, adding, or changing parts of it [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. Checklist labels Oct 24, 2018
@sgomes
sgomes requested review from jsnajdr and sixhours October 24, 2018 17:05
@matticbot

Copy link
Copy Markdown
Contributor

@sixhours sixhours left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested the checklist notification in inline help! All is working as expected.

@jsnajdr jsnajdr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks and works very well 👍 Posted just two little nits.

( storedTask !== task.key || storedTask === null ) &&
! isFinished &&
! taskList.areAllTasksCompleted() &&
( storedTask !== taskList.getFirstIncompleteTask().id || storedTask === null ) &&

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we eliminate the check for areAllTasksCompleted()? That's equivalent to getFirstIncompleteTask() returning null, isn't it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is. It was more of a conceptual thing, but you're right, "no first incomplete task" is easy enough to grasp to not require another method.

} = this.props;

const canShowChecklist = this.canShow();
const taskList = new WpcomTaskList( taskStatuses, { designType } );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we create a new instance on every rerender. Doesn't it cause some unwanted rerenders?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're already inside the render method, so the component and its children are already going to render. That said, this could make a difference if one of the children is looking at its props to determine whether or not to re-render itself (e.g. by being a pure component, or by having a shouldComponentUpdate).

In order to cover those situations, I added memoization to this.

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 );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if there was a getCompletedPercentage method on the taskList? Or getCompletionStatus that would return an object that contains everything our UI wants to know:

{
  completed: 6,
  total: 10,
  percent: 60,
}

Just an idea on how to improve the object-oriented API of WpcomTaskList 🙂

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea! 😄

@sgomes
sgomes force-pushed the update/refactor-wpcom-checklist branch from 74a9606 to 0ffe350 Compare October 31, 2018 16:38
@sgomes

sgomes commented Oct 31, 2018

Copy link
Copy Markdown
Contributor Author

Thanks, @jsnajdr! All comments addressed.

@jsnajdr

jsnajdr commented Nov 1, 2018

Copy link
Copy Markdown
Member

Thanks for the updates @sgomes! There are conflicts coming from a new checklist task added in #27854. Don't forget to add the new task to the WpcomTaskList class, too 😉

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.
@sgomes
sgomes force-pushed the update/refactor-wpcom-checklist branch from 4b912ea to 11b1279 Compare November 2, 2018 12:13
@sgomes

sgomes commented Nov 2, 2018 •

Copy link
Copy Markdown
Contributor Author

Heh, can't leave changes open for more than a few days... I rebased on top of master, after the changes in #27854, making sure to include the new task. Waiting for tests to run and will merge afterwards.

@sgomes sgomes added [Status] Ready to Merge and removed [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. labels Nov 2, 2018
@sgomes
sgomes merged commit 4428400 into master Nov 2, 2018
@sgomes
sgomes deleted the update/refactor-wpcom-checklist branch November 2, 2018 12:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Checklist Enhancement Changes to an existing feature — removing, adding, or changing parts of it

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants