wp-plugin-swiss-football-ma.../assets/admin.js
Reindl David (IT-PTR-CEN2-SL10) fbd595aa36 initial state
2026-03-18 19:58:24 +01:00

95 lines
3.8 KiB
JavaScript

// Swiss Football Admin JavaScript
jQuery(document).ready(function($) {
'use strict';
// Refresh teams functionality
$('#refresh-teams').on('click', function() {
var $button = $(this);
var $status = $('#refresh-status');
$button.prop('disabled', true).text('Refreshing...');
$status.text('').removeClass('success error');
fetch(swi_foot_ajax.rest_url.replace(/\/$/, '') + '/admin/refresh-teams', {
method: 'POST',
credentials: 'same-origin',
headers: { 'Content-Type': 'application/json', 'X-WP-Nonce': swi_foot_ajax.rest_nonce }
}).then(function(resp) {
return resp.json();
}).then(function(response) {
if (response && response.success) {
$status.text('Teams refreshed successfully!').addClass('success');
setTimeout(function() { location.reload(); }, 1500);
} else {
$status.text('Error: ' + (response.error || 'Unknown')).addClass('error');
}
}).catch(function() {
$status.text('Network error occurred.').addClass('error');
}).finally(function() {
$button.prop('disabled', false).text('Refresh Teams List');
});
});
// Clear cache functionality
$('#clear-cache').on('click', function() {
var $button = $(this);
var $status = $('#cache-status');
if (!confirm('Are you sure you want to clear the match data cache?')) {
return;
}
$button.prop('disabled', true);
$status.text('Clearing cache...').removeClass('success error');
fetch(swi_foot_ajax.rest_url.replace(/\/$/, '') + '/admin/clear-cache', {
method: 'POST',
credentials: 'same-origin',
headers: { 'Content-Type': 'application/json', 'X-WP-Nonce': swi_foot_ajax.rest_nonce }
}).then(function(resp) { return resp.json(); }).then(function(response) {
if (response && response.success) {
$status.text('Cache cleared successfully!').addClass('success');
setTimeout(function() { location.reload(); }, 1000);
} else {
$status.text('Error clearing cache.').addClass('error');
}
}).catch(function() {
$status.text('Error clearing cache.').addClass('error');
}).finally(function() {
$button.prop('disabled', false);
});
});
// Test API connection
$('#test-connection').on('click', function() {
var $button = $(this);
var $status = $('#connection-status');
$button.prop('disabled', true).text('Testing...');
$status.text('').removeClass('success error');
fetch(swi_foot_ajax.rest_url.replace(/\/$/, '') + '/admin/test-connection', {
method: 'POST',
credentials: 'same-origin',
headers: { 'Content-Type': 'application/json', 'X-WP-Nonce': swi_foot_ajax.rest_nonce }
}).then(function(resp) { return resp.json(); }).then(function(response) {
if (response && response.success) {
$status.text('Connection successful!').addClass('success');
} else {
$status.text('Connection failed: ' + (response.error || 'Unknown')).addClass('error');
}
}).catch(function() {
$status.text('Network error occurred.').addClass('error');
}).finally(function() {
$button.prop('disabled', false).text('Test API Connection');
});
});
// Auto-save settings notice
$('form').on('submit', function() {
$('<div class="notice notice-info is-dismissible"><p>Settings saved! The plugin will automatically refresh API tokens as needed.</p></div>')
.insertAfter('.wrap h1');
});
});