From a5bb3e56811f9df373d6afdd866adc0af50b1648 Mon Sep 17 00:00:00 2001 From: Piotr Jakubiak <2021323@stud.hs-mannheim.de> Date: Tue, 12 Nov 2024 13:02:47 +0100 Subject: [PATCH] gui: Update add/create Skill,fSS.js, config --- src/main/resources/config/application.yaml | 2 +- .../static/js/skills/fetchSecondarySkills.js | 126 ++++++++++-------- .../resources/templates/pages/skills/add.html | 13 +- .../templates/pages/skills/create.html | 28 ++-- .../templates/pages/skills/overview.html | 4 +- 5 files changed, 97 insertions(+), 76 deletions(-) diff --git a/src/main/resources/config/application.yaml b/src/main/resources/config/application.yaml index 1c11ed0..96ecb80 100644 --- a/src/main/resources/config/application.yaml +++ b/src/main/resources/config/application.yaml @@ -20,7 +20,7 @@ spring: show-sql: true properties: hibernate: - dialect: org.hibernate.dialect.H2Dialect + open-in-view: true # Enable/Disable OpenEntityManagerInViewInterceptor thymeleaf: prefix: "classpath:/templates/" # Path where Thymeleaf templates are stored diff --git a/src/main/resources/static/js/skills/fetchSecondarySkills.js b/src/main/resources/static/js/skills/fetchSecondarySkills.js index 2b30f59..e1c47bb 100644 --- a/src/main/resources/static/js/skills/fetchSecondarySkills.js +++ b/src/main/resources/static/js/skills/fetchSecondarySkills.js @@ -1,6 +1,6 @@ /** * @fileoverview Handles the dynamic population of secondary skills dropdown based on primary skill selection - * @version 4.1.0 + * @version 4.2.0 */ /** @@ -59,7 +59,7 @@ function showTemporaryMessage(message, type, duration = 5000, container) { * @param {HTMLSelectElement} selectElement - The secondary skills select element */ function updateSecondarySkillsDropdown(secondarySkills, selectElement) { - selectElement.innerHTML = ""; // Clear existing options + selectElement.innerHTML = ""; if (!secondarySkills || secondarySkills.length === 0) { selectElement.appendChild( @@ -103,73 +103,83 @@ function initializeSkillsDropdowns() { } secondarySkillSelect.disabled = true; +} - primarySkillSelect.addEventListener("change", async function () { - const selectedPrimarySkillId = this.value; +async function fetchSecondarySkills() { + const primarySkillSelect = document.getElementById("primarySkill"); + const secondarySkillSelect = document.getElementById("secondarySkill"); + const selectedPrimarySkillId = primarySkillSelect.value; + + if (!selectedPrimarySkillId) { secondarySkillSelect.innerHTML = ""; secondarySkillSelect.appendChild( createOption("Select a secondary skill", true, true) ); + secondarySkillSelect.disabled = true; + return; + } - if (!selectedPrimarySkillId) { - secondarySkillSelect.disabled = true; - return; + secondarySkillSelect.innerHTML = ""; + secondarySkillSelect.appendChild(createOption("Loading...", true, true)); + + try { + const response = await fetch( + `/api/secondary-skill/from-primary-skill?psid=${selectedPrimarySkillId}`, + { + method: "GET", + headers: { + "Content-Type": "application/json", + Accept: "application/json", + }, + } + ); + + if (!response.ok) { + const errorMessage = `Server returned ${response.status}: ${response.statusText}`; + throw new Error(errorMessage); } - secondarySkillSelect.innerHTML = ""; - secondarySkillSelect.appendChild(createOption("Loading...", true, true)); + const responseText = await response.text(); + console.log("Response Text:", responseText); + let secondarySkills; try { - const response = await fetch( - `/api/skills/secondary/${selectedPrimarySkillId}`, - { - method: "GET", - headers: { - "Content-Type": "application/json", - }, - } - ); - - if (!response.ok) { - const errorMessage = `Server returned ${response.status}: ${response.statusText}`; - throw new Error(errorMessage); - } - - const secondarySkills = await response.json(); - updateSecondarySkillsDropdown(secondarySkills, secondarySkillSelect); - } catch (error) { - console.error("Error fetching secondary skills:", error); - secondarySkillSelect.innerHTML = ""; - secondarySkillSelect.appendChild( - createOption("Error loading secondary skills", true, true) - ); - secondarySkillSelect.disabled = true; - - let userMessage = ""; - if (error.name === "TypeError" && !window.navigator.onLine) { - userMessage = - "No internet connection. Please check your network and try again."; - } else if (error.message.includes("Server returned 404")) { - userMessage = - "The selected primary skill was not found. Please refresh and try again."; - } else if (error.message.includes("Server returned 500")) { - userMessage = - "Server error occurred. Please try again in a few minutes."; - } else if (error.message.includes("Server returned 403")) { - userMessage = - "You don't have permission to access these skills. Please contact support."; - } else { - userMessage = `Failed to load secondary skills: ${error.message}`; - } - - showTemporaryMessage( - userMessage, - "error", - 5000, - secondarySkillSelect.parentNode - ); + secondarySkills = JSON.parse(responseText); + } catch (jsonError) { + throw new Error(`Failed to parse JSON: ${jsonError.message}`); } - }); + + updateSecondarySkillsDropdown(secondarySkills, secondarySkillSelect); + } catch (error) { + console.error("Error fetching secondary skills:", error); + secondarySkillSelect.innerHTML = ""; + secondarySkillSelect.appendChild( + createOption("Error loading secondary skills", true, true) + ); + secondarySkillSelect.disabled = true; + + let userMessage = ""; + if (error.name === "TypeError" && !window.navigator.onLine) { + userMessage = + "No internet connection. Please check your network and try again."; + } else if (error.message.includes("Server returned 404")) { + userMessage = + "The selected primary skill was not found. Please refresh and try again."; + } else if (error.message.includes("Server returned 500")) { + userMessage = "Server error occurred. Please try again in a few minutes."; + } else if (error.message.includes("Server returned 403")) { + userMessage = + "You don't have permission to access these skills. Please contact support."; + } else { + userMessage = `Failed to load secondary skills: ${error.message}`; + } + showTemporaryMessage( + userMessage, + "error", + 5000, + secondarySkillSelect.parentNode + ); + } } document.addEventListener("DOMContentLoaded", initializeSkillsDropdowns); diff --git a/src/main/resources/templates/pages/skills/add.html b/src/main/resources/templates/pages/skills/add.html index 1b4088b..0f1d3b0 100644 --- a/src/main/resources/templates/pages/skills/add.html +++ b/src/main/resources/templates/pages/skills/add.html @@ -20,8 +20,8 @@