PageRenderTime 29ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/src/public/js/bundle.js

https://gitlab.com/sudobat/micro-blog-front
JavaScript | 245 lines | 172 code | 58 blank | 15 comment | 3 complexity | 8b57a71db13821d61700c3ab046d1c5a MD5 | raw file
  1. document.addEventListener("DOMContentLoaded", () => {
  2. console.log('Microblog v1');
  3. // Common functionallity
  4. const responseToJson = function(response) {
  5. return response.json();
  6. };
  7. const acceptJsonHeaders = new Headers({
  8. 'Accept': 'application/json'
  9. });
  10. // Articles API
  11. const articleContainer = document.getElementById('blog-article');
  12. const getArticleRequest = new Request('http://localhost/api/v1/articles/8', {
  13. method: 'GET',
  14. mode: 'cors',
  15. headers: acceptJsonHeaders
  16. });
  17. const fillArticleContainer = function(article) {
  18. console.log('Article', article);
  19. const articleTitleContainer = document.createElement('h1');
  20. articleTitleContainer.innerHTML = article.title;
  21. const articleContentContainer = document.createElement('div');
  22. articleContentContainer.innerHTML = article.content;
  23. // Clear previous Article
  24. while (articleContainer.lastChild) {
  25. articleContainer.removeChild(articleContainer.lastChild);
  26. }
  27. // Add Article
  28. articleContainer.appendChild(articleTitleContainer);
  29. articleContainer.appendChild(articleContentContainer);
  30. };
  31. const fallbackArticle = function(error) {
  32. console.log('article error', error);
  33. };
  34. fetch(getArticleRequest)
  35. .then(responseToJson)
  36. .then(fillArticleContainer)
  37. .catch(fallbackArticle);
  38. // Comments API
  39. const commentsContainer = document.getElementById('blog-comments');
  40. const commentsForm = document.getElementById('blog-comments-form');
  41. const getCommentsRequest = new Request('http://localhost/api/v1/comments', {
  42. method: 'GET',
  43. mode: 'cors',
  44. headers: acceptJsonHeaders
  45. });
  46. const createCommentRequest = function() {
  47. return new Request('http://localhost/api/v1/comments', {
  48. method: 'POST',
  49. body: new FormData(commentsForm),
  50. mode: 'cors',
  51. headers: acceptJsonHeaders
  52. });
  53. };
  54. const fillCommentsContainer = function(comments) {
  55. console.log('Comments', comments);
  56. // Clear previous Comments
  57. while (commentsContainer.lastChild) {
  58. commentsContainer.removeChild(commentsContainer.lastChild);
  59. }
  60. // Add all the Comments
  61. comments.forEach(comment => {
  62. const commentContainer = document.createElement('div');
  63. commentContainer.setAttribute('class', 'blog-article-comment');
  64. const commentContent = document.createElement('p');
  65. commentContent.innerHTML = comment.content;
  66. commentContainer.appendChild(commentContent);
  67. commentsContainer.appendChild(commentContainer);
  68. });
  69. };
  70. const fallbackComments = function(error) {
  71. console.log('comments error', error);
  72. };
  73. const fallbackCommentSubmit = function(error) {
  74. console.log('comment submit error', error);
  75. }
  76. fetch(getCommentsRequest)
  77. .then(responseToJson)
  78. .then(fillCommentsContainer)
  79. .catch(fallbackComments);
  80. commentsForm.addEventListener('submit', function(e) {
  81. e.preventDefault();
  82. fetch(createCommentRequest())
  83. .then(responseToJson)
  84. .then(function(newComment) {
  85. console.log('comment submit response', newComment);
  86. // Create Comment HTML
  87. const commentContainer = document.createElement('div');
  88. commentContainer.setAttribute('class', 'blog-article-comment');
  89. const commentContent = document.createElement('p');
  90. commentContent.innerHTML = newComment.content;
  91. // Prepend new Comment
  92. commentsContainer.insertBefore(commentContent, commentsContainer.firstChild);
  93. // Clean current Comment Form
  94. commentsForm.reset();
  95. })
  96. .catch(fallbackCommentSubmit);
  97. });
  98. // Users API
  99. let currentUser = null;
  100. const registerForm = document.getElementById('blog-register-form');
  101. const loginForm = document.getElementById('blog-login-form');
  102. const usersContainer = document.getElementById('blog-users');
  103. const userInfoContainer = document.getElementById('blog-user-info');
  104. const userSignUpButton = document.getElementById('blog-user-sign-up-button');
  105. const getUsersRequest = new Request('http://localhost/api/v1/users', {
  106. method: 'GET',
  107. mode: 'cors',
  108. headers: new Headers({
  109. 'Accept': 'application/json'
  110. })
  111. });
  112. const createUserRequest = function() {
  113. return new Request('http://localhost/api/v1/users', {
  114. method: 'POST',
  115. body: new FormData(registerForm),
  116. mode: 'cors',
  117. headers: acceptJsonHeaders
  118. });
  119. };
  120. const loginRegisteredUser = function(newUser) {
  121. console.log('user register submit response', newUser);
  122. currentUser = newUser;
  123. };
  124. const createUserError = function(error) {
  125. console.log('user register submit error', error);
  126. };
  127. const loginUserRequest = function() {
  128. const formData = new FormData(loginForm);
  129. const email = formData.get('email');
  130. const password = formData.get('password');
  131. return new Request('http://localhost/api/v1/users/login?email='+email+'&password='+password, {
  132. method: 'GET',
  133. mode: 'cors',
  134. headers: acceptJsonHeaders
  135. });
  136. };
  137. const loginUser = function(user) {
  138. console.log('user login submit response', user);
  139. currentUser = user;
  140. userInfoContainer.innerHTML = user.first_name + ' ' + user.last_name;
  141. userSignUpButton.className = "hidden";
  142. userInfoContainer.className = userInfoContainer.className.replace(/\bhidden\b/, "");
  143. };
  144. const loginUserError = function(error) {
  145. console.log('user login submit error', error);
  146. };
  147. const fillUsersContainer = function(users) {
  148. console.log('Users', users);
  149. // Clear previous Users
  150. while (usersContainer.lastChild) {
  151. usersContainer.removeChild(usersContainer.lastChild);
  152. }
  153. // Add all the Users
  154. users.forEach(user => {
  155. const userContainer = document.createElement('div');
  156. userContainer.setAttribute('class', 'blog-user');
  157. const userContent = document.createElement('a');
  158. userContent.setAttribute('href', '#');
  159. userContent.innerHTML = user.first_name + ' ' + user.last_name;
  160. userContainer.appendChild(userContent);
  161. usersContainer.appendChild(userContainer);
  162. });
  163. };
  164. const fallbackUsers = function(error) {
  165. console.log('users error', error);
  166. };
  167. fetch(getUsersRequest)
  168. .then(responseToJson)
  169. .then(fillUsersContainer)
  170. .catch(fallbackUsers);
  171. // User registration Form
  172. registerForm.addEventListener('submit', function(e) {
  173. e.preventDefault();
  174. fetch(createUserRequest())
  175. .then(responseToJson)
  176. .then(loginRegisteredUser)
  177. .catch(createUserError);
  178. });
  179. // User login Form
  180. loginForm.addEventListener('submit', function(e) {
  181. e.preventDefault();
  182. fetch(loginUserRequest())
  183. .then(responseToJson)
  184. .then(loginUser)
  185. .catch(loginUserError);
  186. });
  187. });