Advertisement
didkoslawow

Book Library

Jun 26th, 2023 (edited)
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function attachEvents() {
  2.    document.getElementById('loadBooks').addEventListener('click', render);
  3.    document.querySelector('form button').addEventListener('click', onSubmit);
  4.  }
  5.  
  6.  attachEvents();
  7.  render();
  8.  
  9.  async function request(url, id, options) {
  10.    if (options && options.body != undefined) {
  11.      Object.assign(options, {
  12.        headers: {
  13.          'Content-Type': 'application/json'
  14.        }
  15.      })
  16.    }
  17.  
  18.    const response = await fetch(url + id, options);
  19.    const data = await response.json();
  20.  
  21.    return data;
  22.  }
  23.  
  24.  async function render() {
  25.    const data = await request(
  26.      'http://localhost:3030/jsonstore/collections/books/', ''
  27.    );
  28.  
  29.    const tbody = document.querySelector('tbody');
  30.  
  31.    const fragment = document.createDocumentFragment();
  32.  
  33.    Object.entries(data).forEach(book => {
  34.      const tr = createElement('tr', '', book[0]);
  35.      tr.appendChild(createElement('td', book[1].title));
  36.      tr.appendChild(createElement('td', book[1].author));
  37.  
  38.      const td = createElement('td');
  39.      td.appendChild(createElement('button', 'Edit'));
  40.      td.appendChild(createElement('button', 'Delete'));
  41.      tr.appendChild(td);
  42.  
  43.      fragment.appendChild(tr);
  44.    });
  45.  
  46.    tbody.replaceChildren(fragment);
  47.  }
  48.  
  49.  function onSubmit(e) {
  50.    e.preventDefault();
  51.  
  52.    if (e.target.textContent == 'Submit') {
  53.      createBook();
  54.    }
  55.  }
  56.  
  57.  function createBook() {
  58.    const author = document.querySelector('[name="author"]');
  59.    const title = document.querySelector('[name="title"]');
  60.    const body = { author: author.value, title: title.value };
  61.  
  62.    if (body.author && body.title) {
  63.      request('http://localhost:3030/jsonstore/collections/books/', '', {
  64.        method: 'POST',
  65.        body: JSON.stringify(body),
  66.      });
  67.  
  68.      author.value = '';
  69.      title.value = '';
  70.  
  71.      render();
  72.    }
  73.  }
  74.  
  75.  async function deleteBook(id) {
  76.    await request('http://localhost:3030/jsonstore/collections/books/', id, {
  77.      method: 'DELETE',
  78.    });
  79.  }
  80.  
  81.  let id;
  82.  
  83.  function onEdit(e) {
  84.    id = e.target.parentElement.parentElement.id;
  85.    const title = document.getElementById(id).children[0].textContent;
  86.    const author = document.getElementById(id).children[1].textContent;
  87.    const button = document.querySelector('form button');
  88.    document.querySelector('form h3').textContent = 'Edit FORM';
  89.    document.querySelector('[name="title"]').value = title;
  90.    document.querySelector('[name="author"]').value = author;
  91.    button.textContent = 'Save';
  92.    button.removeAttribute('click', onSubmit);
  93.    button.addEventListener('click', saveBook);
  94.  
  95.    document.getElementById(id).remove();
  96.  }
  97.  
  98.  async function saveBook() {
  99.    const button = document.querySelector('form button');
  100.    const author = document.querySelector('[name="author"]');
  101.    const title = document.querySelector('[name="title"]');
  102.    const body = { author: author.value, title: title.value };
  103.  
  104.    await request(
  105.      'http://localhost:3030/jsonstore/collections/books/',
  106.      id,
  107.      {
  108.        method: 'PUT',
  109.        body: JSON.stringify(body),
  110.      }
  111.    );
  112.  
  113.    button.removeAttribute('click', saveBook);
  114.    button.addEventListener('click', onSubmit);
  115.    button.textContent = 'Submit';
  116.    document.querySelector('form h3').textContent = 'FORM';
  117.    author.value = '';
  118.    title.value = '';
  119.    render();
  120.  }
  121.  
  122.  function onDelete(e) {
  123.    const id = e.target.parentElement.parentElement.id;
  124.    deleteBook(id);
  125.    render();
  126.  }
  127.  
  128.  function createElement(type, content, id) {
  129.    const element = document.createElement(type);
  130.  
  131.    if (type == 'button') {
  132.      const ev = content == 'Edit' ? onEdit : onDelete;
  133.      element.addEventListener('click', ev);
  134.    }
  135.  
  136.    if (content) {
  137.      element.textContent = content;
  138.    }
  139.  
  140.    if (id != undefined) {
  141.      element.id = id;
  142.    }
  143.  
  144.    return element;
  145.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement