Advertisement
Mechoboy

Untitled

Jul 4th, 2016
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.74 KB | None | 0 0
  1. /**
  2. * Created by HP on 1.7.2016 г..
  3. */
  4. const kinveyBaseUrl = "https://baas.kinvey.com/"
  5. const kinveyAppKey = "kid_rk1vMRXU"
  6. const kinveyAppSecret = "7d6245f0cb614d1aa0735b775945afbf"
  7. function showView(viewName) {
  8. $('main > section').hide();
  9. $('#' + viewName).show();
  10. }
  11. function showHideMenuLinks() {
  12. $("#linkHome").show();
  13. if (sessionStorage.getItem('authToken')== null) {
  14. $("#linkLogin").show();
  15. $("#linkRegister").show();
  16. $("#linkListBooks").hide();
  17. $("#linkLogout").hide();
  18. $("#linkCreateBook").hide();
  19. } else {
  20. $("#linkLogin").hide();
  21. $("#linkRegister").hide();
  22. $("#linkListBooks").show();
  23. $("#linkCreateBook").show();
  24. $("#linkLogout").show();
  25. }
  26. }
  27. function showInfo(message) {
  28. $('#infoBox').text(message);
  29. $('#infoBox').show();
  30. setTimeout(function() { $('#infoBox').fadeOut()}, 3000)
  31. }
  32. function showError(errorMsg) {
  33. $('#errorBox').text("Error: " + errorMsg);
  34. $('#errorBox').show();
  35. }
  36. $(function() {
  37. showHideMenuLinks()
  38. showView('viewHome');
  39.  
  40. $("#linkHome").click(showHomeView);
  41. $("#linkLogin").click(showLoginView);
  42. $("#linkRegister").click(showRegisterView);
  43. $("#linkListBooks").click(listBooks);
  44. $("#linkCreateBook").click(showCreateBookView);
  45. $("#linkLogout").click(logout);
  46. });
  47. $("#formLogin").submit(function(e) {e.preventDefault(); login(); });
  48. $("#formRegister").submit(function(e) { e.preventDefault(); register(); });
  49. $("#formCreateBook").submit(function(e) {e.preventDefault(); createBook(); });
  50.  
  51. $(document).on({
  52. ajaxStart: function () { $("#loadingBox").show()},
  53. ajaxStop: function () { $("#loadingBox").hide() }
  54. });
  55. function showHomeView() {
  56. showView('viewHome');
  57. }
  58. function showLoginView() {
  59. showView('viewLogin');
  60. }
  61. function login() {
  62. const kinveyLoginUrl = kinveyBaseUrl + "user/" + kinveyAppKey + "/login";
  63. const kinveyAuthHeaders = {
  64. 'Authorization': "Basic " + btoa(kinveyAppKey + ":" + kinveyAppSecret),
  65. };
  66. let userData = {
  67. username: $('#loginUser').val(),
  68. password: $('#loginPass').val()
  69. };
  70. $.ajax({
  71. method: "POST",
  72. url: kinveyLoginUrl,
  73. headers: kinveyAuthHeaders,
  74. data: userData,
  75. success: loginSuccess,
  76. error: handleAjaxError
  77. });
  78. function loginSuccess(response) {
  79. let userAuth = response._kmd.authtoken;
  80. sessionStorage.setItem('authToken', userAuth);
  81. showHideMenuLinks();
  82. listBooks();
  83. showInfo('Login successful.');
  84. }
  85. }
  86. function handleAjaxError(response) {
  87. let errorMsg = JSON.stringify(response);
  88. if (response.readyState === 0)
  89. errorMsg = "Cannot connect due to network error.";
  90. if (response.responseJSON && response.responseJSON.description)
  91. errorMsg = response.responseJSON.description;
  92. showError(errorMsg)
  93. }
  94. function showRegisterView () {
  95. showView('viewRegister')
  96. }
  97. function register() {
  98. const kinveyRegisterUrl = kinveyBaseUrl + "user/" + kinveyAppKey + "/";
  99. const kinveyAuthHeaders = {
  100. 'Authorization': "Basic " + btoa(kinveyAppKey + ":" + kinveyAppSecret),
  101. };
  102. let userData = {
  103. username: $('#registerUser').val(),
  104. password: $('#registerPass').val()
  105. };
  106. $.ajax({
  107. method: "POST",
  108. url: kinveyRegisterUrl,
  109. headers: kinveyAuthHeaders,
  110. data: userData,
  111. success: registerSuccess,
  112. error: handleAjaxError
  113. });
  114. function registerSuccess(response) {
  115. let userAuth = response._kmd.authtoken;
  116. sessionStorage.setItem('authToken', userAuth);
  117. showHideMenuLinks();
  118. listBooks();
  119. showInfo('User registration successful.');
  120. }
  121. }
  122. function listBooks() {
  123. $('#books').empty();
  124. showView('viewBooks');
  125. const kinveyBooksUrl = kinveyBaseUrl + "appdata/" + kinveyAppKey + "/books";
  126. const kinveyAuthHeaders = {
  127. 'Authorization': "Kinvey " + sessionStorage.getItem('authToken'),
  128. };
  129. $.ajax({
  130. method: "GET",
  131. url: kinveyBooksUrl,
  132. headers: kinveyAuthHeaders,
  133. success: loadBooksSuccess,
  134. error: handleAjaxError
  135. });
  136. }
  137. function loadBooksSuccess(books) {
  138. showInfo('Books loaded.');
  139. if (books.length == 0) {
  140. $('#books').text('No books in the library.');
  141. } else {
  142. let booksTable = $('<table>')
  143. .append($('<tr>').append(
  144. '<th>Title</th>',
  145. '<th>Author</th>',
  146. '<th>Description</th>')
  147. );
  148. for (let book of books) {
  149. booksTable.append($('<tr>').append(
  150. $('<td>').text(book.title),
  151. $('<td>').text(book.author),
  152. $('<td>').text(book.description))
  153. );
  154. }
  155. $('#books').append(booksTable);
  156. }
  157. }
  158. function showCreateBookView() {
  159. showView('viewCreateBook');
  160. }
  161. function createBook() {
  162. const kinveyBooksUrl = kinveyBaseUrl + "appdata/" + kinveyAppKey + "/books";
  163. const kinveyAuthHeaders = {
  164. 'Authorization': "Kinvey " + sessionStorage.getItem('authToken'),
  165. };
  166. let bookData = {
  167. title: $('#bookTitle').val(),
  168. author: $('#bookAuthor').val(),
  169. description: $('#bookDescription').val()
  170. };
  171. $.ajax({
  172. method: "POST",
  173. url: kinveyBooksUrl,
  174. headers: kinveyAuthHeaders,
  175. data: bookData,
  176. success: createBookSuccess,
  177. error: handleAjaxError
  178. });
  179. function createBookSuccess(response) {
  180. listBooks();
  181. showInfo('Book created.');
  182. }
  183. }
  184. function logout() {
  185. sessionStorage.clear();
  186. showHideMenuLinks();
  187. showView('viewHome');
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement