Advertisement
Guest User

Untitled

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