Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const kinveyAppId = 'kid_Hy99tVbU';
- const kinveyAppSecret = '2c47ba042eaf469f904645482e4feb96';
- const kinveyBaseUrl = 'https://baas.kinvey.com/';
- function showView(viewId) {
- $("main > section").hide();
- $("#" + viewId).show();
- }
- function showHideNavigationLinks() {
- let loggedIn = sessionStorage.getItem('authToken');
- if (loggedIn != null) {
- $("#linkLogin").hide();
- $("#linkRegister").hide();
- $("#linkListBooks").show();
- $("#linkAddBook").show();
- $("#linkLogout").show();
- } else {
- $("#linkLogin").show();
- $("#linkRegister").show();
- $("#linkListBooks").hide();
- $("#linkAddBook").hide();
- $("#linkLogout").hide();
- }
- }
- function showHomeView() {
- showView('viewHome');
- }
- function showLoginView() {
- showView('viewLogin');
- }
- function login() {
- let authBase64 = btoa(kinveyAppId + ":" + kinveyAppSecret);
- let loginUrl = kinveyBaseUrl + "user/" + kinveyAppId + "/login";
- let loginData = {
- username: $("#loginUser").val(),
- password: $("#loginPass").val()
- };
- $.ajax({
- method: "POST",
- url: loginUrl,
- data: loginData,
- headers: {"Authorization": "Basic " + authBase64},
- success: loginSuccess,
- error: handleAjaxError
- });
- function loginSuccess(response) {
- let userAuth = response._kmd.authtoken;
- sessionStorage.setItem('authToken', userAuth);
- showView('viewHome');
- showHideNavigationLinks();
- showInfo("Login successful");
- }
- }
- function showInfo(messageText) {
- $("#infoBox").text(messageText).show();
- setTimeout(function() {$("#infoBox").fadeOut()}, 3000);
- }
- function showError(errorMsg) {
- $("#errorBox").text("Error: " + errorMsg).show();
- }
- function handleAjaxError(response) {
- let errorMsg = JSON.stringify(response);
- if (response.readyState === 0)
- errorMsg = "Cannot connect due to network error";
- if (response.responseJSON && response.responseJSON.description)
- errorMsg = response.responseJSON.description;
- showError(errorMsg);
- }
- function showRegisterView() {
- showView('viewRegister');
- }
- function register() {
- let authBase64 = btoa(kinveyAppId + ":" + kinveyAppSecret);
- let registerUrl = kinveyBaseUrl + "user/" + kinveyAppId + "/";
- let registerData = {
- username: $("#registerUser").val(),
- password: $("#registerPass").val()
- };
- $.ajax({
- method: "POST",
- url: registerUrl,
- data: registerData,
- headers: {"Authorization": "Basic " + authBase64},
- success: registerSuccess,
- error: handleAjaxError
- });
- function registerSuccess(response) {
- let userAuth = response._kmd.authtoken;
- sessionStorage.setItem('authToken', userAuth);
- showHomeView();
- showHideNavigationLinks();
- showInfo("Registration successful");
- }
- }
- function showListBooksView() {
- $("#books").empty();
- showView('viewListBooks');
- let booksUrl = kinveyBaseUrl + "appdata/" + kinveyAppId + "/books";
- $.ajax({
- method: "GET",
- url: booksUrl,
- headers: {"Authorization": "Kinvey " + sessionStorage.getItem('authToken')},
- success: listBooksSuccess,
- error: handleAjaxError
- });
- function listBooksSuccess(books) {
- showInfo("Books loaded");
- if (books.length == 0) {
- $("#books").text("No books in the library.");
- } else {
- let booksTable = $('<table>')
- .append($('<tr>').append(
- '<th>Title</th>',
- '<th>Author</th>',
- '<th>Description</th>')
- );
- for (let book of books) {
- booksTable.append($('<tr>').append(
- $('<td>').text(book.title),
- $('<td>').text(book.author),
- $('<td>').text(book.description))
- );
- if (book.comments == null) {
- booksTable.append($('<tr>').append($('<td class="noReview" colspan="3">').text("No reader reviews.")));
- } else {
- booksTable.append($('<tr>').append($('<td class="review" colspan="3">')));
- for (let comment of book.comments) {
- booksTable.find('td.review').append(
- $('<div>').text(comment.commentText),
- $('<div>').text(comment.author)
- );
- }
- }
- }
- $("#books").append(booksTable);
- }
- }
- }
- function showAddBookView() {
- showView('viewAddBook');
- $("#bookTitle").val('');
- $("#bookAuthor").val('');
- $("#bookDescription").val('');
- }
- function addBook() {
- $("#books").empty();
- let booksUrl = kinveyBaseUrl + "appdata/" + kinveyAppId + "/books";
- let bookData = {
- title: $("#bookTitle").val(),
- author: $("#bookAuthor").val(),
- description: $("#bookDescription").val()
- //comments: [{author: "Zorro", commentText: "ZZZ"},
- //{author: "Todd", commentText: "Boooo!"},
- //{author: "Bob", commentText: "Oops!"},
- //{author: "Sean", commentText: "Shingles"}]
- };
- $.ajax({
- method: "POST",
- url: booksUrl,
- headers: {"Authorization": "Kinvey " + sessionStorage.getItem('authToken')},
- data: bookData,
- success: addBookSuccess,
- error: handleAjaxError
- });
- function addBookSuccess() {
- showListBooksView();
- showInfo("Book successfully added");
- }
- }
- function logout() {
- sessionStorage.clear();
- showView('viewHome');
- showHideNavigationLinks();
- }
- $(function() {
- showHideNavigationLinks();
- showView('viewHome');
- $("#linkHome").click(showHomeView);
- $("#linkLogin").click(showLoginView);
- $("#linkRegister").click(showRegisterView);
- $("#linkListBooks").click(showListBooksView);
- $("#linkAddBook").click(showAddBookView);
- $("#linkLogout").click(logout);
- $("#formLogin").submit(function(e) {e.preventDefault(); login()});
- $("#formRegister").submit(function(e) {e.preventDefault(); register()});
- $("#formAddBook").submit(function(e) {e.preventDefault(); addBook()});
- $(document).on({
- ajaxStart: function() {$("#loadingBox").show()},
- ajaxStop: function() {$("#loadingBox").hide()}
- })
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement