Advertisement
bebo231312312321

Untitled

Oct 27th, 2024
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function lockedProfile() {
  2.  
  3.     const mainElement = document.querySelector('#main');
  4.  
  5.     let url = 'http://localhost:3030/jsonstore/advanced/profiles';
  6.  
  7.     fetch(url)
  8.         .then(response => response.json())
  9.         .then(data => {
  10.  
  11.             mainElement.innerHTML = '';
  12.             Object.values(data).forEach((el, index) => createProfile(el, index + 1));
  13.         });
  14.  
  15.     function createProfile(data, userIndex) {
  16.  
  17.         const profileDiv = createEl('div', { class: 'profile' });
  18.  
  19.         const img = createEl('img', { src: './iconProfile2.png', class: 'userIcon' });
  20.         profileDiv.appendChild(img);
  21.  
  22.         profileDiv.appendChild(createEl('label', {}, 'Lock'));
  23.         const inputLock = createEl('input', {
  24.             type: 'radio',
  25.             name: `user${userIndex}Locked`,
  26.             checked: true
  27.         });
  28.         profileDiv.appendChild(inputLock);
  29.  
  30.         profileDiv.appendChild(createEl('label', {}, 'Unlock'));
  31.         const inputUnlock = createEl('input', {
  32.             type: 'radio',
  33.             name: `user${userIndex}Locked`,
  34.             value: 'unlock'
  35.         });
  36.         profileDiv.appendChild(inputUnlock);
  37.  
  38.         profileDiv.appendChild(document.createElement('br'));
  39.         profileDiv.appendChild(document.createElement('hr'));
  40.  
  41.         profileDiv.appendChild(createEl('label', {}, 'Username'));
  42.         profileDiv.appendChild(createEl('input', {
  43.             type: 'text',
  44.             name: `user${userIndex}Username`,
  45.             value: data.username,
  46.             disabled: true,
  47.             readOnly: true
  48.         }));
  49.  
  50.         const userHiddenFieldsDiv = createEl('div', { id: `user${userIndex}HiddenFields`, style: 'display: none' });
  51.         userHiddenFieldsDiv.appendChild(document.createElement('hr'));
  52.  
  53.         userHiddenFieldsDiv.appendChild(createEl('label', {}, 'Email:'));
  54.         userHiddenFieldsDiv.appendChild(createEl('input', {
  55.             type: 'email',
  56.             name: `user${userIndex}Email`,
  57.             value: data.email,
  58.             disabled: true,
  59.             readOnly: true
  60.         }));
  61.  
  62.         userHiddenFieldsDiv.appendChild(createEl('label', {}, 'Age:'));
  63.         userHiddenFieldsDiv.appendChild(createEl('input', {
  64.             type: 'number',
  65.             name: `user${userIndex}Age`,
  66.             value: data.age,
  67.             disabled: true,
  68.             readOnly: true
  69.         }));
  70.  
  71.         profileDiv.appendChild(userHiddenFieldsDiv);
  72.  
  73.         const showMoreBtn = createEl('button', {}, 'Show more');
  74.         showMoreBtn.addEventListener('click', () => {
  75.             if (inputUnlock.checked) {
  76.                 if (userHiddenFieldsDiv.style.display === 'none') {
  77.                     userHiddenFieldsDiv.style.display = 'block';
  78.                     showMoreBtn.textContent = 'Hide it';
  79.                 } else {
  80.                     userHiddenFieldsDiv.style.display = 'none';
  81.                     showMoreBtn.textContent = 'Show more';
  82.                 }
  83.             }
  84.         });
  85.         profileDiv.appendChild(showMoreBtn);
  86.  
  87.         mainElement.appendChild(profileDiv);
  88.     }
  89.  
  90.     function createEl(tag, attributes = {}, text = '') {
  91.         const element = document.createElement(tag);
  92.  
  93.         for (const [key, value] of Object.entries(attributes)) {
  94.             element.setAttribute(key, value);
  95.         }
  96.  
  97.         if (text) {
  98.             element.textContent = text;
  99.         }
  100.  
  101.         return element;
  102.     }
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement