Files
hermes/WebApp/wwwroot/js/userSkills.js

184 lines
7.5 KiB
JavaScript

var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { getMyAccount, unhideElementById, getMyRegisteredEventIds } from './generalUseHelpers.js';
var redirected = false;
document.addEventListener("DOMContentLoaded", () => __awaiter(void 0, void 0, void 0, function* () {
var container = document.getElementById("mainContainer");
const modifyBtn = document.getElementById("editBtn");
const removeBtn = document.getElementById("removeBtn");
const applyBtn = document.getElementById("applyBtn");
const leaveBtn = document.getElementById("leaveBtn");
var org_id = -1;
var org_name = "";
try {
var user = yield getMyAccount();
if (user && user.isOrganisation) {
const org_id = user.organisationId;
fetch('/api/organizations/' + org_id)
.then(response => response.json())
.then(data => {
org_name = data.name;
unhideElementById(document, "orgname");
})
.catch(error => {
console.error('Failed to fetch organization:', error);
});
}
else {
unhideElementById(document, "orgno");
}
}
catch (_a) {
window.location.href = "login.html";
}
var thisAccount = null;
thisAccount = yield getMyAccount();
if (thisAccount.isOrganisation)
org_id = thisAccount.organisationId;
if (thisAccount == null) {
if (container !== null)
container.innerHTML = `<p class="text-danger">Błąd we wczytywaniu wydarzenia. <a href="/" style="color:#2898BD;">Powrót -></a></p>`;
}
else {
const nameText = document.getElementById("nameText");
const orgnameText = document.getElementById("orgname");
const dateText = document.getElementById("dateText");
const newdateText = new Date(thisAccount.createdAt).toLocaleDateString('pl-PL');
const newtimeText = new Date(thisAccount.createdAt).toLocaleTimeString('pl-PL');
nameText.innerHTML = thisAccount.firstName + " " + thisAccount.lastName + " (" + thisAccount.email + ")";
dateText.innerHTML = "📅 Account creation date: " + newdateText + " " + newtimeText;
orgnameText.innerHTML = "👥 Organization: " + org_name;
if (org_id == -1) {
unhideElementById(document, "skillscont");
}
else if (org_id == -1) {
// Użytkownik jest wolontariuszem
try {
const registeredIds = yield getMyRegisteredEventIds();
}
catch (_b) {
}
}
unhideElementById(document, "mainContainer");
}
}));
window.onload = () => {
const selectedSkillsContainer = document.getElementById('selected-skills');
const dropdown = document.getElementById('skill-dropdown');
dropdown.addEventListener('change', () => {
const skillName = dropdown.options[dropdown.selectedIndex].text;
const skillId = dropdown.options[dropdown.selectedIndex].value;
if (skillName) {
addSkill(skillName, skillId, false);
dropdown.value = ''; // Reset dropdown
}
});
function fetchSkills() {
fetch('/api/skills')
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
populateDropdown(data);
})
.catch(error => {
console.error('There was a problem with the fetch operation:', error);
});
}
function fetchUserSkills() {
fetch('/api/auth/skills')
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
populateSkills(data);
})
.catch(error => {
console.error('There was a problem with the fetch operation:', error);
});
}
// Populate dropdown with fetched skills
function populateDropdown(skills) {
skills.forEach(skill => {
const option = document.createElement('option');
option.value = skill.skillId.toString();
option.textContent = skill.skillName;
dropdown.appendChild(option);
});
}
function populateSkills(skills) {
skills.forEach(skill => {
addSkill(skill.skillName, skill.skillId, true);
});
}
// Call fetchSkills to populate dropdown on load, same for fetchUserSkills()
fetchSkills();
fetchUserSkills();
function getRandomColor() {
const r = Math.floor(Math.random() * 256);
const g = Math.floor(Math.random() * 256);
const b = Math.floor(Math.random() * 256);
return `rgb(${r}, ${g}, ${b})`;
}
function addSkill(skillName, skillId, dummy_add) {
return __awaiter(this, void 0, void 0, function* () {
if (!document.querySelector(`#selected-skills .skill[data-skill="${skillName}"]`)) {
const skillDiv = document.createElement('div');
skillDiv.className = 'skill';
skillDiv.textContent = skillName;
skillDiv.setAttribute('data-skill', skillName);
skillDiv.style.backgroundColor = getRandomColor();
if (!dummy_add) {
var skill = skillId;
var payload = {
skill
};
var res = yield fetch('/api/auth/add_skill', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
});
var data = yield res.json();
if (res.ok)
skillDiv.remove();
else
alert(data.message);
}
const removeButton = document.createElement('button');
removeButton.textContent = 'X';
removeButton.addEventListener('click', () => __awaiter(this, void 0, void 0, function* () {
var skill = skillId;
var payload = {
skill
};
var res = yield fetch('/api/auth/remove_skill', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
});
var data = yield res.json();
if (res.ok)
skillDiv.remove();
else
alert(data.message);
}));
skillDiv.appendChild(removeButton);
selectedSkillsContainer.appendChild(skillDiv);
}
});
}
};