From 7aa1fa775d66fade1d1202f72e8ee4923f0ce1d2 Mon Sep 17 00:00:00 2001 From: Jan Jakes Date: Thu, 1 Sep 2022 11:12:11 +0200 Subject: [PATCH] Add automation registry context to editor state [PREMIUM-194] --- .../automation/editor/store/initial_state.ts | 1 + .../src/automation/editor/store/selectors.ts | 13 ++++++++++++- .../js/src/automation/editor/store/types.ts | 16 ++++++++++++++++ .../lib/AdminPages/Pages/AutomationEditor.php | 19 +++++++++++++++++++ mailpoet/views/automation/editor.html | 1 + 5 files changed, 49 insertions(+), 1 deletion(-) diff --git a/mailpoet/assets/js/src/automation/editor/store/initial_state.ts b/mailpoet/assets/js/src/automation/editor/store/initial_state.ts index 6e76790d4e..e320811740 100644 --- a/mailpoet/assets/js/src/automation/editor/store/initial_state.ts +++ b/mailpoet/assets/js/src/automation/editor/store/initial_state.ts @@ -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, diff --git a/mailpoet/assets/js/src/automation/editor/store/selectors.ts b/mailpoet/assets/js/src/automation/editor/store/selectors.ts index c6a7a190a0..cd97c9aa29 100644 --- a/mailpoet/assets/js/src/automation/editor/store/selectors.ts +++ b/mailpoet/assets/js/src/automation/editor/store/selectors.ts @@ -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); } diff --git a/mailpoet/assets/js/src/automation/editor/store/types.ts b/mailpoet/assets/js/src/automation/editor/store/types.ts index 4ddddb5d47..683a97e230 100644 --- a/mailpoet/assets/js/src/automation/editor/store/types.ts +++ b/mailpoet/assets/js/src/automation/editor/store/types.ts @@ -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; + }; + } + >; +}; + export type StepGroup = 'actions' | 'logical' | 'triggers'; export type StepType = { @@ -20,6 +35,7 @@ export type StepType = { }; export type State = { + context: Context; stepTypes: Record; workflowData: Workflow; workflowSaved: boolean; diff --git a/mailpoet/lib/AdminPages/Pages/AutomationEditor.php b/mailpoet/lib/AdminPages/Pages/AutomationEditor.php index f79f4c5a44..ce58a7b8ee 100644 --- a/mailpoet/lib/AdminPages/Pages/AutomationEditor.php +++ b/mailpoet/lib/AdminPages/Pages/AutomationEditor.php @@ -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(), diff --git a/mailpoet/views/automation/editor.html b/mailpoet/views/automation/editor.html index 0c79acf4c6..113671fa74 100644 --- a/mailpoet/views/automation/editor.html +++ b/mailpoet/views/automation/editor.html @@ -7,6 +7,7 @@ <% block after_javascript %> <%= javascript('automation_editor.js')%>