Advertisement
rafatpc

Untitled

Mar 11th, 2019
1,030
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const RESULT_ELEMENT = '#result-1';
  2. const WEB_URL = 'https://pipedream.com/r/en3q7l2t3db32';
  3.  
  4. function request(module, data = {}, target = RESULT_ELEMENT) {
  5.     const params = {
  6.         request: module,
  7.         data
  8.     };
  9.  
  10.     showLoader(target);
  11.  
  12.     return fetch(WEB_URL, {
  13.         mode: 'cors',
  14.         method: 'POST',
  15.         body: JSON.stringify(params),
  16.         headers: {
  17.             'Content-Type': 'application/json'
  18.         },
  19.     }).then(response => {
  20.         if (!response.ok) {
  21.             throw new Error(`[Request] Module <${module}> not found!`);
  22.         }
  23.         return response.text();
  24.     }).then(response => {
  25.         display(response, target);
  26.     }).finally(() => {
  27.         setTimeout(() => {
  28.             hideLoader(target);
  29.         }, 5000);
  30.     });
  31. }
  32.  
  33. function showLoader(selector) {
  34.     const loadingElement = document.querySelector(selector);
  35.     const loader = getLoaderElement();
  36.  
  37.     loadingElement.innerHTML = loader;
  38.     loadingElement.classList.add('spinning');
  39. }
  40.  
  41. function hideLoader(selector) {
  42.     document.querySelector(selector).classList.remove('spinning');
  43. }
  44.  
  45. function getLoaderElement() {
  46.     return '<span>Loading...</span>';
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement