Guest User

Untitled

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