index.html 3.21 KB
Newer Older
Adrian's avatar
Adrian committed
1
2
3
4
5
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
Adrian's avatar
cambios    
Adrian committed
6
  <title>.:: Envío de notificaciones push ::.</title>
Adrian's avatar
Adrian committed
7
8
  <link rel="stylesheet" href="./style.css">
  <script type="text/javascript">
Adrian's avatar
cambios    
Adrian committed
9
10
11
12
13
    if (window.location.protocol.trim() === 'https') {
      var baseURL = 'https://boliviasegura.gob.bo/admin-api';
    } else {
      var baseURL = 'http://localhost:5000';
    }
Adrian's avatar
Adrian committed
14
    function login () {
Adrian's avatar
cambios    
Adrian committed
15
16
17
18
19
20
21
22
23
24
      service(baseURL + '/login', { 
        usuario: document.getElementById('user').value,
        password: document.getElementById('password').value
      })
      .then((data) => {
        if (data.finalizado) {
          window.localStorage.setItem('site_token', data.datos.token);
          document.getElementById('login').style.display = 'none';
          document.getElementById('data').style.display = 'block';
        } else {
Adrian's avatar
Adrian committed
25
26
          document.getElementById('user').value = '';
          document.getElementById('password').value = '';
Adrian's avatar
cambios    
Adrian committed
27
          alert('Credenciales incorrectas');
Adrian's avatar
cambios    
Adrian committed
28
29
        }
      });
Adrian's avatar
cambios    
Adrian committed
30
    } 
Adrian's avatar
cambios    
Adrian committed
31
    async function service(url = '', data = {}, method = 'POST') {
Adrian's avatar
Adrian committed
32
      const response = await fetch(url, {
Adrian's avatar
cambios    
Adrian committed
33
        method: method,
Adrian's avatar
Adrian committed
34
35
36
37
        mode: 'cors',
        cache: 'no-cache',
        credentials: 'same-origin',
        headers: {
Adrian's avatar
cambios    
Adrian committed
38
          'Authorization': 'Bearer ' + window.localStorage.getItem('site_token'),
Adrian's avatar
Adrian committed
39
40
41
42
43
44
45
46
47
          'Content-Type': 'application/json'
        },
        redirect: 'follow',
        referrerPolicy: 'no-referrer',
        body: JSON.stringify(data)
      });
      return await response.json();
    }
    function sendData () {
Adrian's avatar
cambios    
Adrian committed
48
      service(baseURL + '/send', { 
Adrian's avatar
Adrian committed
49
50
        titulo: document.querySelector('#title').value,
        text: document.querySelector('#description').value
Adrian's avatar
Adrian committed
51
52
      })
      .then((data) => {
Adrian's avatar
cambios    
Adrian committed
53
54
55
56
57
        if (data.finalizado) {
          document.querySelector('#title').value = '';
          document.querySelector('#description').value = '';
          alert('Mensaje enviado');
        } else {
Adrian's avatar
Adrian committed
58
          alert('Estimado usuario el token expiró, por favor vuelva a autenticarse');
Adrian's avatar
Adrian committed
59
60
          document.getElementById('user').value = '';
          document.getElementById('password').value = '';
Adrian's avatar
cambios    
Adrian committed
61
62
63
          document.getElementById('login').style.display = 'block';
          document.getElementById('data').style.display = 'none';
        }
Adrian's avatar
Adrian committed
64
65
66
67
68
69
      });
    }
  </script>
</head>
<body>
    <div class="main">
Adrian's avatar
cambios    
Adrian committed
70
      <section class="title">Envío de notificaciones push</section>
Adrian's avatar
Adrian committed
71
72
      <section id="login">
        <form action="#" onsubmit="event.preventDefault(); login();" class="login">
Adrian's avatar
Adrian committed
73
74
          <input type="text" id="user" required placeholder="Usuario">
          <input type="password" id="password" required placeholder="Contraseña">
75
          <button type="submit">INGRESAR</button>
Adrian's avatar
Adrian committed
76
77
78
79
80
81
82
83
84
85
86
87
        </form>
      </section>
      <section id="data">
          <form action="#" onsubmit="event.preventDefault(); sendData()" class="form">
            <input type="text" id="title" required placeholder="Titulo">
            <textarea name="comments" required  placeholder="Descripción" id="description" cols="30" rows="20"></textarea>
            <button type="submit">Enviar</button>
          </form>
      </section>
    </div>
</body>
</html>