Add automation registry context to editor state
[PREMIUM-194]
This commit is contained in:
@ -3,6 +3,7 @@ import { AutomationEditorWindow, State } from './types';
|
||||
declare let window: AutomationEditorWindow;
|
||||
|
||||
export const getInitialState = (): State => ({
|
||||
context: { ...window.mailpoet_automation_context },
|
||||
stepTypes: {},
|
||||
workflowData: { ...window.mailpoet_automation_workflow },
|
||||
workflowSaved: true,
|
||||
|
@ -2,7 +2,7 @@ import { createRegistrySelector } from '@wordpress/data';
|
||||
import { store as interfaceStore } from '@wordpress/interface';
|
||||
import { store as preferencesStore } from '@wordpress/preferences';
|
||||
import { storeName } from './constants';
|
||||
import { Feature, State, StepType } from './types';
|
||||
import { Context, Feature, State, StepType } from './types';
|
||||
import { Item } from '../components/inserter/item';
|
||||
import { Step, Workflow } from '../components/workflow/types';
|
||||
|
||||
@ -21,6 +21,17 @@ export function isInserterSidebarOpened(state: State): boolean {
|
||||
return state.inserterSidebar.isOpened;
|
||||
}
|
||||
|
||||
export function getContext(state: State): Context {
|
||||
return state.context;
|
||||
}
|
||||
|
||||
export function getContextStep(
|
||||
state: State,
|
||||
key: string,
|
||||
): Context['steps'][number] | undefined {
|
||||
return state.context.steps[key];
|
||||
}
|
||||
|
||||
export function getSteps(state: State): StepType[] {
|
||||
return Object.values(state.stepTypes);
|
||||
}
|
||||
|
@ -2,9 +2,24 @@ import { ComponentType } from 'react';
|
||||
import { Step, Workflow } from '../components/workflow/types';
|
||||
|
||||
export interface AutomationEditorWindow extends Window {
|
||||
mailpoet_automation_context: Context;
|
||||
mailpoet_automation_workflow: Workflow;
|
||||
}
|
||||
|
||||
export type Context = {
|
||||
steps: Record<
|
||||
string,
|
||||
{
|
||||
key: string;
|
||||
name: string;
|
||||
args_schema: {
|
||||
type: 'object';
|
||||
properties?: Record<string, { type: string; default?: unknown }>;
|
||||
};
|
||||
}
|
||||
>;
|
||||
};
|
||||
|
||||
export type StepGroup = 'actions' | 'logical' | 'triggers';
|
||||
|
||||
export type StepType = {
|
||||
@ -20,6 +35,7 @@ export type StepType = {
|
||||
};
|
||||
|
||||
export type State = {
|
||||
context: Context;
|
||||
stepTypes: Record<string, StepType>;
|
||||
workflowData: Workflow;
|
||||
workflowSaved: boolean;
|
||||
|
@ -6,6 +6,7 @@ use DateTimeImmutable;
|
||||
use MailPoet\AdminPages\PageRenderer;
|
||||
use MailPoet\Automation\Engine\Data\Step;
|
||||
use MailPoet\Automation\Engine\Data\Workflow;
|
||||
use MailPoet\Automation\Engine\Registry;
|
||||
use MailPoet\Automation\Engine\Storage\WorkflowStorage;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
use MailPoet\WP\Notice as WPNotice;
|
||||
@ -17,16 +18,21 @@ class AutomationEditor {
|
||||
/** @var PageRenderer */
|
||||
private $pageRenderer;
|
||||
|
||||
/** @var Registry */
|
||||
private $registry;
|
||||
|
||||
/** @var WPFunctions */
|
||||
private $wp;
|
||||
|
||||
public function __construct(
|
||||
WorkflowStorage $workflowStorage,
|
||||
PageRenderer $pageRenderer,
|
||||
Registry $registry,
|
||||
WPFunctions $wp
|
||||
) {
|
||||
$this->workflowStorage = $workflowStorage;
|
||||
$this->pageRenderer = $pageRenderer;
|
||||
$this->registry = $registry;
|
||||
$this->wp = $wp;
|
||||
}
|
||||
|
||||
@ -45,6 +51,7 @@ class AutomationEditor {
|
||||
}
|
||||
|
||||
$this->pageRenderer->displayPage('automation/editor.html', [
|
||||
'context' => $this->buildContext(),
|
||||
'workflow' => $this->buildWorkflow($workflow),
|
||||
'sub_menu' => 'mailpoet-automation',
|
||||
'api' => [
|
||||
@ -54,6 +61,18 @@ class AutomationEditor {
|
||||
]);
|
||||
}
|
||||
|
||||
private function buildContext(): array {
|
||||
$steps = [];
|
||||
foreach ($this->registry->getSteps() as $key => $step) {
|
||||
$steps[$key] = [
|
||||
'key' => $step->getKey(),
|
||||
'name' => $step->getName(),
|
||||
'args_schema' => $step->getArgsSchema()->toArray(),
|
||||
];
|
||||
}
|
||||
return ['steps' => $steps];
|
||||
}
|
||||
|
||||
private function buildWorkflow(Workflow $workflow): array {
|
||||
return [
|
||||
'id' => $workflow->getId(),
|
||||
|
@ -7,6 +7,7 @@
|
||||
<% block after_javascript %>
|
||||
<script type="text/javascript">
|
||||
var mailpoet_automation_api = <%= json_encode(api) %>;
|
||||
var mailpoet_automation_context = <%= json_encode(context) %>;
|
||||
var mailpoet_automation_workflow = <%= workflow ? json_encode(workflow) : 'undefined' %>;
|
||||
</script>
|
||||
<%= javascript('automation_editor.js')%>
|
||||
|
Reference in New Issue
Block a user