Advertisement
Kamend1

05.Locked-profile

Apr 3rd, 2025
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. async function lockedProfile() {
  2.      
  3.     let baseUrl = `http://localhost:3030/jsonstore/advanced/profiles`;
  4.     let mainSite = document.getElementById('main');
  5.  
  6.     mainSite.innerHTML = '';
  7.  
  8.         try {
  9.             let response = await fetch(baseUrl);
  10.             let profileData = await response.json();
  11.             let profiles = Object.values(profileData);
  12.            
  13.             for (let profile of profiles) {
  14.                 let username = profile.username;
  15.                 let email = profile.email;
  16.                 let age = profile.age;
  17.                 let idx = profiles.indexOf(profile) + 1;
  18.  
  19.                 let profileCard = createProfileCard(username, email, age, idx);
  20.                 mainSite.appendChild(profileCard);
  21.             }
  22.  
  23.         } catch (error) {
  24.             console.error(error);
  25.         }
  26.        
  27.     function createProfileCard(username, email, age, userIndex) {
  28.         let profileCard = document.createElement('div');
  29.         profileCard.classList.add('profile');
  30.    
  31.         let img = document.createElement('img');
  32.         img.src = './iconProfile2.png';
  33.         img.classList.add('userIcon');
  34.    
  35.         let lockLabel = document.createElement('label');
  36.         lockLabel.textContent = 'Lock';
  37.    
  38.         let lockRadio = document.createElement('input');
  39.         lockRadio.type = 'radio';
  40.         lockRadio.name = `user${userIndex}Locked`;
  41.         lockRadio.value = 'lock';
  42.         lockRadio.checked = true;
  43.    
  44.         let unlockLabel = document.createElement('label');
  45.         unlockLabel.textContent = 'Unlock';
  46.    
  47.         let unlockRadio = document.createElement('input');
  48.         unlockRadio.type = 'radio';
  49.         unlockRadio.name = `user${userIndex}Locked`;
  50.         unlockRadio.value = 'unlock';
  51.    
  52.         let br = document.createElement('br');
  53.         let hr1 = document.createElement('hr');
  54.    
  55.         let usernameLabel = document.createElement('label');
  56.         usernameLabel.textContent = 'Username';
  57.    
  58.         let usernameInput = document.createElement('input');
  59.         usernameInput.type = 'text';
  60.         usernameInput.name = `user${userIndex}Username`;
  61.         usernameInput.value = username;
  62.         usernameInput.disabled = true;
  63.         usernameInput.readOnly = true;
  64.    
  65.         let hiddenDiv = document.createElement('div');
  66.         hiddenDiv.classList.add(`user${userIndex}Username`);
  67.         hiddenDiv.style.display = 'none';
  68.    
  69.         let hr2 = document.createElement('hr');
  70.    
  71.         let emailLabel = document.createElement('label');
  72.         emailLabel.textContent = 'Email:';
  73.    
  74.         let emailInput = document.createElement('input');
  75.         emailInput.type = 'email';
  76.         emailInput.name = `user${userIndex}Email`;
  77.         emailInput.value = email;
  78.         emailInput.disabled = true;
  79.         emailInput.readOnly = true;
  80.    
  81.         let ageLabel = document.createElement('label');
  82.         ageLabel.textContent = 'Age:';
  83.    
  84.         let ageInput = document.createElement('input');
  85.         ageInput.type = 'number';
  86.         ageInput.name = `user${userIndex}Age`;
  87.         ageInput.value = age;
  88.         ageInput.disabled = true;
  89.         ageInput.readOnly = true;
  90.    
  91.         hiddenDiv.appendChild(hr2);
  92.         hiddenDiv.appendChild(emailLabel);
  93.         hiddenDiv.appendChild(emailInput);
  94.         hiddenDiv.appendChild(ageLabel);
  95.         hiddenDiv.appendChild(ageInput);
  96.    
  97.         let showMoreBtn = document.createElement('button');
  98.         showMoreBtn.textContent = 'Show more';
  99.    
  100.         showMoreBtn.addEventListener('click', showMore);
  101.    
  102.         profileCard.appendChild(img);
  103.         profileCard.appendChild(lockLabel);
  104.         profileCard.appendChild(lockRadio);
  105.         profileCard.appendChild(unlockLabel);
  106.         profileCard.appendChild(unlockRadio);
  107.         profileCard.appendChild(br);
  108.         profileCard.appendChild(hr1);
  109.         profileCard.appendChild(usernameLabel);
  110.         profileCard.appendChild(usernameInput);
  111.         profileCard.appendChild(hiddenDiv);
  112.         profileCard.appendChild(showMoreBtn);
  113.    
  114.         return profileCard;
  115.     }
  116.    
  117.     function showMore (event) {
  118.         let button = event.currentTarget;
  119.         let currentProfile = event.currentTarget.parentElement;
  120.         let currentProfileHidden = currentProfile.querySelector('div:last-of-type');
  121.         let currentProfileLock = currentProfile.querySelector('input[type="radio"][value="lock"]');
  122.         let isLocked = currentProfileLock.checked;
  123.  
  124.         if (button.textContent === 'Show more') {
  125.            
  126.             if(!isLocked) {
  127.                 button.textContent = 'Hide it';
  128.                 currentProfileHidden.style.display = 'block';
  129.             }
  130.         } else {
  131.             button.textContent = 'Show more';
  132.             currentProfileHidden.style.display = 'none';
  133.         }
  134.     }
  135. }
  136.  
  137.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement