fix: add missing JavaScript for master trainers overview page

Resolves missing data on /master-trainer/trainers/ page by adding the JavaScript
file that handles AJAX loading of trainer statistics and table data.

 Changes:
- Created assets/js/hvac-master-trainers-overview.js
  - Handles AJAX calls to load trainer stats and filtered trainer lists
  - Implements filter change handlers for status, region, and search
  - Includes debounced search input for better UX
  - Initializes interactive table elements after load

- Updated includes/class-hvac-master-trainers-overview.php
  - Added enqueue_scripts() method to properly load the JS file
  - Registers wp_enqueue_scripts action hook
  - Detects master trainer pages via shortcode or template slug
  - Ensures jQuery dependency is met

The page was showing navigation and filters but no data because the JavaScript
to make AJAX calls to hvac_master_trainers_stats and hvac_master_trainers_filter
actions was completely missing.

Tested on staging and deployed to production.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
ben 2025-10-29 16:45:03 -03:00
parent 80f11e71dd
commit aebfb9adb8
2 changed files with 186 additions and 3 deletions

View file

@ -0,0 +1,156 @@
/**
* Master Trainers Overview JavaScript
* Handles AJAX loading of trainer data and filtering
*/
(function($) {
'use strict';
// Wait for DOM and dependencies
$(document).ready(function() {
console.log('[HVAC] Master Trainers Overview: Initializing...');
// Check if we're on the trainers page
if ($('#hvac-master-trainers-overview').length === 0) {
console.log('[HVAC] Master Trainers Overview: Not on trainers page, skipping initialization');
return;
}
console.log('[HVAC] Master Trainers Overview: Found trainers container, loading data...');
// Load initial stats
loadTrainerStats();
// Load initial trainers list
loadTrainersList();
// Handle filter changes
$('#hvac-trainers-filters').on('submit', function(e) {
e.preventDefault();
loadTrainersList();
});
// Handle filter changes on select/input
$('#filter-status, #filter-region, #filter-search').on('change keyup', debounce(function() {
loadTrainersList();
}, 500));
});
/**
* Load trainer statistics
*/
function loadTrainerStats() {
console.log('[HVAC] Loading trainer stats...');
$.ajax({
url: hvac_master_trainers_ajax.ajax_url,
type: 'POST',
data: {
action: 'hvac_master_trainers_stats',
nonce: hvac_master_trainers_ajax.nonce
},
success: function(response) {
console.log('[HVAC] Stats loaded successfully', response);
if (response.success && response.data.html) {
$('#hvac-stats-tiles').html(response.data.html).show();
$('#hvac-stats-loading').hide();
} else {
console.error('[HVAC] Stats load failed:', response);
$('#hvac-stats-loading').html('<p class="hvac-error">Failed to load statistics.</p>');
}
},
error: function(xhr, status, error) {
console.error('[HVAC] Stats AJAX error:', status, error);
$('#hvac-stats-loading').html('<p class="hvac-error">Error loading statistics.</p>');
}
});
}
/**
* Load trainers list with current filters
*/
function loadTrainersList() {
console.log('[HVAC] Loading trainers list...');
// Show loading state
$('#hvac-trainers-loading').show();
$('#hvac-trainers-table-container').hide();
// Get filter values
var filters = {
action: 'hvac_master_trainers_filter',
nonce: hvac_master_trainers_ajax.nonce,
status: $('#filter-status').val() || 'all',
region: $('#filter-region').val() || '',
search: $('#filter-search').val() || ''
};
console.log('[HVAC] Filter values:', filters);
$.ajax({
url: hvac_master_trainers_ajax.ajax_url,
type: 'POST',
data: filters,
success: function(response) {
console.log('[HVAC] Trainers loaded successfully', response);
if (response.success && response.data.html) {
$('#hvac-trainers-table-container').html(response.data.html).show();
$('#hvac-trainers-loading').hide();
// Initialize any interactive elements in the table
initializeTrainerActions();
} else {
console.error('[HVAC] Trainers load failed:', response);
$('#hvac-trainers-loading').html('<p class="hvac-error">Failed to load trainers.</p>');
}
},
error: function(xhr, status, error) {
console.error('[HVAC] Trainers AJAX error:', status, error);
$('#hvac-trainers-loading').html('<p class="hvac-error">Error loading trainers.</p>');
}
});
}
/**
* Initialize trainer action buttons
*/
function initializeTrainerActions() {
console.log('[HVAC] Initializing trainer actions...');
// Handle view profile buttons
$('.hvac-view-trainer').on('click', function(e) {
e.preventDefault();
var trainerId = $(this).data('trainer-id');
console.log('[HVAC] View trainer:', trainerId);
// Navigate to trainer profile
window.location.href = $(this).attr('href');
});
// Handle edit buttons
$('.hvac-edit-trainer').on('click', function(e) {
e.preventDefault();
var trainerId = $(this).data('trainer-id');
console.log('[HVAC] Edit trainer:', trainerId);
window.location.href = $(this).attr('href');
});
// Any other interactive elements...
}
/**
* Debounce function for search input
*/
function debounce(func, wait) {
var timeout;
return function() {
var context = this, args = arguments;
clearTimeout(timeout);
timeout = setTimeout(function() {
func.apply(context, args);
}, wait);
};
}
})(jQuery);

View file

@ -76,12 +76,39 @@ class HVAC_Master_Trainers_Overview {
// AJAX handlers for trainers overview
add_action( 'wp_ajax_hvac_master_trainers_filter', array( $this, 'ajax_filter_trainers' ) );
add_action( 'wp_ajax_hvac_master_trainers_stats', array( $this, 'ajax_get_stats' ) );
// Shortcode for embedding trainers overview
add_shortcode( 'hvac_master_trainers', array( $this, 'render_trainers_overview' ) );
// Add function for template integration
// Add function for template integration
add_action( 'init', array( $this, 'register_template_functions' ) );
// Enqueue scripts for master trainers overview
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
}
/**
* Enqueue scripts for master trainers overview
*/
public function enqueue_scripts() {
// Only enqueue on master trainers pages
if ( ! is_page() ) {
return;
}
global $post;
if ( ! $post || ( strpos( $post->post_content, '[hvac_master_trainers]' ) === false &&
get_page_template_slug( $post->ID ) !== 'page-master-trainers.php' ) ) {
return;
}
wp_enqueue_script(
'hvac-master-trainers-overview',
HVAC_PLUGIN_URL . 'assets/js/hvac-master-trainers-overview.js',
array( 'jquery' ),
HVAC_VERSION,
true
);
}
/**