vaakata

BooksLibrary_4.07.16

Jul 4th, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const kinveyAppID = 'kid_BkSPwnsH';
  2. const kinveyAppSecret = '1292427a4d324036a96b3c93256da065';
  3. const kinveyServiceUrl = '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.authToken != null;
  12.     if (loggedIn) {
  13.         $("#linkLogin").hide();
  14.         $("#linkRegister").hide();
  15.         $("#linkListBooks").show();
  16.         $("#linkCreateBook").show();
  17.         $("#linkLogout").show();
  18.     } else {
  19.         $("#linkLogin").show();
  20.         $("#linkRegister").show();
  21.         $("#linkListBooks").hide();
  22.         $("#linkCreateBook").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 = kinveyServiceUrl + "user/" + kinveyAppID + "/login";
  38.     let loginData = {
  39.         username: $("#loginUser").val(),
  40.         password: $("#loginPass").val()
  41.     };
  42.  
  43.     $.ajax({
  44.         method: "POST",
  45.         url: loginUrl,
  46.         data: loginData,
  47.         headers: { "Authorization": "Basic " + authBase64 },
  48.         success: loginSuccess,
  49.         error: showAjaxError
  50.     });
  51.  
  52.     function loginSuccess(data, status) {
  53.         sessionStorage.authToken = data._kmd.authtoken;
  54.         showListBooksView();
  55.         showHideNavigationLinks();
  56.         showInfo("Login successful");
  57.     }
  58. }
  59.  
  60. function showInfo(messageText) {
  61. $("#infoBox").text(messageText).show().delay(3000).fadeOut() ;
  62. }
  63.  
  64. function showAjaxError(data, status) {
  65.     let errorMsg = "Error:" + JSON.stringify(data);
  66.     $('#errorBox').text(errorMsg).show();
  67. }
  68.    
  69. function showRegisterView() {
  70.     showView('viewRegister');
  71. }
  72.  
  73. function register() {
  74.     let authBase64 = btoa(kinveyAppID + ":" + kinveyAppSecret);
  75.     let loginUrl = kinveyServiceUrl + "user/" + kinveyAppID + "/";
  76.     let loginData = {
  77.         username: $("#registerUser").val(),
  78.         password: $("#registerPass").val(),
  79.     };
  80.  
  81.     $.ajax ({
  82.         method:"POST",
  83.         url: loginUrl,
  84.         data: loginData,
  85.         headers: { "Authorization": "Basic " + authBase64 },
  86.         success: registerSuccess,
  87.         error: showAjaxError
  88.     });
  89.  
  90.     function registerSuccess(data, status) {
  91.         sessionStorage.authToken = data._kmd.authtoken;
  92.         showListBooksView();
  93.         showHideNavigationLinks();
  94.         showInfo("User registered successfullly");
  95.     }
  96.  
  97. }
  98.  
  99. function showListBooksView() {
  100.     showView('viewListBooks');
  101.     $("#books").text('');
  102.     let booksUrl = kinveyServiceUrl + "appdata/" + kinveyAppID + "/books-library";
  103.     let authHeaders = { "Authorization": "Kinvey " + sessionStorage.authToken
  104.     };
  105.     $.ajax({
  106.         method: "GET",
  107.         url: booksUrl,
  108.         headers: authHeaders,
  109.         success: booksLoaded,
  110.         error: showAjaxError,
  111.     });
  112.  
  113.     function booksLoaded(books, status) {
  114.         showInfo("Books loaded.");
  115.  
  116.         let booksTable = $("<table>")
  117.             .append($("<tr>")
  118.                 .append($('<th>Title</th>'))
  119.                 .append($('<th>Author</th>'))
  120.                 .append($('<th>Description</th>'))
  121.             );
  122.  
  123.         for (let book of books){
  124.             booksTable.append($("<tr>")
  125.                     .append($('<td></td>').text(book.title))
  126.                     .append($('<td></td>').text(book.author))
  127.                     .append($('<td></td>').text(book.description))
  128.                 );
  129.         }
  130.        $("#books").append(booksTable);//video time point: 2:12:22
  131.     }
  132. }
  133.  
  134. function showCreateBookView() {
  135.     showView('viewCreateBook');
  136. }
  137.  
  138. function createBook() {
  139.     $("#createBook").text('');
  140.     let booksUrl = kinveyServiceUrl + "appdata/" + kinveyAppID + "/books-library";
  141.     let authHeaders = {
  142.         "Authorization": "Kinvey " + sessionStorage.authToken
  143.     };
  144.     let newBookData = {
  145.         title: $("#bookTitle").val(),
  146.         author: $("#bookAuthor").val(),
  147.         description: $("#bookDescription").val(),
  148.     };
  149.  
  150.     $.ajax({
  151.         method: "POST",
  152.         url: booksUrl,
  153.         data: newBookData,
  154.         headers: authHeaders,
  155.         success: bookCreated,
  156.         error: showAjaxError
  157.     });
  158.  
  159.     function bookCreated(data) {
  160.         showListBooksView();
  161.         showInfo("Books created.");
  162.     }
  163. }
  164.  
  165. function logout() {
  166.     sessionStorage.clear();
  167.     showHideNavigationLinks();
  168. }
  169.  
  170. function showInfo(messageText) {
  171.     $("#infoBox").text(messageText).show().delay(3000).fadeOut() ;
  172. }
  173.  
  174. $(function () {
  175.     $("#linkHome").click(showHomeView);
  176.     $("#linkLogin").click(showLoginView);
  177.     $("#linkRegister").click(showRegisterView);
  178.     $("#linkListBooks").click(showListBooksView);
  179.     $("#linkCreateBook").click(showCreateBookView);
  180.     $("#linkLogout").click(logout);
  181.  
  182.     $("#formLogin").submit(function(e) {e.preventDefault(); login()});
  183.     $("#formRegister").submit(function(e) {e.preventDefault(); register()});
  184.     $("#formCreateBook").submit(function(e) {e.preventDefault(); createBook()});
  185.  
  186.     showHomeView();
  187.     showHideNavigationLinks();
  188.  
  189.     $(document)
  190.         .ajaxStart(function() {
  191.             $("#loadingBox").show();
  192.         })
  193.         .ajaxStop(function() {
  194.             $("#loadingBox").hide();
  195.         })
  196. });
Add Comment
Please, Sign In to add comment