@woocommerce/data
Version:
WooCommerce Admin data store and utilities
111 lines (110 loc) • 3.99 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DeprecatedTasks = void 0;
/**
* External dependencies
*/
const hooks_1 = require("@wordpress/hooks");
const qs_1 = require("qs");
const deprecated_1 = __importDefault(require("@wordpress/deprecated"));
function getQuery() {
const searchString = window.location && window.location.search;
if (!searchString) {
return {};
}
const search = searchString.substring(1);
return (0, qs_1.parse)(search);
}
/**
* A simple class to handle deprecated tasks using the woocommerce_admin_onboarding_task_list filter.
*/
class DeprecatedTasks {
constructor() {
/**
* **Deprecated** Filter Onboarding tasks.
*
* @filter woocommerce_admin_onboarding_task_list
* @deprecated
* @param {Array} tasks Array of tasks.
* @param {Array} query Url query parameters.
*/
this.filteredTasks = (0, hooks_1.applyFilters)('woocommerce_admin_onboarding_task_list', [], getQuery());
if (this.filteredTasks && this.filteredTasks.length > 0) {
(0, deprecated_1.default)('woocommerce_admin_onboarding_task_list', {
version: '2.10.0',
alternative: 'TaskLists::add_task()',
plugin: '@woocommerce/data',
});
}
this.tasks = this.filteredTasks.reduce((org, task) => {
return {
...org,
[task.key]: task,
};
}, {});
}
hasDeprecatedTasks() {
return this.filteredTasks.length > 0;
}
getPostData() {
return this.hasDeprecatedTasks()
? {
extended_tasks: this.filteredTasks.map((task) => ({
title: task.title,
content: task.content,
additional_info: task.additionalInfo,
time: task.time,
level: task.level ? parseInt(task.level, 10) : 3,
list_id: task.type || 'extended',
can_view: task.visible,
id: task.key,
is_snoozeable: task.allowRemindMeLater,
is_dismissable: task.isDismissable,
is_complete: task.completed,
})),
}
: null;
}
mergeDeprecatedCallbackFunctions(taskLists) {
if (this.filteredTasks.length > 0) {
for (const taskList of taskLists) {
// Merge any extended task list items, to keep the callback functions around.
taskList.tasks = taskList.tasks.map((task) => {
if (this.tasks && this.tasks[task.id]) {
return {
...this.tasks[task.id],
...task,
isDeprecated: true,
};
}
return task;
});
}
}
return taskLists;
}
/**
* Used to keep backwards compatibility with the extended task list filter on the client.
* This can be removed after version WC Admin 2.10 (see deprecated notice in resolvers.js).
*
* @param {Object} task the returned task object.
* @param {Array} keys to keep in the task object.
* @return {Object} task with the keys specified.
*/
static possiblyPruneTaskData(task, keys) {
if (!task.time && !task.title) {
// client side task
return keys.reduce((simplifiedTask, key) => {
return {
...simplifiedTask,
[key]: task[key],
};
}, { id: task.id });
}
return task;
}
}
exports.DeprecatedTasks = DeprecatedTasks;