Guest User

Untitled

a guest
Jul 1st, 2016
111
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const kinveyAppId = 'kid_Hy99tVbU';
  2. const kinveyAppSecret = '2c47ba042eaf469f904645482e4feb96';
  3. const kinveyBaseUrl = 'https://baas.kinvey.com/';
  4.  
  5. function showView(viewId) {
  6. $("main > section").hide();
  7. $("#" + viewId).show();
  8. }
  9.  
  10. function showHideNavigationLinks() {
  11. let loggedIn = sessionStorage.getItem('authToken');
  12. if (loggedIn != null) {
  13. $("#linkLogin").hide();
  14. $("#linkRegister").hide();
  15. $("#linkListBooks").show();
  16. $("#linkAddBook").show();
  17. $("#linkLogout").show();
  18. } else {
  19. $("#linkLogin").show();
  20. $("#linkRegister").show();
  21. $("#linkListBooks").hide();
  22. $("#linkAddBook").hide();
  23. $("#linkLogout").hide();
  24. }
  25. }
  26.  
  27. function showHomeView() {
  28. showView('viewHome');
  29. }
  30.  
  31. function showLoginView() {
  32. showView('viewLogin');
  33. }
  34.  
  35. function login() {
  36. let authBase64 = btoa(kinveyAppId + ":" + kinveyAppSecret);
  37. let loginUrl = kinveyBaseUrl + "user/" + kinveyAppId + "/login";
  38. let loginData = {
  39. username: $("#loginUser").val(),
  40. password: $("#loginPass").val()
  41. };
  42. $.ajax({
  43. method: "POST",
  44. url: loginUrl,
  45. data: loginData,
  46. headers: {"Authorization": "Basic " + authBase64},
  47. success: loginSuccess,
  48. error: handleAjaxError
  49. });
  50.  
  51. function loginSuccess(response) {
  52. let userAuth = response._kmd.authtoken;
  53. sessionStorage.setItem('authToken', userAuth);
  54. showView('viewHome');
  55. showHideNavigationLinks();
  56. showInfo("Login successful");
  57. }
  58. }
  59.  
  60. function showInfo(messageText) {
  61. $("#infoBox").text(messageText).show();
  62. setTimeout(function() {$("#infoBox").fadeOut()}, 3000);
  63. }
  64.  
  65. function showError(errorMsg) {
  66. $("#errorBox").text("Error: " + errorMsg).show();
  67. }
  68.  
  69. function handleAjaxError(response) {
  70. let errorMsg = JSON.stringify(response);
  71. if (response.readyState === 0)
  72. errorMsg = "Cannot connect due to network error";
  73. if (response.responseJSON && response.responseJSON.description)
  74. errorMsg = response.responseJSON.description;
  75. showError(errorMsg);
  76. }
  77.  
  78. function showRegisterView() {
  79. showView('viewRegister');
  80. }
  81.  
  82. function register() {
  83. let authBase64 = btoa(kinveyAppId + ":" + kinveyAppSecret);
  84. let registerUrl = kinveyBaseUrl + "user/" + kinveyAppId + "/";
  85. let registerData = {
  86. username: $("#registerUser").val(),
  87. password: $("#registerPass").val()
  88. };
  89. $.ajax({
  90. method: "POST",
  91. url: registerUrl,
  92. data: registerData,
  93. headers: {"Authorization": "Basic " + authBase64},
  94. success: registerSuccess,
  95. error: handleAjaxError
  96. });
  97.  
  98. function registerSuccess(response) {
  99. let userAuth = response._kmd.authtoken;
  100. sessionStorage.setItem('authToken', userAuth);
  101. showHomeView();
  102. showHideNavigationLinks();
  103. showInfo("Registration successful");
  104. }
  105. }
  106.  
  107. function showListBooksView() {
  108. $("#books").empty();
  109. showView('viewListBooks');
  110.  
  111. let booksUrl = kinveyBaseUrl + "appdata/" + kinveyAppId + "/books";
  112. $.ajax({
  113. method: "GET",
  114. url: booksUrl,
  115. headers: {"Authorization": "Kinvey " + sessionStorage.getItem('authToken')},
  116. success: listBooksSuccess,
  117. error: handleAjaxError
  118. });
  119.  
  120. function listBooksSuccess(books) {
  121. showInfo("Books loaded");
  122. if (books.length == 0) {
  123. $("#books").text("No books in the library.");
  124. } else {
  125. let booksTable = $('<table>')
  126. .append($('<tr>').append(
  127. '<th>Title</th>',
  128. '<th>Author</th>',
  129. '<th>Description</th>')
  130. );
  131. for (let book of books) {
  132. booksTable.append($('<tr>').append(
  133. $('<td>').text(book.title),
  134. $('<td>').text(book.author),
  135. $('<td>').text(book.description))
  136. );
  137. if (book.comments == null) {
  138. booksTable.append($('<tr>').append($('<td class="noReview" colspan="3">').text("No reader reviews.")));
  139. } else {
  140. booksTable.append($('<tr>').append($('<td class="review" colspan="3">')));
  141. for (let comment of book.comments) {
  142. booksTable.find('td.review').append(
  143. $('<div>').text(comment.commentText),
  144. $('<div>').text(comment.author)
  145. );
  146. }
  147. }
  148. }
  149. $("#books").append(booksTable);
  150. }
  151. }
  152. }
  153.  
  154.  
  155. function showAddBookView() {
  156. showView('viewAddBook');
  157. $("#bookTitle").val('');
  158. $("#bookAuthor").val('');
  159. $("#bookDescription").val('');
  160. }
  161.  
  162. function addBook() {
  163. $("#books").empty();
  164. let booksUrl = kinveyBaseUrl + "appdata/" + kinveyAppId + "/books";
  165. let bookData = {
  166. title: $("#bookTitle").val(),
  167. author: $("#bookAuthor").val(),
  168. description: $("#bookDescription").val()
  169. //comments: [{author: "Zorro", commentText: "ZZZ"},
  170. //{author: "Todd", commentText: "Boooo!"},
  171. //{author: "Bob", commentText: "Oops!"},
  172. //{author: "Sean", commentText: "Shingles"}]
  173. };
  174. $.ajax({
  175. method: "POST",
  176. url: booksUrl,
  177. headers: {"Authorization": "Kinvey " + sessionStorage.getItem('authToken')},
  178. data: bookData,
  179. success: addBookSuccess,
  180. error: handleAjaxError
  181. });
  182.  
  183. function addBookSuccess() {
  184. showListBooksView();
  185. showInfo("Book successfully added");
  186. }
  187. }
  188.  
  189. function logout() {
  190. sessionStorage.clear();
  191. showView('viewHome');
  192. showHideNavigationLinks();
  193. }
  194.  
  195. $(function() {
  196. showHideNavigationLinks();
  197. showView('viewHome');
  198.  
  199. $("#linkHome").click(showHomeView);
  200. $("#linkLogin").click(showLoginView);
  201. $("#linkRegister").click(showRegisterView);
  202. $("#linkListBooks").click(showListBooksView);
  203. $("#linkAddBook").click(showAddBookView);
  204. $("#linkLogout").click(logout);
  205.  
  206. $("#formLogin").submit(function(e) {e.preventDefault(); login()});
  207. $("#formRegister").submit(function(e) {e.preventDefault(); register()});
  208. $("#formAddBook").submit(function(e) {e.preventDefault(); addBook()});
  209.  
  210. $(document).on({
  211. ajaxStart: function() {$("#loadingBox").show()},
  212. ajaxStop: function() {$("#loadingBox").hide()}
  213. })
  214. });
RAW Paste Data