123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- <!doctype html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
- <meta name="description" content="">
- <meta name="author" content="">
- <link rel="icon" href="favicon.ico">
- <title>Mailer API client</title>
- <!-- Bootstrap core CSS -->
- <link href="dist/css/bootstrap.min.css" rel="stylesheet">
- <!-- Custom styles for this template -->
- <link href="dist/css/album.css" rel="stylesheet">
- </head>
- <body>
- <header>
- <div class="navbar navbar-dark bg-dark box-shadow">
- <div class="container d-flex justify-content-between">
- <a href="#" class="navbar-brand d-flex align-items-center">
- <strong>Mailer API client</strong>
- </a>
- <span class="btn btn-primary" id="btn_new_email" style="float:right;top:0;">New email</span>
- <span class="btn btn-warning" id="btn_refresh" style="float:right;top:0;" onclick="update();">Refresh</span>
- <span class="btn btn-secondary" id="btn_login" style="float:right;top:0;">Login</span>
- </div>
- </div>
- </header>
- <main role="main">
- <div class="album py-5 bg-light">
- <div class="container">
- <div class="row" id="user_container">
- <label>Retrieve mailbox from</label>
- <input type="text" id="user_selector" class="form-control" value="emmey.orlene@yopmail.com">
- <span class="alert alert-warning" id="alert"></span>
- </div>
- <hr>
- <div class="row" id="mailbox_container">
- </div>
- </div>
- </div>
- </main>
-
- <div class="modal modal-lg fade" id="new_mail_modal" tabindex="-1" role="dialog" aria-labelledby="new_mail_modal" aria-hidden="true">
- <div class="modal-dialog" role="document">
- <div class="modal-content">
- <div class="modal-header">
- <h5 class="modal-title">Compose a new email</h5>
- <button type="button" class="close" data-dismiss="modal" aria-label="Close">
- <span aria-hidden="true">×</span>
- </button>
- </div>
- <div class="modal-body">
- <form>
- <div class="form-group">
- <label>To</label>
- <input type="email" class="form-control" placeholder="email@domain.com" id="mail_to">
- </div>
- <div class="form-group">
- <label>Subject</label>
- <input type="text" class="form-control" placeholder="Manolo" id="mail_subject">
- </div>
- <div class="form-group">
- <label>Body</label>
- <textarea id="mail_body" rows="10" class="form-control"></textarea>
- </div>
- </form>
- </div>
- <div class="modal-footer">
- <span class="btn btn-primary" onclick="send_mail();" data-dismiss="modal">Create</span>
- <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
- </div>
- </div>
- </div>
- </div>
-
- <div class="modal modal-lg fade" id="login_modal" tabindex="-1" role="dialog" aria-labelledby="login_modal" aria-hidden="true">
- <div class="modal-dialog" role="document">
- <div class="modal-content">
- <div class="modal-header">
- <h5 class="modal-title">Login</h5>
- <button type="button" class="close" data-dismiss="modal" aria-label="Close">
- <span aria-hidden="true">×</span>
- </button>
- </div>
- <div class="modal-body">
- <form>
- <div class="form-group">
- <label>Username</label>
- <input type="email" class="form-control" id="login_username">
- </div>
- <div class="form-group">
- <label>Password</label>
- <input type="password" class="form-control" id="login_password">
- </div>
- </form>
- </div>
- <div class="modal-footer">
- <span class="btn btn-primary" onclick="do_login();" data-dismiss="modal">Login</span>
- <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
- </div>
- </div>
- </div>
- </div>
-
-
- <!-- Bootstrap core JavaScript
- ================================================== -->
- <!-- Placed at the end of the document so the pages load faster -->
- <script src="assets/js/vendor/jquery-slim.min.js"></script>
- <script src="assets/js/vendor/popper.min.js"></script>
- <script src="dist/js/bootstrap.min.js"></script>
- <script src="assets/js/vendor/holder.min.js"></script>
- <script>
- var tmp = "";
- var token = "";
- var api = "http://127.0.0.1:8080/api/v1";
- var auth = "http://127.0.0.1:8080/auth/authenticate";
- $(document).ready(function(){
- $('#alert').hide();
- $('#btn_new_email').click(function(){$("#new_mail_modal").modal().show();});
- $('#btn_login').click(function(){$("#login_modal").modal().show();});
- });
-
- function update(){
- get_mailbox();
- }
-
- function do_login(){
- $.ajax({
- contentType: 'application/json',
- data: JSON.stringify({username:$('#login_username').val(),
- password:$('#login_password').val()}),
- dataType: 'json',
- type: 'POST',
- url: auth
- })
- .done(function(data) {
- console.log(data.access_token);
- token = data.access_token;
- $('#alert').removeClass('alert-warning').addClass('alert-success').text("Correctly athenticated").show();
- setTimeout(close_alert, 5000);
- })
- .fail(function(data, textStatus, xhr) {
- $('#alert').removeClass('alert-success').addClass('alert-warning').text("Not autenticated").show();
- setTimeout(close_alert, 5000);
- });
- }
-
- function get_mailbox(){
- $.ajax({
- type: 'GET',
- url: `${api}/${$('#user_selector').val()}`,
- headers: {
- "Authorization": "Bearer "+token
- }
- })
- .done(function(data) {
- $('#mailbox_container').html('');
- data.forEach((item, i) => {
- $(email_assembler(item)).appendTo($('#mailbox_container'));
- });
- })
- .fail(function(data, textStatus, xhr) {
- $('#alert').removeClass('alert-success').addClass('alert-warning').text("Not autenticated").show();
- setTimeout(close_alert, 5000);
- });
- }
-
- function email_assembler(item){
- return $.parseHTML(`<div class="col-md-12" id="mail_elem_${item.id}">
- <div class="card mb-12 box-shadow">
- <div class="card-body">
- <span class="btn btn-danger" onclick="delete_mail('${item.id}');" style="float:right;">X</span>
- <span class="card-text">From: ${item.from.firstname} ${item.from.lastname} (${item.from.email})</span><br>
- <span class="card-text">To: ${item.to.firstname} ${item.to.lastname} (${item.to.email})</span><br>
- <span class="card-text">Date: ${item.date}</span><br>
- <span class="card-text">Subject: <strong>${item.subject}</strong></span>
- </div>
- <div class="card-body">
- <span class="card-text">${item.body}</span>
- </div>
- </div>
- </div>`);
- }
-
- function send_mail(){
- $.ajax({
- contentType: 'application/json',
- data: JSON.stringify({from:$('#user_selector').val(),
- to:$('#mail_to').val(),
- subject:$('#mail_subject').val(),
- body:$('#mail_body').val()}),
- dataType: 'json',
- type: 'POST',
- headers: {
- "Authorization": "Bearer "+token
- },
- url: api
- })
- .done(function(data) {
- console.log(data);
- })
- .fail(function(data, textStatus, xhr) {
- $('#alert').removeClass('alert-success').addClass('alert-warning').text("Not autenticated").show();
- setTimeout(close_alert, 5000);
- });
-
- $("#new_mail_modal").modal().hide();
- }
-
- function delete_mail(id){
- $.ajax({
- url: `${api}/${id}`,
- type: 'DELETE',
- headers: {
- "Authorization": "Bearer "+token
- },
- success: function(d) {
- console.log(`#mail_elem_${d}`);
- $(`#mail_elem_${d}`).remove();
- }
- })
- .fail(function(data, textStatus, xhr) {
- $('#alert').removeClass('alert-success').addClass('alert-warning').text("Not autenticated").show();
- setTimeout(close_alert, 5000);
- });
- }
-
- function close_alert(){
- $('#alert').hide();
- }
- </script>
- </body>
- </html>
|