diff --git a/layouts/form/single.html b/layouts/form/single.html index 81c731a0c..fe95d1b51 100644 --- a/layouts/form/single.html +++ b/layouts/form/single.html @@ -8,8 +8,8 @@

{{ .Title | markdownify }}

- + href="admin-form.md">Admin +
diff --git a/static/js/admin-form.js b/static/js/admin-form.js new file mode 100644 index 000000000..9018dbddc --- /dev/null +++ b/static/js/admin-form.js @@ -0,0 +1,158 @@ +// =================================== +// Constants and Configuration +// =================================== +const hostname = window.location.hostname; +let serviceHost = ''; + +if (hostname.includes('staging-onprem.rc.virginia.edu') || hostname.includes('staging.rc.virginia.edu')) { + serviceHost = 'https://uvarc-unified-service-test.pods.uvarc.io'; +} else if (hostname === 'rc.virginia.edu') { + serviceHost = 'https://uvarc-unified-service-prod.pods.uvarc.io'; +} else { + console.warn('Unknown environment, defaulting to staging'); + serviceHost = 'https://uvarc-unified-service-test.pods.uvarc.io'; +} + +const API_CONFIG = { + updateUidUrl: `${serviceHost}/uvarc/api/resource/rcadminform/group`, + updateStatusUrl: `${serviceHost}//uvarc/api/resource/rcadminform/group/update`, + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json', + 'X-Requested-With': 'XMLHttpRequest', + 'Origin': window.location.origin + } +}; + + +// ==================== +// Helper Functions +// ==================== +function showMessage(container, message, color = 'red') { + container.html(`

${message}

