client.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  6. <meta name="description" content="">
  7. <meta name="author" content="">
  8. <link rel="icon" href="favicon.ico">
  9. <title>Mailer API client</title>
  10. <!-- Bootstrap core CSS -->
  11. <link href="dist/css/bootstrap.min.css" rel="stylesheet">
  12. <!-- Custom styles for this template -->
  13. <link href="dist/css/album.css" rel="stylesheet">
  14. </head>
  15. <body>
  16. <header>
  17. <div class="navbar navbar-dark bg-dark box-shadow">
  18. <div class="container d-flex justify-content-between">
  19. <a href="#" class="navbar-brand d-flex align-items-center">
  20. <strong>Mailer API client</strong>
  21. </a>
  22. <span class="btn btn-primary" id="btn_new_email" style="float:right;top:0;">New email</span>
  23. <span class="btn btn-warning" id="btn_refresh" style="float:right;top:0;" onclick="update();">Refresh</span>
  24. </div>
  25. </div>
  26. </header>
  27. <main role="main">
  28. <div class="album py-5 bg-light">
  29. <div class="container">
  30. <div class="row" id="user_container">
  31. <label>Retrieve mailbox from</label>
  32. <input type="text" id="user_selector" class="form-control" value="emmey.orlene@yopmail.com">
  33. </div>
  34. <hr>
  35. <div class="row" id="mailbox_container">
  36. </div>
  37. </div>
  38. </div>
  39. </main>
  40. <div class="modal modal-lg fade" id="new_mail_modal" tabindex="-1" role="dialog" aria-labelledby="new_mail_modal" aria-hidden="true">
  41. <div class="modal-dialog" role="document">
  42. <div class="modal-content">
  43. <div class="modal-header">
  44. <h5 class="modal-title">Compose a new email</h5>
  45. <button type="button" class="close" data-dismiss="modal" aria-label="Close">
  46. <span aria-hidden="true">&times;</span>
  47. </button>
  48. </div>
  49. <div class="modal-body">
  50. <form>
  51. <div class="form-group">
  52. <label>To</label>
  53. <input type="email" class="form-control" placeholder="email@domain.com" id="mail_to">
  54. </div>
  55. <div class="form-group">
  56. <label>Subject</label>
  57. <input type="text" class="form-control" placeholder="Manolo" id="mail_subject">
  58. </div>
  59. <div class="form-group">
  60. <label>Body</label>
  61. <textarea id="mail_body" rows="10" class="form-control"></textarea>
  62. </div>
  63. </form>
  64. </div>
  65. <div class="modal-footer">
  66. <span class="btn btn-primary" onclick="send_mail();" data-dismiss="modal">Create</span>
  67. <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
  68. </div>
  69. </div>
  70. </div>
  71. </div>
  72. <!-- Bootstrap core JavaScript
  73. ================================================== -->
  74. <!-- Placed at the end of the document so the pages load faster -->
  75. <script src="assets/js/vendor/jquery-slim.min.js"></script>
  76. <script src="assets/js/vendor/popper.min.js"></script>
  77. <script src="dist/js/bootstrap.min.js"></script>
  78. <script src="assets/js/vendor/holder.min.js"></script>
  79. <script>
  80. var tmp = "";
  81. var api = "http://127.0.0.1:8080/api/v1";
  82. $(document).ready(function(){
  83. update();
  84. $('#btn_new_email').click(function(){$("#new_mail_modal").modal().show();});
  85. //setInterval(update, 5000);
  86. });
  87. function update(){
  88. get_mailbox();
  89. }
  90. function get_mailbox(){
  91. $.get(`${api}/${$('#user_selector').val()}`, function(data) {
  92. $('#mailbox_container').html('');
  93. data.forEach((item, i) => {
  94. $(email_assembler(item)).appendTo($('#mailbox_container'));
  95. });
  96. });
  97. }
  98. function email_assembler(item){
  99. return $.parseHTML(`<div class="col-md-12" id="mail_elem_${item.id}">
  100. <div class="card mb-12 box-shadow">
  101. <div class="card-body">
  102. <span class="btn btn-danger" onclick="delete_mail('${item.id}');" style="float:right;">X</span>
  103. <span class="card-text">From: &nbsp;${item.from.firstname} ${item.from.lastname} (${item.from.email})</span><br>
  104. <span class="card-text">To: &nbsp;${item.to.firstname} ${item.to.lastname} (${item.to.email})</span><br>
  105. <span class="card-text">Date: &nbsp;${item.date}</span><br>
  106. <span class="card-text">Subject: &nbsp;<strong>${item.subject}</strong></span>
  107. </div>
  108. <div class="card-body">
  109. <span class="card-text">${item.body}</span>
  110. </div>
  111. </div>
  112. </div>`);
  113. }
  114. function send_mail(){
  115. $.ajax({
  116. contentType: 'application/json',
  117. data: JSON.stringify({from:$('#user_selector').val(),
  118. to:$('#mail_to').val(),
  119. subject:$('#mail_subject').val(),
  120. body:$('#mail_body').val()}),
  121. dataType: 'json',
  122. type: 'POST',
  123. url: api
  124. })
  125. .done(function(data) {
  126. console.log(data);
  127. });
  128. $("#new_mail_modal").modal().hide();
  129. }
  130. function delete_mail(id){
  131. $.ajax({
  132. url: `${api}/${id}`,
  133. type: 'DELETE',
  134. success: function(d) {
  135. console.log(`#mail_elem_${d}`);
  136. $(`#mail_elem_${d}`).remove();
  137. }
  138. });
  139. }
  140. </script>
  141. </body>
  142. </html>