bluesky-Connector/assets/js/admin.js

146 lines
5.2 KiB
JavaScript
Raw Permalink Normal View History

2024-11-21 09:05:37 +00:00
jQuery(document).ready(function($) {
// Post status container element
const $statusContainer = $('.bluesky-post-status');
// Helper function to display status messages
function updateStatus(message, type = 'info') {
const $status = $(`<div class="notice notice-${type} is-dismissible"><p>${message}</p></div>`);
$statusContainer.prepend($status);
// Add dismiss button functionality
$status.find('.notice-dismiss').on('click', function() {
$status.fadeOut(300, function() { $(this).remove(); });
});
// Auto dismiss after 5 seconds for success messages
if (type === 'success') {
setTimeout(() => {
$status.fadeOut(300, function() { $(this).remove(); });
}, 5000);
}
}
// Handle immediate posting
$('.bluesky-share-post').on('click', function(e) {
e.preventDefault();
const $button = $(this);
const postId = $button.data('post-id');
const nonce = $button.data('nonce');
// Disable button and show loading state
$button.prop('disabled', true)
.addClass('updating-message')
.text(blueskyAdmin.strings.publishing);
$.ajax({
url: blueskyAdmin.ajaxUrl,
type: 'POST',
data: {
action: 'bluesky_post_now',
post_id: postId,
nonce: nonce
},
success: function(response) {
if (response.success) {
updateStatus(response.data.message, 'success');
location.reload();
} else {
updateStatus(response.data.message, 'error');
$button.prop('disabled', false)
.removeClass('updating-message')
.text(blueskyAdmin.strings.retry);
}
},
error: function(xhr, status, error) {
updateStatus(blueskyAdmin.strings.error, 'error');
$button.prop('disabled', false)
.removeClass('updating-message')
.text(blueskyAdmin.strings.retry);
}
});
});
// Handle retry posting
2024-11-21 09:05:37 +00:00
$('.bluesky-retry-post').on('click', function(e) {
e.preventDefault();
const $button = $(this);
const postId = $button.data('post-id');
const nonce = $button.data('nonce');
2024-11-21 09:05:37 +00:00
// Disable button and show loading state
$button.prop('disabled', true)
.addClass('updating-message')
.text(blueskyAdmin.strings.retrying);
2024-11-21 09:05:37 +00:00
$.ajax({
url: blueskyAdmin.ajaxUrl,
2024-11-21 09:05:37 +00:00
type: 'POST',
data: {
action: 'bluesky_post_now',
2024-11-21 09:05:37 +00:00
post_id: postId,
nonce: nonce
2024-11-21 09:05:37 +00:00
},
success: function(response) {
if (response.success) {
updateStatus(response.data.message, 'success');
2024-11-21 09:05:37 +00:00
location.reload();
} else {
updateStatus(response.data.message, 'error');
$button.prop('disabled', false)
.removeClass('updating-message')
.text(blueskyAdmin.strings.retry);
2024-11-21 09:05:37 +00:00
}
},
error: function(xhr, status, error) {
updateStatus(blueskyAdmin.strings.error, 'error');
$button.prop('disabled', false)
.removeClass('updating-message')
.text(blueskyAdmin.strings.retry);
2024-11-21 09:05:37 +00:00
}
});
});
// Handle reposting
$('.bluesky-repost').on('click', function(e) {
2024-11-21 09:05:37 +00:00
e.preventDefault();
const $button = $(this);
const postId = $button.data('post-id');
const nonce = $button.data('nonce');
if (!confirm(blueskyAdmin.strings.confirmRepost)) {
return;
}
2024-11-21 09:05:37 +00:00
$button.prop('disabled', true)
.addClass('updating-message')
.text(blueskyAdmin.strings.reposting);
2024-11-21 09:05:37 +00:00
$.ajax({
url: blueskyAdmin.ajaxUrl,
2024-11-21 09:05:37 +00:00
type: 'POST',
data: {
action: 'bluesky_post_now',
2024-11-21 09:05:37 +00:00
post_id: postId,
nonce: nonce,
repost: true
2024-11-21 09:05:37 +00:00
},
success: function(response) {
if (response.success) {
updateStatus(response.data.message, 'success');
2024-11-21 09:05:37 +00:00
location.reload();
} else {
updateStatus(response.data.message, 'error');
$button.prop('disabled', false)
.removeClass('updating-message')
.text(blueskyAdmin.strings.repost);
2024-11-21 09:05:37 +00:00
}
},
error: function(xhr, status, error) {
updateStatus(blueskyAdmin.strings.error, 'error');
$button.prop('disabled', false)
.removeClass('updating-message')
.text(blueskyAdmin.strings.repost);
2024-11-21 09:05:37 +00:00
}
});
});
});