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
This commit is contained in:
parent
99b01d6dd2
commit
fb85e6b7d9
|
@ -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;
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -123,6 +123,19 @@ class OnboardingTasks extends \WC_REST_Data_Controller {
|
|||
)
|
||||
);
|
||||
|
||||
register_rest_route(
|
||||
$this->namespace,
|
||||
'/' . $this->rest_base . '/(?P<id>[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<id>[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 );
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue