If anyone has the ability to generate/correct some javascript code to replace the notification badge number with the number of records in a table (with one or more conditions preferably). Ive spent a couple of hours with chat gpt to no avail.
If i´m not mistaken the code should call the records with a condition from the datatable using the ollowing API script:
var myHeaders = new Headers();
myHeaders.append(“X-Tadabase-App-id”, “{{appId}}”);
myHeaders.append(“X-Tadabase-App-Key”, “{{appKey}}”);
myHeaders.append(“X-Tadabase-App-Secret”, “{{appSecret}}”);
var requestOptions = {
method: 'GET',
headers: myHeaders,
redirect: 'follow'
};
fetch("https://api.tadabase.io/api/v1/data-tables/{{tableId}}/records?filters[items][0][field_id]=field_36&filters[items][0][operator]=is&filters[items][0][val]=Test 2", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
after this the javascript should count the number of records and replace the notification badge.
I guess in my use case I expect several notifications per minute at certain times of the month so if feasible the script would ideally run every set period of time (in my case every 5 mins).
The following is what i´ve got chatgpt to churn out upto now and it´s unclear after checking the console log even if the API is working:
// Ensure the DOM is loaded before running the script
document.addEventListener("DOMContentLoaded", function() {
// API credentials
var myHeaders = new Headers();
myHeaders.append("X-Tadabase-App-id", "XXXXXXXXX");
myHeaders.append("X-Tadabase-App-Key", "XXXXXXXXX");
myHeaders.append("X-Tadabase-App-Secret", "XXXXXXXXX");
var requestOptions = {
method: 'GET',
headers: myHeaders,
redirect: 'follow'
};
// Fetch the data from Tadabase API
fetch("https://api.tadabase.io/api/v1/data-tables/LX6QawrZk4/records?filters[items][0][field_id]=field_285&filters[items][0][operator]=is&filters[items][0][val]=En%20Proceso%20TPV%20Virtual", requestOptions)
.then(response => {
if (!response.ok) {
throw new Error("Network response was not ok");
}
return response.json(); // Convert response to JSON
})
.then(result => {
console.log(result); // Debug: Log the entire API result
// Check if the result has items and count them
if (result.items && result.items.length) {
let numberOfPayments = result.items.length; // Get the number of items (payments)
console.log('Number of payments:', numberOfPayments); // Debug: Log the number of payments
// Update the badge with the dynamic number of payments
let notificationBadge = document.querySelector('.tb-notification-badge');
if (notificationBadge) {
notificationBadge.textContent = numberOfPayments; // Update badge
} else {
console.log("Notification badge element not found."); // Log if the element is not found
}
} else {
console.log("No records found or result structure is unexpected.");
document.querySelector('.tb-notification-badge').textContent = "0"; // Default to 0 if no items are returned
}
})
.catch(error => console.log('Fetch error:', error)); // Log any fetch errors
});
Any help with this highly appreciated and I imagine other will benefit from such script