fix: template save API + contextual variables per event

1. API Route Fix (NotificationsController.php):
   - Changed PUT to POST for /templates/:eventId/:channelId
   - Frontend was using api.post() but backend only accepted PUT
   - Templates can now be saved

2. Contextual Variables (EventRegistry.php):
   - Added get_variables_for_event() method
   - Returns category-based variables (order, customer, product, etc.)
   - Merges event-specific variables from event definition
   - Sorted alphabetically for easy browsing

3. API Response (NotificationsController.php):
   - Template API now returns available_variables for the event
   - Frontend can show only relevant variables

4. Frontend (EditTemplate.tsx):
   - Removed hardcoded 50+ variable list
   - Now uses template.available_variables from API
   - Variables update based on selected event type
This commit is contained in:
Dwindi Ramadhana
2026-01-01 21:31:10 +07:00
parent b8f179a984
commit ccdd88a629
3 changed files with 265 additions and 162 deletions

View File

@@ -69,7 +69,7 @@ class NotificationsController {
],
]);
// GET/PUT /woonoow/v1/notifications/templates/:eventId/:channelId
// GET/POST /woonoow/v1/notifications/templates/:eventId/:channelId
register_rest_route($this->namespace, '/' . $this->rest_base . '/templates/(?P<eventId>[a-zA-Z0-9_-]+)/(?P<channelId>[a-zA-Z0-9_-]+)', [
[
'methods' => 'GET',
@@ -77,7 +77,7 @@ class NotificationsController {
'permission_callback' => [$this, 'check_permission'],
],
[
'methods' => 'PUT',
'methods' => 'POST',
'callback' => [$this, 'save_template'],
'permission_callback' => [$this, 'check_permission'],
],
@@ -486,6 +486,9 @@ class NotificationsController {
}
}
// Add available variables for this event (contextual)
$template['available_variables'] = EventRegistry::get_variables_for_event($event_id, $recipient_type);
return new WP_REST_Response($template, 200);
}