/
run-automation-once.tsx
47 lines (43 loc) · 1.3 KB
/
run-automation-once.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { ToggleControl } from '@wordpress/components';
import { dispatch, select, useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { storeName } from '../../../editor/store';
export function showRunOnlyOnce(): boolean {
const automation = select(storeName).getAutomationData();
const triggers = Object.values(automation.steps).filter(
(step) => step.type === 'trigger',
);
if (triggers.length === 0) {
return true;
}
const subscriberTriggers = triggers.filter((trigger) =>
select(storeName)
.getStepSubjectKeys(trigger.key)
.includes('mailpoet:subscriber'),
);
return subscriberTriggers.length > 0;
}
export function RunAutomationOnce(): JSX.Element {
const { automationData } = useSelect(
(s) => ({
automationData: s(storeName).getAutomationData(),
}),
[],
);
const checked =
(automationData.meta?.['mailpoet:run-once-per-subscriber'] as boolean) ||
false;
return (
<ToggleControl
className="mailpoet-automation-run-only-once"
label={__('Run this automation only once per subscriber.', 'mailpoet')}
checked={checked}
onChange={(value) => {
void dispatch(storeName).updateAutomationMeta(
'mailpoet:run-once-per-subscriber',
value,
);
}}
/>
);
}