client.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. <span class="btn btn-secondary" id="btn_login" style="float:right;top:0;">Login</span>
  25. </div>
  26. </div>
  27. </header>
  28. <main role="main">
  29. <div class="album py-5 bg-light">
  30. <div class="container">
  31. <div class="row" id="user_container">
  32. <label>Retrieve mailbox from</label>
  33. <input type="text" id="user_selector" class="form-control" value="emmey.orlene@yopmail.com">
  34. <span class="alert alert-warning" id="alert"></span>
  35. </div>
  36. <hr>
  37. <div class="row" id="mailbox_container">
  38. </div>
  39. </div>
  40. </div>
  41. </main>
  42. <div class="modal modal-lg fade" id="new_mail_modal" tabindex="-1" role="dialog" aria-labelledby="new_mail_modal" aria-hidden="true">
  43. <div class="modal-dialog" role="document">
  44. <div class="modal-content">
  45. <div class="modal-header">
  46. <h5 class="modal-title">Compose a new email</h5>
  47. <button type="button" class="close" data-dismiss="modal" aria-label="Close">
  48. <span aria-hidden="true">&times;</span>
  49. </button>
  50. </div>
  51. <div class="modal-body">
  52. <form>
  53. <div class="form-group">
  54. <label>To</label>
  55. <input type="email" class="form-control" placeholder="email@domain.com" id="mail_to">
  56. </div>
  57. <div class="form-group">
  58. <label>Subject</label>
  59. <input type="text" class="form-control" placeholder="Manolo" id="mail_subject">
  60. </div>
  61. <div class="form-group">
  62. <label>Body</label>
  63. <textarea id="mail_body" rows="10" class="form-control"></textarea>
  64. </div>
  65. </form>
  66. </div>
  67. <div class="modal-footer">
  68. <span class="btn btn-primary" onclick="send_mail();" data-dismiss="modal">Create</span>
  69. <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
  70. </div>
  71. </div>
  72. </div>
  73. </div>
  74. <div class="modal modal-lg fade" id="login_modal" tabindex="-1" role="dialog" aria-labelledby="login_modal" aria-hidden="true">
  75. <div class="modal-dialog" role="document">
  76. <div class="modal-content">
  77. <div class="modal-header">
  78. <h5 class="modal-title">Login</h5>
  79. <button type="button" class="close" data-dismiss="modal" aria-label="Close">
  80. <span aria-hidden="true">&times;</span>
  81. </button>
  82. </div>
  83. <div class="modal-body">
  84. <form>
  85. <div class="form-group">
  86. <label>Username</label>
  87. <input type="email" class="form-control" id="login_username">
  88. </div>
  89. <div class="form-group">
  90. <label>Password</label>
  91. <input type="password" class="form-control" id="login_password">
  92. </div>
  93. </form>
  94. </div>
  95. <div class="modal-footer">
  96. <span class="btn btn-primary" onclick="do_login();" data-dismiss="modal">Login</span>
  97. <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
  98. </div>
  99. </div>
  100. </div>
  101. </div>
  102. <!-- Bootstrap core JavaScript
  103. ================================================== -->
  104. <!-- Placed at the end of the document so the pages load faster -->
  105. <script src="assets/js/vendor/jquery-slim.min.js"></script>
  106. <script src="assets/js/vendor/popper.min.js"></script>
  107. <script src="dist/js/bootstrap.min.js"></script>
  108. <script src="assets/js/vendor/holder.min.js"></script>
  109. <script>
  110. var tmp = "";
  111. var token = "";
  112. var api = "http://127.0.0.1:8080/api/v1";
  113. var auth = "http://127.0.0.1:8080/auth/authenticate";
  114. $(document).ready(function(){
  115. $('#alert').hide();
  116. $('#btn_new_email').click(function(){$("#new_mail_modal").modal().show();});
  117. $('#btn_login').click(function(){$("#login_modal").modal().show();});
  118. });
  119. function update(){
  120. get_mailbox();
  121. }
  122. function do_login(){
  123. $.ajax({
  124. contentType: 'application/json',
  125. data: JSON.stringify({username:$('#login_username').val(),
  126. password:$('#login_password').val()}),
  127. dataType: 'json',
  128. type: 'POST',
  129. url: auth
  130. })
  131. .done(function(data) {
  132. console.log(data.access_token);
  133. token = data.access_token;
  134. $('#alert').removeClass('alert-warning').addClass('alert-success').text("Correctly athenticated").show();
  135. setTimeout(close_alert, 5000);
  136. })
  137. .fail(function(data, textStatus, xhr) {
  138. $('#alert').removeClass('alert-success').addClass('alert-warning').text("Not autenticated").show();
  139. setTimeout(close_alert, 5000);
  140. });
  141. }
  142. function get_mailbox(){
  143. $.ajax({
  144. type: 'GET',
  145. url: `${api}/${$('#user_selector').val()}`,
  146. headers: {
  147. "Authorization": "Bearer "+token
  148. }
  149. })
  150. .done(function(data) {
  151. $('#mailbox_container').html('');
  152. data.forEach((item, i) => {
  153. $(email_assembler(item)).appendTo($('#mailbox_container'));
  154. });
  155. })
  156. .fail(function(data, textStatus, xhr) {
  157. $('#alert').removeClass('alert-success').addClass('alert-warning').text("Not autenticated").show();
  158. setTimeout(close_alert, 5000);
  159. });
  160. }
  161. function email_assembler(item){
  162. return $.parseHTML(`<div class="col-md-12" id="mail_elem_${item.id}">
  163. <div class="card mb-12 box-shadow">
  164. <div class="card-body">
  165. <span class="btn btn-danger" onclick="delete_mail('${item.id}');" style="float:right;">X</span>
  166. <span class="card-text">From: &nbsp;${item.from.firstname} ${item.from.lastname} (${item.from.email})</span><br>
  167. <span class="card-text">To: &nbsp;${item.to.firstname} ${item.to.lastname} (${item.to.email})</span><br>
  168. <span class="card-text">Date: &nbsp;${item.date}</span><br>
  169. <span class="card-text">Subject: &nbsp;<strong>${item.subject}</strong></span>
  170. </div>
  171. <div class="card-body">
  172. <span class="card-text">${item.body}</span>
  173. </div>
  174. </div>
  175. </div>`);
  176. }
  177. function send_mail(){
  178. $.ajax({
  179. contentType: 'application/json',
  180. data: JSON.stringify({from:$('#user_selector').val(),
  181. to:$('#mail_to').val(),
  182. subject:$('#mail_subject').val(),
  183. body:$('#mail_body').val()}),
  184. dataType: 'json',
  185. type: 'POST',
  186. headers: {
  187. "Authorization": "Bearer "+token
  188. },
  189. url: api
  190. })
  191. .done(function(data) {
  192. console.log(data);
  193. })
  194. .fail(function(data, textStatus, xhr) {
  195. $('#alert').removeClass('alert-success').addClass('alert-warning').text("Not autenticated").show();
  196. setTimeout(close_alert, 5000);
  197. });
  198. $("#new_mail_modal").modal().hide();
  199. }
  200. function delete_mail(id){
  201. $.ajax({
  202. url: `${api}/${id}`,
  203. type: 'DELETE',
  204. headers: {
  205. "Authorization": "Bearer "+token
  206. },
  207. success: function(d) {
  208. console.log(`#mail_elem_${d}`);
  209. $(`#mail_elem_${d}`).remove();
  210. }
  211. })
  212. .fail(function(data, textStatus, xhr) {
  213. $('#alert').removeClass('alert-success').addClass('alert-warning').text("Not autenticated").show();
  214. setTimeout(close_alert, 5000);
  215. });
  216. }
  217. function close_alert(){
  218. $('#alert').hide();
  219. }
  220. </script>
  221. </body>
  222. </html>