client.html 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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="new_user" style="float:right;top:0;">New user</span>
  23. <span class="btn btn-primary" id="new_passwd" style="float:right;top:0;">Password recovery</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. <select id="user_selector" class="form-control"></select>
  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_user_modal" tabindex="-1" role="dialog" aria-labelledby="new_user_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">Create a new user</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>Email</label>
  53. <input type="email" class="form-control" placeholder="email@domain.com" id="user_email">
  54. </div>
  55. <div class="form-group">
  56. <label>Firstname</label>
  57. <input type="text" class="form-control" placeholder="Manolo" id="user_firstname">
  58. </div>
  59. <div class="form-group">
  60. <label>Lastname</label>
  61. <input type="text" class="form-control" placeholder="Martinez" id="user_lastname">
  62. </div>
  63. </form>
  64. </div>
  65. <div class="modal-footer">
  66. <span class="btn btn-primary" onclick="create_user();" 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. <div class="modal modal-lg fade" id="new_passwd_modal" tabindex="-1" role="dialog" aria-labelledby="new_passwd_modal" aria-hidden="true">
  73. <div class="modal-dialog" role="document">
  74. <div class="modal-content">
  75. <div class="modal-header">
  76. <h5 class="modal-title">Password recovery</h5>
  77. <button type="button" class="close" data-dismiss="modal" aria-label="Close">
  78. <span aria-hidden="true">&times;</span>
  79. </button>
  80. </div>
  81. <div class="modal-body">
  82. <form>
  83. <div class="form-group">
  84. <label>Email</label>
  85. <input type="email" class="form-control" placeholder="email@domain.com" id="recover_email">
  86. </div>
  87. </form>
  88. </div>
  89. <div class="modal-footer">
  90. <span class="btn btn-primary" onclick="recover_passwd();" data-dismiss="modal">Create</span>
  91. <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
  92. </div>
  93. </div>
  94. </div>
  95. </div>
  96. <!-- Bootstrap core JavaScript
  97. ================================================== -->
  98. <!-- Placed at the end of the document so the pages load faster -->
  99. <script src="assets/js/vendor/jquery-slim.min.js"></script>
  100. <script src="assets/js/vendor/popper.min.js"></script>
  101. <script src="dist/js/bootstrap.min.js"></script>
  102. <script src="assets/js/vendor/holder.min.js"></script>
  103. <script>
  104. var tmp = "";
  105. $(document).ready(function(){
  106. $('#new_user').click(function(){$("#new_user_modal").modal().show();});
  107. $('#new_passwd').click(function(){$("#new_passwd_modal").modal().show();});
  108. $('#user_selector').change(function(){tmp = $("#user_selector").val();});
  109. update();
  110. setInterval(update, 1000);
  111. });
  112. function update(){
  113. get_mailbox();
  114. get_users();
  115. }
  116. function get_mailbox(){
  117. $.get('http://127.0.0.1:8080/api/v1/mailbox/'+$('#user_selector').val(), function(data) {
  118. $('#mailbox_container').html('');
  119. data.mails.forEach((item, i) => {
  120. $(email_assembler(item)).appendTo($('#mailbox_container'));
  121. });
  122. });
  123. }
  124. function get_users(){
  125. $.get('http://127.0.0.1:8080/api/v1/users', function(data) {
  126. $('#user_selector').html('');
  127. data.forEach((item, i) => {
  128. $(user_assembler(item)).appendTo($('#user_selector'));
  129. });
  130. });
  131. }
  132. function email_assembler(item){
  133. return $.parseHTML(`<div class="col-md-12">
  134. <div class="card mb-12 box-shadow">
  135. <div class="card-body">
  136. <span class="card-text">From: &nbsp;${item.from.email}</span><br>
  137. <span class="card-text">To: &nbsp;${item.to.email}</span><br>
  138. <span class="card-text">Subject: &nbsp;<strong>${item.subject}</strong></span>
  139. </div>
  140. <div class="card-body">
  141. <span class="card-text">${item.body}</span>
  142. </div>
  143. </div>
  144. </div>`);
  145. }
  146. function user_assembler(item){
  147. return $.parseHTML(`<option value="${item.email}" ${item.email == tmp ? 'selected' : ''}>${item.email}</option>`);
  148. }
  149. function create_user(){
  150. $.ajax({
  151. contentType: 'application/json',
  152. data: JSON.stringify({email:$('#user_email').val(), firstname:$('#user_firstname').val(), lastname:$('#user_lastname').val()}),
  153. dataType: 'json',
  154. type: 'POST',
  155. url: 'http://127.0.0.1:8080/api/v1/users'
  156. });
  157. $("#new_user_modal").modal().hide();
  158. }
  159. function recover_passwd(){
  160. $.ajax({
  161. type: 'POST',
  162. url: 'http://127.0.0.1:8080/api/v1/users/'+$('#recover_email').val()+'/passwd'
  163. });
  164. $("#new_passwd_modal").modal().hide();
  165. }
  166. </script>
  167. </body>
  168. </html>