Guest User

Javascript

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