Only return viewable tasks from tasks endpoint (https://github.com/woocommerce/woocommerce-admin/pull/7718)

This commit is contained in:
Joel Thiessen 2021-09-29 16:47:01 -07:00 committed by GitHub
parent 43f68534ef
commit 9c5c39732b
5 changed files with 25 additions and 46 deletions

View File

@ -52,13 +52,12 @@ export const TaskList: React.FC< TaskListProps > = ( {
const nowTimestamp = Date.now();
const visibleTasks = tasks.filter(
( task ) =>
task.canView &&
! task.isDismissed &&
( ! task.isSnoozed || task.snoozedUntil < nowTimestamp )
);
const incompleteTasks = tasks.filter(
( task ) => task.canView && ! task.isComplete && ! task.isDismissed
( task ) => ! task.isComplete && ! task.isDismissed
);
const [ expandedTask, setExpandedTask ] = useState(

View File

@ -760,6 +760,7 @@ class OnboardingTasks extends \WC_REST_Data_Controller {
},
$lists
);
return rest_ensure_response( array_values( apply_filters( 'woocommerce_admin_onboarding_tasks', $json ) ) );
}

View File

@ -56,7 +56,7 @@ class Task {
*
* @var bool
*/
protected $can_view = true;
public $can_view = true;
/**
* Time string.
@ -261,15 +261,6 @@ class Task {
return $update;
}
/**
* Bool for task visibility.
*
* @return bool
*/
public function is_visible() {
return $this->can_view && ! $this->is_snoozed() && ! $this->is_dismissed();
}
/**
* Get the task as JSON.
*
@ -284,7 +275,7 @@ class Task {
'actionLabel' => $this->action_label,
'actionUrl' => $this->action_url,
'isComplete' => $this->is_complete,
'isVisible' => $this->is_visible(),
'canView' => $this->can_view,
'time' => $this->time,
'isDismissed' => $this->is_dismissed(),
'isDismissable' => $this->is_dismissable,

View File

@ -112,6 +112,22 @@ class TaskList {
$this->tasks[] = new Task( $args );
}
/**
* Get only visible tasks in list.
*
* @return array
*/
public function get_viewable_tasks() {
return array_values(
array_filter(
$this->tasks,
function( $task ) {
return $task->can_view;
}
)
);
}
/**
* Get the list for use in JSON.
*
@ -127,8 +143,9 @@ class TaskList {
function( $task ) {
return $task->get_json();
},
$this->tasks
$this->get_viewable_tasks()
),
);
}

View File

@ -28,7 +28,7 @@ class WC_Tests_OnboardingTasks_Task extends WC_Unit_Test_Case {
)
);
$this->assertEquals( true, $task->is_visible() );
$this->assertEquals( true, $task->can_view );
}
/**
@ -42,7 +42,7 @@ class WC_Tests_OnboardingTasks_Task extends WC_Unit_Test_Case {
)
);
$this->assertEquals( false, $task->is_visible() );
$this->assertEquals( false, $task->can_view );
}
/**
@ -96,20 +96,6 @@ class WC_Tests_OnboardingTasks_Task extends WC_Unit_Test_Case {
$this->assertNotContains( $task->id, $dismissed );
}
/**
* Tests that a dismissed task is not visible.
*/
public function test_dismissed_visibility() {
$task = new Task(
array(
'id' => 'wc-unit-test-task',
'is_dismissable' => true,
)
);
$task->dismiss();
$this->assertEquals( false, $task->is_visible() );
}
/**
* Tests that a task can be snoozed.
@ -145,21 +131,6 @@ class WC_Tests_OnboardingTasks_Task extends WC_Unit_Test_Case {
$this->assertArrayNotHasKey( $task->id, $snoozed );
}
/**
* Tests that a task is not visible when snoozed.
*/
public function test_snoozed_visibility() {
$task = new Task(
array(
'id' => 'wc-unit-test-snoozeable-task',
'is_snoozeable' => true,
)
);
$task->snooze();
$this->assertEquals( false, $task->is_visible() );
}
/**
* Tests that a task's snooze time is automatically added.
*/
@ -233,7 +204,7 @@ class WC_Tests_OnboardingTasks_Task extends WC_Unit_Test_Case {
$this->assertArrayHasKey( 'actionLabel', $json );
$this->assertArrayHasKey( 'actionUrl', $json );
$this->assertArrayHasKey( 'isComplete', $json );
$this->assertArrayHasKey( 'isVisible', $json );
$this->assertArrayHasKey( 'canView', $json );
$this->assertArrayHasKey( 'time', $json );
$this->assertArrayHasKey( 'isDismissed', $json );
$this->assertArrayHasKey( 'isDismissable', $json );