From fb85e6b7d92f8289a10c985497c7c0ea3bba6104 Mon Sep 17 00:00:00 2001 From: Joshua T Flowers Date: Mon, 20 Sep 2021 12:55:33 -0400 Subject: [PATCH] Add hide task list endpoint and data actions (https://github.com/woocommerce/woocommerce-admin/pull/7578) * Add task list hide endpoint and data store actions * Fix requesting properties for hide task list request --- .../data/src/onboarding/action-types.js | 3 + .../packages/data/src/onboarding/actions.js | 38 ++++++++++ .../packages/data/src/onboarding/reducer.js | 47 ++++++++++++ .../src/API/OnboardingTasks.php | 74 +++++++++++++++++++ 4 files changed, 162 insertions(+) diff --git a/plugins/woocommerce-admin/packages/data/src/onboarding/action-types.js b/plugins/woocommerce-admin/packages/data/src/onboarding/action-types.js index 0b2c5e34ab9..3516348e1dd 100644 --- a/plugins/woocommerce-admin/packages/data/src/onboarding/action-types.js +++ b/plugins/woocommerce-admin/packages/data/src/onboarding/action-types.js @@ -21,6 +21,9 @@ const TYPES = { UNDO_SNOOZE_TASK_ERROR: 'UNDO_SNOOZE_TASK_ERROR', UNDO_SNOOZE_TASK_REQUEST: 'UNDO_SNOOZE_TASK_REQUEST', UNDO_SNOOZE_TASK_SUCCESS: 'UNDO_SNOOZE_TASK_SUCCESS', + HIDE_TASK_LIST_ERROR: 'HIDE_TASK_LIST_ERROR', + HIDE_TASK_LIST_REQUEST: 'HIDE_TASK_LIST_REQUEST', + HIDE_TASK_LIST_SUCCESS: 'HIDE_TASK_LIST_SUCCESS', }; export default TYPES; diff --git a/plugins/woocommerce-admin/packages/data/src/onboarding/actions.js b/plugins/woocommerce-admin/packages/data/src/onboarding/actions.js index b23bcd3ba51..2c284905df8 100644 --- a/plugins/woocommerce-admin/packages/data/src/onboarding/actions.js +++ b/plugins/woocommerce-admin/packages/data/src/onboarding/actions.js @@ -149,6 +149,28 @@ export function undoDismissTaskSuccess( task ) { }; } +export function hideTaskListError( taskListId, error ) { + return { + type: TYPES.HIDE_TASK_LIST_ERROR, + taskListId, + error, + }; +} + +export function hideTaskListRequest( taskListId ) { + return { + type: TYPES.HIDE_TASK_LIST_REQUEST, + taskListId, + }; +} + +export function hideTaskListSuccess( taskList ) { + return { + type: TYPES.HIDE_TASK_LIST_SUCCESS, + taskList, + }; +} + export function setTasksStatus( tasksStatus ) { return { type: TYPES.SET_TASKS_STATUS, @@ -258,3 +280,19 @@ export function* undoDismissTask( id ) { throw new Error(); } } + +export function* hideTaskList( id ) { + yield hideTaskListRequest( id ); + + try { + const taskList = yield apiFetch( { + path: `${ WC_ADMIN_NAMESPACE }/onboarding/tasks/${ id }/hide`, + method: 'POST', + } ); + + yield hideTaskListSuccess( taskList ); + } catch ( error ) { + yield hideTaskListError( id, error ); + throw new Error(); + } +} diff --git a/plugins/woocommerce-admin/packages/data/src/onboarding/reducer.js b/plugins/woocommerce-admin/packages/data/src/onboarding/reducer.js index e6d6f6e3eb3..b3e4d5bce5f 100644 --- a/plugins/woocommerce-admin/packages/data/src/onboarding/reducer.js +++ b/plugins/woocommerce-admin/packages/data/src/onboarding/reducer.js @@ -61,6 +61,8 @@ const onboarding = ( selector, task, taskId, + taskListId, + taskList, taskLists, tasksStatus, } @@ -262,6 +264,51 @@ const onboarding = ( }, taskLists: getUpdatedTaskLists( state.taskLists, task ), }; + case TYPES.HIDE_TASK_LIST_ERROR: + return { + ...state, + errors: { + ...state.errors, + hideTaskList: error, + }, + taskLists: state.taskLists.map( ( list ) => { + if ( taskListId === list.id ) { + return { + ...list, + isHidden: false, + }; + } + return list; + } ), + }; + case TYPES.HIDE_TASK_LIST_REQUEST: + return { + ...state, + requesting: { + ...state.requesting, + hideTaskList: true, + }, + taskLists: state.taskLists.map( ( list ) => { + if ( taskListId === list.id ) { + return { + ...list, + isHidden: true, + }; + } + return list; + } ), + }; + case TYPES.HIDE_TASK_LIST_SUCCESS: + return { + ...state, + requesting: { + ...state.requesting, + hideTaskList: false, + }, + taskLists: state.taskLists.map( ( list ) => { + return taskListId === list.id ? taskList : list; + } ), + }; default: return state; } diff --git a/plugins/woocommerce-admin/src/API/OnboardingTasks.php b/plugins/woocommerce-admin/src/API/OnboardingTasks.php index 75bb2d5bf22..804d3ca84c2 100644 --- a/plugins/woocommerce-admin/src/API/OnboardingTasks.php +++ b/plugins/woocommerce-admin/src/API/OnboardingTasks.php @@ -123,6 +123,19 @@ class OnboardingTasks extends \WC_REST_Data_Controller { ) ); + register_rest_route( + $this->namespace, + '/' . $this->rest_base . '/(?P[a-z0-9_\-]+)/hide', + array( + array( + 'methods' => \WP_REST_Server::EDITABLE, + 'callback' => array( $this, 'hide_task_list' ), + 'permission_callback' => array( $this, 'hide_task_list_permission_check' ), + ), + 'schema' => array( $this, 'get_public_item_schema' ), + ) + ); + register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P[a-z0-9_\-]+)/dismiss', @@ -245,6 +258,20 @@ class OnboardingTasks extends \WC_REST_Data_Controller { return true; } + /** + * Check if a given request has permission to hide task lists. + * + * @param WP_REST_Request $request Full details about the request. + * @return WP_Error|boolean + */ + public function hide_task_list_permission_check( $request ) { + if ( ! current_user_can( 'manage_woocommerce' ) ) { + return new \WP_Error( 'woocommerce_rest_cannot_update', __( 'Sorry, you are not allowed to hide task lists.', 'woocommerce-admin' ), array( 'status' => rest_authorization_required_code() ) ); + } + + return true; + } + /** * Check if a given request has access to manage woocommerce. * @@ -878,4 +905,51 @@ class OnboardingTasks extends \WC_REST_Data_Controller { return rest_ensure_response( OnboardingTasksFeature::get_task_by_id( $id ) ); } + + /** + * Hide a task list. + * + * @param WP_REST_Request $request Request data. + * + * @return WP_REST_Response|WP_Error + */ + public function hide_task_list( $request ) { + $id = $request->get_param( 'id' ); + $task_list = TaskLists::get_list( $id ); + + if ( ! $task_list ) { + return new \WP_Error( + 'woocommerce_tasks_invalid_task_list', + __( 'Sorry, that task list was not found', 'woocommerce-admin' ), + array( + 'status' => 404, + ) + ); + } + + $update = $task_list->hide(); + $json = $task_list->get_json(); + + if ( $update ) { + $completed_task_count = array_reduce( + $json['tasks'], + function( $total, $task ) { + return $task['isComplete'] ? $total + 1 : $total; + }, + 0 + ); + + wc_admin_record_tracks_event( + $id . '_completed', + array( + 'action' => 'remove_card', + 'completed_task_count' => $completed_task_count, + 'incomplete_task_count' => count( $json['tasks'] ) - $completed_task_count, + ) + ); + } + + return rest_ensure_response( $json ); + } + }