/
open-tab.ts
31 lines (29 loc) · 956 Bytes
/
open-tab.ts
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
import { dispatch, select } from '@wordpress/data';
import { CurrentView, storeName } from '../store';
type ValidTabs = 'automation-flow' | 'emails' | 'orders' | 'subscribers';
export function openTab(tab: ValidTabs, currentView?: CurrentView): void {
if (currentView) {
const section = select(storeName).getSection(tab);
const payload = {
...section,
customQuery: {
...section.customQuery,
filter: {
...currentView.filters,
},
},
currentView,
};
void dispatch(storeName).updateSection(payload);
}
const classMap: Record<ValidTabs, string> = {
'automation-flow': 'mailpoet-analytics-tab-flow',
emails: 'mailpoet-analytics-tab-emails',
orders: 'mailpoet-analytics-tab-orders',
subscribers: 'mailpoet-analytics-tab-subscribers',
};
const tabElement: HTMLButtonElement | null = document.querySelector(
`.${classMap[tab]}`,
);
tabElement?.click();
}