Spaces:
Runtime error
Runtime error
Update templates/API_connectors.html
Browse files1234569 unmodified lines1617181920212223242526272821 unmodified lines50515250535455563 unmodified lines6061626360646566676869707117 unmodified lines899091929394953 unmodified lines99100101102961031041051061001071081091 unmodified line1111121131081141151161171181131141151161171191201211221191231241251261272 unmodified lines1301311321331341351361 unmodified line13813914014114214314414597 unmodified lines2432442452382462472482495 unmodified lines25525625725825926026126214 unmodified lines2772782792702712722732802812822832842852862872882892902912922932942952772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513522963543553563572972982993003013023033043593603053063073083093103111 unmodified line3133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453683463473483493503513523533543553563573583593603613623633643653663673683693703713723743753733743753773763773783794 unmodified lines3843853863883873883903913923933943953963973983893903913923933943953963973983994004004014024034044054064074084094104114124134144154164044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984994604614624634644654665015024674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085045055065075085095105105115125135135145155161 unmodified line5185195205215215225235246 unmodified lines53153253353453453553653711 unmodified lines54955055155255355455255355414 unmodified lines5695705715755725735745755765775785795805815825833 unmodified lines5875885895895905905915925935944 unmodified lines5996006016016026036046056066076086096106116126026146156166176186196206216226236036046056063 unmodified lines610611612633613614615616<!DOCTYPE html><html>
<head> <title>API Connectors</title> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>9 unmodified lines .form-group { margin-top: -10px; }
.card { padding: 0; margin-top: -15px; margin-left: -40px; }
.header-row { display: flex; justify-content: space-between;21 unmodified lines padding: 5px; }
th, th, td { white-space: nowrap; }3 unmodified lines /* Sno column */ width: 5%; }
div.dataTables_wrapper div.dataTables_length select{ div.dataTables_wrapper div.dataTables_length select { width: 60px; display: inline-block; }
th:nth-child(2), td:nth-child(2) { /* API Name column */17 unmodified lines /* View column */ width: 5%; }
th:nth-child(6), td:nth-child(6) { /* View column */3 unmodified lines .reduced-width { width: 25%; }
div.dataTables_wrapper div.dataTables_length select{ div.dataTables_wrapper div.dataTables_length select { width: 60px; display: inline-block; } </style></head>
1 unmodified line {% include 'sidepane.html' %}
<div class="wrapper"> <div class="main-header"style="border-bottom: none;"> <div class="main-header" style="border-bottom: none;"> <!-- Content Header (Page header) --> <div class="content-header"> <div class="container-fluid"> <div class="container mt-2"> <div class="form-group left-align"> <label for="company" class="mr-1" style="margin-left:-60px;margin-top: 5px;">Company Name <span class="text-danger">*</span></label> <select type="text" id="company" name="company" class="form-control"style="width: 20%;margin-left:-60px;" required> <option value="" selected>Select</option> </select> <div class="form-group left-align"id="company-select"> <h4 id="selectedCompany" style="margin-left: 300px;"></h4>
<div class="col-12 d-flex justify-content-end mb-3"> <button class="btn btn-primary" style="margin-top: -40px;margin-right: -25px;position: fixed;" id="add">Add</button> <button class="btn btn-primary" style="margin-top: -40px;margin-right: -25px;position: fixed;" id="add">Add</button> </div> </div> <div class="row">2 unmodified lines </div> </div> </div> <h3 id="company_name" style="margin-left: 400px;">{{company_name}}</h3> <section class="content" id="contentSection" style="display: none;"> <div class="container-fluid"> <div class="form-group">1 unmodified line <input type="hidden" id="company_id" name="company_id" class="form-control" required> </div> </div>
<!-- Main content --> <section class="content"> <div class="container-fluid">97 unmodified lines </div> <div class="modal-footer"> <!-- <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button> --> <button type="button" id="saveupdate" class="btn btn-primary" style="display: none;">Update</button> <button type="button" id="saveupdate" class="btn btn-primary" style="display: none;">Update</button> <button type="button" id="save" onclick="save_file()" class="btn btn-primary">Save</button> </div> </div>5 unmodified lines <!-- Include DataTables JS and your custom script --> <script src="https://cdn.datatables.net/1.10.25/js/jquery.dataTables.min.js"></script> <script src="https://cdn.datatables.net/1.10.25/js/dataTables.bootstrap4.min.js"></script>
<script> $(document).ready(function () { // Show modal function14 unmodified lines // });
}); document.addEventListener("DOMContentLoaded", function () {
// clearFormFields(); fetchCompanies(); var table = $('#apiTable').DataTable(); const role = sessionStorage.getItem('userRole'); console.log('Current role:', role); // Debug statement to check the role const company_name=sessionStorage.getItem('company_name'); console.log("company_name",company_name); // document.getElementById('company_name').textContent = company_name; const passedCompanyName = sessionStorage.getItem('company_name') document.getElementById('selectedCompany').innerText = `Company name: ${passedCompanyName}`; const company_id = sessionStorage.getItem('company_id'); document.getElementById("company_id").value = company_id;
data_get_from_db(company_id);
const companySelect = document.getElementById('company'); const documentForm = document.getElementById('documentForm'); const table = $('#apiTable').DataTable(); // Initialize DataTable at the start companySelect.addEventListener('change', async function () { $("#message-container").empty(); const selectedCompanyName = companySelect.options[companySelect.selectedIndex].text; console.log(`Selected Company Name: ${selectedCompanyName}`);
if (selectedCompanyName !== "Select") {
try { const response = await fetch(`/api/company_id?company_name=${encodeURIComponent(selectedCompanyName)}`); if (!response.ok) { contentSection.style.display = 'none'; ("Company id does not exist for this company name does not exist. Please fill in the details."); const messageContainer = document.getElementById('message-container'); if (messageContainer) { messageContainer.innerHTML = "<div class='alert alert-danger'>API details do not exist for this company. Please fill in the details.</div>"; } throw new Error('Network response was not ok'); } const data = await response.json(); console.log('data returned', data); console.log(`Company ID: ${data.company_id}`); const companyId = data.company_id; document.getElementById('company_id').value = companyId;
if (companyId) { try { const Response = await fetch(`/api/get_api_connectors?company_id=${companyId}&company_name=${selectedCompanyName}`); const connectorsResponse = await Response.json(); console.log("api connector data table after connecting to table", connectorsResponse); table.clear(); if (!Array.isArray(connectorsResponse)) { throw new TypeError('Expected an array of companies');
displayErrorMessage("Data do not exist for this company. Please fill in the details by clicking add button."); contentSection.style.display = 'none'; } else {
connectorsResponse.forEach((company, index) => { table.row.add([ index + 1, company.APIName, company.APIEndpoint, `<a href='#' class='btn btn-info btn-sm'data-kid-id='${company.row_id}' data-action="view" onclick='viewCompany(this)''><i class='fas fa-eye'></i></a>`, `<a href='#' class='btn btn-warning btn-sm'data-kid-id='${company.row_id}' data-action="edit" onclick='editCompany(this)'><i class='fas fa-edit'></i></a>`, `<a href='#' class='btn btn-danger btn-sm' data-kid-id='${company.row_id}' onclick='deleteCompany(this)'><i class='fas fa-trash'></i></a>`, // "<a href='#' class='btn btn-info btn-sm'><i class='fas fa-eye' fa-eye'></i></a>", // "<a href='#' class='btn btn-warning btn-sm'><i class='fas fa-edit'></i></a>", // "<a href='#' class='btn btn-danger btn-sm'><i class='fas fa-trash'></i></button>" ]).draw(false); }); contentSection.style.display = 'block'; }
} catch (error) { console.error('Error fetching company documents:', error); //displayEmptyTable();
displayErrorMessage("Data do not exist for this company. Please fill in the details by clicking add button."); contentSection.style.display = 'none'; } } else { // displayEmptyTable();
displayErrorMessage("Document details2 for knowledge do not exist for this company. Please fill in the details."); } } catch (error) { console.error('No details for this company ID or data documents:', error); // displayEmptyTable();
displayErrorMessage("Document details3 do not exist for this company. Please fill in the details."); contentSection.style.display = 'none'; } } else {
table.clear().draw(); document.getElementById('contentSection').style.display = 'none'; // Optionally, you might want to clear the form fields as well clearFormFields();
function moveToNextInput(inputs, currentIndex) { const nextInput = inputs[currentIndex + 1]; if (nextInput) { nextInput.focus(); } else { // Optionally, submit the form or trigger the save button document.getElementById("save").focus(); } }); } }
function displayErrorMessage(message) { const messageContainer = document.getElementById('message-container'); if (messageContainer) {1 unmodified line } }
async function data_get_from_db(companyId) { if (companyId) { try { console.log('comp_id',companyId) const Response = await fetch(`/api/get_api_connectors?company_id=${companyId}`); console.log("responce from api :==> ", Response) const connectorsResponse = await Response.json(); console.log("knowledge data table after connecting to table", connectorsResponse); console.log("api connector data table after connecting to table", connectorsResponse); const table = $('#apiTable').DataTable(); // Initialize DataTable at the start table.clear(); if (!Array.isArray(connectorsResponse)) { throw new TypeError('Expected an array of companies');
displayErrorMessage("Data do not exist for this company. Please fill in the details by clicking add button."); contentSection.style.display = 'none'; } else {
connectorsResponse.forEach((company, index) => { table.row.add([ index + 1, company.APIName, company.APIEndpoint, `<a href='#' class='btn btn-info btn-sm'data-kid-id='${company.row_id}' data-action="view" onclick='viewCompany(this)''><i class='fas fa-eye'></i></a>`, `<a href='#' class='btn btn-warning btn-sm'data-kid-id='${company.row_id}' data-action="edit" onclick='editCompany(this)'><i class='fas fa-edit'></i></a>`, `<a href='#' class='btn btn-danger btn-sm' data-kid-id='${company.row_id}' onclick='deleteCompany(this)'><i class='fas fa-trash'></i></a>`, ]).draw(false); }); }); contentSection.style.display = 'block'; }
} catch (error) { console.error('Error fetching company documents:', error); //displayEmptyTable();
displayErrorMessage("Data do not exist for this company. Please fill in the details by clicking add button."); contentSection.style.display = 'none'; } } else { // displayEmptyTable();
displayErrorMessage("Document details2 for knowledge do not exist for this company. Please fill in the details."); }
}
function viewCompany(button) { var companyId = $(button).data('kid-id'); const modalTitle = document.getElementById('addModalLabel'); modalTitle.textContent = 'View API Details'; if(companyId) { $.getJSON(`/api/viewapiconnectors/${companyId}`, function (company) { $('#APIName').val(company.api_name).attr('readonly','readonly');
$('#APIName').val(company.api_name).attr('readonly', 'readonly'); $('#APIEndpoint').val(company.api_endpoint).attr('readonly', 'readonly'); $('#Auth_Bearer') .val(company.auth_token).attr('readonly', 'readonly') ; $('#Auth_Bearer').val(company.auth_token).attr('readonly', 'readonly'); $('#Inputjson').val(company.input_param).attr('readonly', 'readonly'); $('#OutputJson').val(company.output_json).attr('readonly', 'readonly'); $('#Description').val(company.description).attr('readonly', 'readonly');4 unmodified lines alert("Error retrieving API details."); }); }
} // Edit knowledge base details function editCompany(button) { var companyId = $(button).data('kid-id'); const modalTitle = document.getElementById('addModalLabel'); modalTitle.textContent = 'Edit API Details'; // Fetch company details by ID $.getJSON(`/api/viewapiconnectors/${companyId}`, function (company) { $('#APIName').val(company.api_name); function editCompany(button) { var companyId = $(button).data('kid-id'); const modalTitle = document.getElementById('addModalLabel'); modalTitle.textContent = 'Edit API Details'; if(companyId) { // Fetch company details by ID $.getJSON(`/api/viewapiconnectors/${companyId}`, function (company) {
$('#APIName').val(company.api_name); $('#APIEndpoint').val(company.api_endpoint); $('#Auth_Bearer') .val(company.auth_token); $('#Auth_Bearer').val(company.auth_token); $('#Inputjson').val(company.input_param); $('#OutputJson').val(company.output_json); $('#Description').val(company.description); // Show/hide appropriate elements $('#uploadFile').show(); // Hide the file upload field $('#save').hide(); // Hide the default save button $('#saveupdate').show(); // Show the update button // Ensure fields are editable $('#APIName').removeAttr('readonly'); $('#APIEndpoint').removeAttr('readonly'); $('#Auth_Bearer').removeAttr('readonly'); $('#Inputjson').removeAttr('readonly'); $('#OutputJson').removeAttr('readonly'); $('#Description').removeAttr('readonly');
// Show/hide appropriate elements $('#uploadFile').show(); // Hide the file upload field $('#save').hide(); // Hide the default save button $('#saveupdate').show(); // Show the update button
// Ensure fields are editable $('#APIName').removeAttr('readonly'); $('#APIEndpoint').removeAttr('readonly'); $('#Auth_Bearer').removeAttr('readonly'); $('#Inputjson').removeAttr('readonly'); $('#OutputJson').removeAttr('readonly'); $('#Description').removeAttr('readonly');
// Show the modal $('#addModal').modal('show'); // Set up the click event for the update button $('#saveupdate').off('click').on('click', function () { saveupdate(companyId); // Pass the company ID for updating }); }).fail(function () { alert("Error in retrieving API details."); });}
function saveupdate(companyId, filename) { const formData = new FormData(); const company_id=document.getElementById("company_id").value; formData.append("company_id", $('#company_id').val()); formData.append("APIName", $('#APIName').val()); formData.append("APIEndpoint", $('#APIEndpoint').val()); formData.append("Auth_Bearer", $('#Auth_Bearer').val()); formData.append("Inputjson", $('#Inputjson').val()); formData.append("OutputJson", $('#OutputJson').val()); formData.append("Description", $('#Description').val());console.log("company_id to table refresh",company_id)const messageContainer = document.getElementById('message-container'); $.ajax({ url: `/api/editapiconnectors/${companyId}`, type: 'PUT', data: formData, processData: false, // Required for FormData contentType: false, // Required for FormData success: function (response) { // alert('Knowledgebase details updated successfully.'); if(messageContainer)
// Show the modal $('#addModal').modal('show');
// Set up the click event for the update button $('#saveupdate').off('click').on('click', function () { saveupdate(companyId); // Pass the company ID for updating }); }).fail(function () { alert("Error in retrieving API details."); }); } }
function saveupdate(companyId, filename) { const formData = new FormData();
const company_id = document.getElementById("company_id").value; formData.append("company_id", $('#company_id').val()); formData.append("APIName", $('#APIName').val()); formData.append("APIEndpoint", $('#APIEndpoint').val()); formData.append("Auth_Bearer", $('#Auth_Bearer').val()); formData.append("Inputjson", $('#Inputjson').val()); formData.append("OutputJson", $('#OutputJson').val()); formData.append("Description", $('#Description').val()); console.log("company_id to table refresh", company_id) const messageContainer = document.getElementById('message-container'); $.ajax({ url: `/api/editapiconnectors/${companyId}`, type: 'PUT', data: formData, processData: false, // Required for FormData contentType: false, // Required for FormData success: function (response) { // alert('Knowledgebase details updated successfully.');
if (messageContainer) messageContainer.innerHTML = ` <div class='alert alert-success'> API Data Updated successfully <button class='close' onclick='dismissMessage()'>OK</button> </div>`; $('#addModal').modal('hide'); // Close the modal after saving fetchUpdatedDocuments(company_id); }, error: function () { alert('Form has no changes to update knowledge base details.'); } });}function dismissMessage() { const messageContainer = document.getElementById('message-container'); if (messageContainer) { messageContainer.innerHTML = ''; }}async function fetchUpdatedDocuments(company_id) { console.log("company_id:", company_id);
var table = $('#apiTable').DataTable(); try { const response = await fetch(`/api/api_updatetable?company_id=${company_id}`); const connectorsResponse = await response.json();
console.log("Fetched connectorsResponse:", connectorsResponse);
table.clear(); // Clear existing data in the table
if (!Array.isArray(connectorsResponse) || connectorsResponse.length === 0) { displayErrorMessage("Knowledgebase details do not exist for this company. Please fill in the details."); contentSection.style.display = 'none'; } else { connectorsResponse.forEach((company, index) => { table.row.add([ index + 1, company.api_name, company.api_endpoint, `<a href='#' class='btn btn-info btn-sm' data-kid-id='${company.row_id}' data-action="view" onclick='viewCompany(this)'><i class='fas fa-eye'></i></a>`, `<a href='#' class='btn btn-warning btn-sm' data-kid-id='${company.row_id}' data-action="edit" onclick='editCompany(this)'><i class='fas fa-edit'></i></a>`, `<a href='#' class='btn btn-danger btn-sm' data-kid-id='${company.row_id}' onclick='deleteCompany(this)'><i class='fas fa-trash'></i></a>`, company.row_id ]); $('#addModal').modal('hide'); // Close the modal after saving fetchUpdatedDocuments(company_id); }, error: function () { alert('Form has no changes to update knowledge base details.'); } }); table.draw(false); // Draw the table with the new data contentSection.style.display = 'block'; } function dismissMessage() { const messageContainer = document.getElementById('message-container'); if (messageContainer) { messageContainer.innerHTML = ''; } } async function fetchUpdatedDocuments(company_id) { console.log("company_id:", company_id);
var table = $('#apiTable').DataTable(); try { const response = await fetch(`/api/api_updatetable?company_id=${company_id}`); const connectorsResponse = await response.json();
console.log("Fetched connectorsResponse:", connectorsResponse);
table.clear(); // Clear existing data in the table
if (!Array.isArray(connectorsResponse) || connectorsResponse.length === 0) { displayErrorMessage("Knowledgebase details do not exist for this company. Please fill in the details."); contentSection.style.display = 'none'; } else { connectorsResponse.forEach((company, index) => { table.row.add([ index + 1, company.api_name, company.api_endpoint, `<a href='#' class='btn btn-info btn-sm' data-kid-id='${company.row_id}' data-action="view" onclick='viewCompany(this)'><i class='fas fa-eye'></i></a>`, `<a href='#' class='btn btn-warning btn-sm' data-kid-id='${company.row_id}' data-action="edit" onclick='editCompany(this)'><i class='fas fa-edit'></i></a>`, `<a href='#' class='btn btn-danger btn-sm' data-kid-id='${company.row_id}' onclick='deleteCompany(this)'><i class='fas fa-trash'></i></a>`, company.row_id ]); }); table.draw(false); // Draw the table with the new data contentSection.style.display = 'block'; } } catch (error) { console.error("Error fetching updated documents:", error); displayErrorMessage("There was an error fetching the updated documents."); }} } catch (error) { console.error("Error fetching updated documents:", error); displayErrorMessage("There was an error fetching the updated documents."); } }
// Delete company // Delete company function deleteCompany(button) { var companyId = $(button).data('kid-id'); console.log("k_id deleted",companyId); console.log("k_id deleted", companyId); $.ajax({ type: "DELETE", url: `/api/deleteapi/${companyId}`,1 unmodified line var table = $('#apiTable').DataTable(); table.row($(button).closest('tr')).remove().draw(); const messageContainer = document.getElementById('message-container'); if(messageContainer) if (messageContainer) messageContainer.innerHTML = ` <div class='alert alert-danger'> API Data Deleted successfully6 unmodified lines alert("Error deleting company: " + xhr.responseJSON.detail); } }); } }
function save_file() { const form = document.getElementById('documentForm');11 unmodified lines const Inputjson = $('#Inputjson').val(); const OutputJson = $('#OutputJson').val(); const Description = $('#Description').val(); const view = "<a href='#' class='btn btn-info btn-sm'><i class='fas fa-eye'></i></a>"; const edit = "<a href='#' class='btn btn-warning btn-sm'><i class='fas fa-edit'></i></a>"; const dele = "<a href='#' class='btn btn-danger btn-sm'><i class='fas fa-trash'></i></a>";
var formData = new FormData($('#documentForm')[0]);
14 unmodified lines if (!response.ok) { throw new Error('Network response was not ok'); } return response.text(); return response.json(); }) .then(data => { row_id = data.row_id; var table = $('#apiTable').DataTable(); var rowCount = table.rows().count(); const view = `<a href='#' class='btn btn-info btn-sm'data-kid-id='${row_id}' data-action="view" onclick='viewCompany(this)''><i class='fas fa-eye'></i></a>`; const edit = `<a href='#' class='btn btn-warning btn-sm'data-kid-id='${row_id}' data-action="edit" onclick='editCompany(this)'><i class='fas fa-edit'></i></a>`; const dele = `<a href='#' class='btn btn-danger btn-sm' data-kid-id='${row_id}' onclick='deleteCompany(this)'><i class='fas fa-trash'></i></a>`; table.row.add([ rowCount + 1, APIName,3 unmodified lines dele ]).draw(false);
// alert('Document saved successfully'); if(messageContainer) // alert('Document saved successfully'); if (messageContainer) messageContainer.innerHTML = ` <div class='alert alert-success'> API Data saved successfully4 unmodified lines }) .catch(error => console.error('Error:', error)); } async function fetchCompanies() { try { const response = await fetch('/api/companies'); if (!response.ok) { throw new Error('Network response was not ok'); } const data = await response.json(); displayCompanies(data.companies); } catch (error) { console.error('Error fetching companies:', error); } }
function displayCompanies(companies) { const companySelect = document.getElementById('company'); companySelect.innerHTML = '<option value="" selected>Select</option>'; // Reset the dropdown companies.forEach(company => { const option = document.createElement('option'); option.value = company.name; option.textContent = company.name; companySelect.appendChild(option); }); }
function clearFormFields() { document.getElementById('APIName').value = ""; document.getElementById('APIEndpoint').value = "";3 unmodified lines document.getElementById('Description').value = ""; }
</script></body>
templates/API_connectors.html+223-243