`); +} + +// ==================== +// Form Submissions +// ==================== + +// --- Update UID Form --- +$(document).on('submit', '#update_uid_form', function(e) { + e.preventDefault(); + + const groupName = $('#group_name_for_update').val().trim(); + const ownerUid = $('#owner_uid').val().trim(); + const responseContainer = $('#updateUidMessage'); + + if (!groupName || !ownerUid) { + showMessage(responseContainer, 'Both Group Name and Owner UID are required.'); + return; + } + const requestUrl = `${API_CONFIG.updateUidUrl}/${groupName}`; + console.log("Request URL:", requestUrl); + $.ajax({ + url: requestUrl, + type: 'PUT', + contentType: 'application/json', + data: JSON.stringify({ owner_uid: ownerUid }), + headers: { + ...API_CONFIG.headers + }, + success: function(response) { + const resObj = Array.isArray(response) ? response[0] : response; + showMessage(responseContainer, resObj.message, resObj.status === 'success' ? 'green' : 'red'); + if (resObj.status === 'success') $('#update_uid_form')[0].reset(); + }, + error: function(xhr) { + const errorMessage = xhr.responseJSON?.message || 'An error occurred while updating UID.'; + showMessage(responseContainer, errorMessage); + } + }); +}); + +// --- Update Status Form --- +$(document).on('submit', '#update_status_form', function(e) { + e.preventDefault(); + + const responseContainer = $('#statusMessage'); + const formData = $(this).serialize(); + + const requestUrl = `${API_CONFIG.updateStatusUrl}`; + console.log("Request URL:", requestUrl); + + $.ajax({ + url: requestUrl, + type: 'PUT', + contentType: 'application/x-www-form-urlencoded', + data: formData, + success: function(response) { + const resObj = Array.isArray(response) ? response[0] : response; + showMessage(responseContainer, resObj.message, resObj.status === 'success' ? 'green' : 'red'); + if (resObj.status === 'success') $('#update_status_form')[0].reset(); + }, + error: function(xhr) { + const errorMessage = xhr.responseJSON?.message || 'An error occurred while updating status.'; + showMessage(responseContainer, errorMessage); + } + }); +}); + +// ==================== +// Tabs +// ==================== +document.addEventListener('DOMContentLoaded', () => { + const tabButtons = document.querySelectorAll('.tab-button'); + const tabContents = document.querySelectorAll('.tab-content'); + + tabButtons.forEach(button => { + button.addEventListener('click', () => { + const tabId = button.getAttribute('data-tab'); + + tabButtons.forEach(btn => btn.classList.remove('active')); + button.classList.add('active'); + + tabContents.forEach(content => { + content.style.display = content.id === `form${tabId}` ? 'block' : 'none'; + }); + }); + }); + + if (tabButtons.length > 0) tabButtons[0].click(); +}); + +// ==================== +// Cancel Buttons +// ==================== +document.addEventListener('click', (e) => { + if (e.target.classList.contains('cancel-button')) { + e.preventDefault(); + + const urlParams = new URLSearchParams(window.location.search); + const referrerParam = urlParams.get('from'); + const fallbackUrl = 'https://www.rc.virginia.edu/userinfo/hpc/access/'; + const redirectUrl = referrerParam || document.referrer || fallbackUrl; + + window.location.href = redirectUrl; + } +}); + +// ==================== +// Page Ready +// ==================== +$(document).ready(function () { + // Remove unnecessary sections + $(".blog-sidebar").remove(); + + // Hide certain rows and remove required attributes + ['#departmet_clasification_row', '#discipline_row'].forEach(r => $(r).hide()); + $('#discipline, #department, #classification').removeAttr('required'); + + // Ensure status message container exists + if ($('#statusMessage').length === 0) { + $('#update_status_form').prepend('
'); + } + if ($('#updateUidMessage').length === 0) { + $('#update_uid_form').prepend('
'); + } + +}); \ No newline at end of file diff --git a/static/js/claim-form.js b/static/js/claim-form.js new file mode 100644 index 000000000..0760a59ca --- /dev/null +++ b/static/js/claim-form.js @@ -0,0 +1,198 @@ +// =================================== + // Constants and Configuration + // =================================== + const hostname = window.location.hostname; + + let serviceHost = ''; + + if (hostname.includes('staging-onprem.rc.virginia.edu') || hostname.includes('staging.rc.virginia.edu')) { + serviceHost = 'http://localhost:5000'; + } else if (hostname === 'rc.virginia.edu') { + serviceHost = 'https://uvarc-unified-service-prod.pods.uvarc.io'; + } else { + console.warn('Unknown environment, defaulting to staging'); + serviceHost = 'https://uvarc-unified-service-test.pods.uvarc.io'; + } + + const API_CONFIG = { + baseUrl: `${serviceHost}/uvarc/api/resource/rcwebform/user`, + groupClaimUrl: `${serviceHost}/uvarc/api/ticket/pi/claim-group`, + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json', + 'X-Requested-With': 'XMLHttpRequest', + 'Origin': window.location.origin + } + }; + + function getUserId() { + const userId = $('#uid').val() || "Unknown"; // Fetch the user ID dynamically + if (userId === "Unknown") { + console.error("User ID is not available. Please ensure you are logged in."); + showErrorMessage("Failed to retrieve user information. Please log in and refresh the page."); + throw new Error("User ID is unknown."); + } + console.log("User ID:", userId); + return userId; + } + + async function fetchAndPopulateGroups(userId) { + try { + // Construct the API request URL + const requestUrl = `${API_CONFIG.baseUrl}/${userId}?user_groups_info=true`; + console.log("Request URL:", requestUrl); + + // Perform the AJAX call using jQuery + const jsonResponse = await $.ajax({ + url: requestUrl, + method: "GET", + headers: { + ...API_CONFIG.headers, + 'Origin': window.location.origin // Dynamically set the origin + }, + credentials: 'include' + }); + + // Save to global variable for further use + consoleData = jsonResponse; + console.log("Fetched groups and resources:", consoleData); + + // Parse and populate user groups and resources + const {userGroups} = parseConsoleData(jsonResponse); + // Populate dropdowns for user groups + if (Array.isArray(userGroups) && userGroups.length > 0) { + console.log("Populating user groups:", userGroups); + populateUserGroupsDropdown(userGroups); + } else { + console.warn("No user groups found."); + populateUserGroupsDropdown([]); + } + + } catch (error) { + console.error("Error fetching user groups:", error); + handleApiError(error); // Display a user-friendly error message + } finally { + // Remove the waiting message + utils?.removeWaitingMessage?.() || waitingMessage.remove(); + } + } + + function parseConsoleData(data) { + if (!Array.isArray(data) || data.length === 0) { + console.error("Invalid consoleData format or empty data:", data); + return {userGroups: []}; + } + const userGroups = data[0]?.user_groups || []; + + console.log("Parsed user groups:", userGroups); + return { userGroups }; + } + + + function populateUserGroupsDropdown(groups) { + const $dropdown = $('#user_groups'); + const currentValue = $dropdown.val(); + // Clear existing options but retain the placeholder + $dropdown.empty(); + $dropdown.append( + $('