-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathadmin.js
More file actions
204 lines (173 loc) · 7.03 KB
/
Copy pathadmin.js
File metadata and controls
204 lines (173 loc) · 7.03 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
* Admin JavaScript for WPGraphQL Webhooks
*/
(function ($) {
'use strict';
$( document ).ready(
function () {
// Handle adding new header fields
$( '#add-header' ).on(
'click',
function () {
var headerRow = $( wpGraphQLWebhooks.headerTemplate || wpGraphQLWebhooks.headerRowTemplate );
$( '#webhook-headers-container' ).append( headerRow );
}
);
// Handle removing header fields
$( document ).on(
'click',
'.remove-header',
function () {
var headerRows = $( '.webhook-header-row' );
// Keep at least one header row
if (headerRows.length > 1) {
$( this ).closest( '.webhook-header-row' ).remove();
} else {
// Clear the values instead of removing the last row
$( this ).closest( '.webhook-header-row' ).find( 'input' ).val( '' );
}
}
);
// Confirm webhook deletion
$( '.delete-webhook' ).on(
'click',
function (e) {
if ( ! confirm( wpGraphQLWebhooks.confirmDelete )) {
e.preventDefault();
}
}
);
// Handle test webhook clicks using event delegation
$( document ).on(
'click',
'.test-webhook',
function (e) {
e.preventDefault();
var $link = $( this );
var webhookId = $link.data( 'webhook-id' );
var originalText = $link.text();
// Prevent multiple clicks
if ($link.hasClass( 'testing' )) {
return false;
}
// Generate unique ID for this test result
var resultId = 'webhook-test-result-' + webhookId + '-' + Date.now();
// Update UI to show testing
$link.text( 'Testing...' ).addClass( 'testing' );
// Send test request
$.ajax({
url: wpGraphQLWebhooks.ajaxUrl,
type: 'POST',
data: {
action: 'test_webhook',
webhook_id: webhookId,
nonce: wpGraphQLWebhooks.nonce
},
success: function(response) {
if (response.success) {
$link.text( originalText ).removeClass( 'testing' ).addClass( 'success' );
if (response.data) {
var $row = $link.closest( 'tr' );
var colspan = $row.find( 'td, th' ).length;
// Build detailed result HTML
var resultHtml = '<div class="webhook-test-details">';
resultHtml += '<button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button>';
resultHtml += '<p><strong>' + response.data.message + '</strong></p>';
// Response details
if (response.data.response_code) {
var statusClass = response.data.success ? 'success' : 'error';
resultHtml += '<p>Response Status: <span class="status-' + statusClass + '">' + response.data.response_code + '</span></p>';
}
// Show payload sent
if (response.data.test_payload) {
resultHtml += '<details>';
resultHtml += '<summary>Test Payload Sent</summary>';
resultHtml += '<pre class="webhook-test-payload">' + JSON.stringify(response.data.test_payload, null, 2) + '</pre>';
resultHtml += '</details>';
}
// Show response body if available
if (response.data.response_body) {
resultHtml += '<details>';
resultHtml += '<summary>Response Body</summary>';
resultHtml += '<pre class="webhook-response-body">' + response.data.response_body + '</pre>';
resultHtml += '</details>';
}
resultHtml += '</div>';
var noticeClass = response.data.success ? 'notice-success' : 'notice-warning';
var $resultRow = $( '<tr id="' + resultId + '" class="webhook-test-result"><td colspan="' + colspan + '"><div class="notice ' + noticeClass + ' is-dismissible inline">' + resultHtml + '</div></td></tr>' );
$row.after( $resultRow );
// Handle dismiss button
$( '#' + resultId + ' .notice-dismiss' ).on( 'click', function() {
$( '#' + resultId ).fadeOut(function() {
$( this ).remove();
});
});
}
} else {
$link.text( originalText ).removeClass( 'testing' ).addClass( 'error' );
var errorData = response.data || {};
var $row = $link.closest( 'tr' );
var colspan = $row.find( 'td, th' ).length;
// Build error HTML
var errorHtml = '<div class="webhook-test-details">';
errorHtml += '<button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button>';
errorHtml += '<p><strong>Test failed: ' + (errorData.message || 'Unknown error') + '</strong></p>';
if (errorData.error_code) {
errorHtml += '<p>Error Code: ' + errorData.error_code + '</p>';
}
if (errorData.error_data) {
errorHtml += '<details>';
errorHtml += '<summary>Error Details</summary>';
errorHtml += '<pre>' + JSON.stringify(errorData.error_data, null, 2) + '</pre>';
errorHtml += '</details>';
}
errorHtml += '</div>';
var $resultRow = $( '<tr id="' + resultId + '" class="webhook-test-result"><td colspan="' + colspan + '"><div class="notice notice-error is-dismissible inline">' + errorHtml + '</div></td></tr>' );
$row.after( $resultRow );
// Handle dismiss button
$( '#' + resultId + ' .notice-dismiss' ).on( 'click', function() {
$( '#' + resultId ).fadeOut(function() {
$( this ).remove();
});
});
}
},
error: function(xhr, status, error) {
$link.text( originalText ).removeClass( 'testing' ).addClass( 'error' );
var $row = $link.closest( 'tr' );
var colspan = $row.find( 'td, th' ).length;
var errorHtml = '<div class="webhook-test-details">';
errorHtml += '<button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button>';
errorHtml += '<p><strong>Test error: ' + error + '</strong></p>';
errorHtml += '</div>';
var $resultRow = $( '<tr id="' + resultId + '" class="webhook-test-result"><td colspan="' + colspan + '"><div class="notice notice-error is-dismissible inline">' + errorHtml + '</div></td></tr>' );
$row.after( $resultRow );
// Handle dismiss button
$( '#' + resultId + ' .notice-dismiss' ).on( 'click', function() {
$( '#' + resultId ).fadeOut(function() {
$( this ).remove();
});
});
}
});
}
);
// Handle bulk actions if using WP_List_Table
$('#doaction, #doaction2').on('click', function(e) {
var action = $(this).prev('select').val();
if (action === 'delete') {
var checked = $('input[name="webhook[]"]:checked');
if (checked.length === 0) {
alert('Please select at least one webhook to delete.');
e.preventDefault();
return false;
}
if (!confirm(wpGraphQLWebhooks.confirmDelete)) {
e.preventDefault();
return false;
}
}
});
}
);
})( jQuery );