submissions.js
3.78 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import domReady from '@wordpress/dom-ready';
import {
unmountComponentAtNode,
createElement,
render,
} from '@wordpress/element';
import {
TriggerEmailActionComponent,
TriggerBulkExportComponent,
} from './components';
import {
globalParams,
triggerEmailActionsParams,
bulkExportParams,
} from './params';
domReady( function() {
const selectTopElement =
document.querySelector( '#bulk-action-selector-top' ) !== null
? document.querySelector( '#bulk-action-selector-top' )
: 0,
selectBottomElement =
document.querySelector( '#bulk-action-selector-bottom' ) !== null
? document.querySelector( '#bulk-action-selector-bottom' )
: 0;
//Check if a Form Bulk action selector exists
if ( selectTopElement.length > 0 && selectBottomElement.length > 0 ) {
//Re-Trigger Email Actions feature
openComponentOnSelection( selectTopElement, selectBottomElement );
interceptFormSubmit();
}
//Bulk Export Accross Forms feature
bulkExportAccrossForms();
} );
const generateKey = () => {
return `nf_${ new Date().getTime() }`;
}
//Open Component during Selection
const openComponentOnSelection = ( selectTopElement, selectBottomElement ) => {
//Add a change event listener on the selectors
[ selectTopElement, selectBottomElement ].forEach( ( el ) => {
const position = el.id.includes( 'top' )
? jQuery( '.tablenav.top' )
: jQuery( '.tablenav.bottom' );
el.addEventListener( 'change', ( event ) => {
if ( event.target.value === 'trigger-email-action' ) {
//Open Email Actions component
triggerEmailAction( position );
}
} );
} );
};
//Intercept Form Submission
const interceptFormSubmit = () => {
//Intercept submission of bulk actions
jQuery( '#posts-filter' ).on( 'submit', function( e ) {
//Check the Form submitter and value of the bulk select field
const doaction1 =
jQuery( '#bulk-action-selector-top' )[ 0 ].value ===
'trigger-email-action' &&
e.originalEvent.submitter.id === 'doaction';
const doaction2 =
jQuery( '#bulk-action-selector-bottom' )[ 0 ].value ===
'trigger-email-action' &&
e.originalEvent.submitter.id === 'doaction2';
if ( doaction1 || doaction2 ) {
//Stop PHP redirection process
e.preventDefault();
const position = doaction1
? jQuery( '.tablenav.top' )
: jQuery( '.tablenav.bottom' );
triggerEmailAction( position );
}
} );
};
//Component for trigger Email Action feature
const triggerEmailAction = ( position ) => {
const compDetect = document.getElementById( 'nf-trigger-emails-container' );
if ( compDetect !== null ) {
unmountComponentAtNode( compDetect );
}
//Set props
const params = {
globalParams,
triggerEmailActionsParams,
key: generateKey(),
fetchController: new AbortController()
};
//Create new element on the NF submissions page to trigger Email actions
const triggerEmailActionsContainer = document.createElement( 'div' );
triggerEmailActionsContainer.id = 'nf-trigger-emails-container';
position.after( triggerEmailActionsContainer );
//Render component in the new element created
const triggerEmailActionsElement = createElement(
TriggerEmailActionComponent,
params
);
render(
triggerEmailActionsElement,
document.getElementById( 'nf-trigger-emails-container' )
);
};
//Component for Bulk Export Accross Forms feature
const bulkExportAccrossForms = () => {
//Set props
const params = {
key: generateKey(),
globalParams,
bulkExportParams,
};
const pos = jQuery( '.tablenav.bottom .actions' );
const bulkExportContainer = document.createElement( 'div' );
bulkExportContainer.id = 'nf-bulk-export-container';
pos.after( bulkExportContainer );
//Render component in the new element created
const bulkExportElement = createElement(
TriggerBulkExportComponent,
params
);
render(
bulkExportElement,
document.getElementById( 'nf-bulk-export-container' )
);
};