Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const KinveyBaseUrl = "https://baas.kinvey.com/";
- const KinveyAppKey = "kid_S1Jz5itH";
- const KinveyAppSecret = "66335d68729e441fbf3f3d0cc1dc0fea";
- function showView(viewName) {
- $('main > section').hide();
- $('#' = viewName).show();
- }
- function showHideMenuLinks() {
- $("#linkHome").show();
- if(sessionStorage.getItem('authToken') == null) {
- $('#linkLogin').show();
- $('#linkRegister').show();
- $('#linkListBooks').hide();
- $('#linkCreateBooks').hide();
- $('#linkLogout').hide();
- } else {
- $('#linkLogin').hide();
- $('#linkRegister').hide();
- $('#linkListBooks').show();
- $('#linkCreateBooks').show();
- $('#linkLogout').show();
- }
- }
- function showInfo(message) {
- $('#infoBox').text(message);
- $('#infoBox').show();
- setTimeout(function () { $('#infoBox').fadeOut() }, 3000);
- }
- function showError(errorMsg) {
- $('#errorBox').text("Error:" + errorMsg);
- $('#errorBox').show();
- }
- $(function () {
- showHideMenuLinks();
- showView('viewHome');
- $('#linkHome').click(showHomeView);
- $('#linkLogin').click(showLoginView);
- $('#linkRegister').click(showRegisterView);
- $('#linkListBooks').click(listBooks);
- $('#linkCreateBooks').click(showCreateBooks);
- $('#linkLogout').click(logout);
- $('$formLogin').submit(function (e) {
- e.preventDefault(); login();
- });
- $('formRegister').submit(function (e) {
- e.preventDefault(); register();
- });
- $('formCreateBook').submit(function (e) {
- e.preventDefault(); createBook();
- });
- $(document).on({
- ajaxStart: function() { $('#loadingBox').show()},
- ajaxStop: function() { $('#loadingBox').hide()}
- });
- });
- function showHomeView () {
- showView('viewHome');
- }
- function showLoginView() {
- showView('viewLogin');
- }
- function login() {
- const kinveyLoginUrl = KinveyBaseUrl + "user/" + KinveyAppKey + "/login";
- const kinveyAuthHeaders = {
- 'Authorization': "Basic" + btoa(KinveyAppKey + ":" + KinveyAppSecret)
- };
- let userData = {
- username: $("#loginUser").val(),
- password: $('#loginPassword').val()
- };
- $.ajax ({
- method: "POST",
- url: kinveyLoginUrl,
- headers: kinveyAuthHeaders,
- data: userData,
- success: loginSuccess,
- error: handleAjaxError
- });
- function loginSuccess (response) {
- let userAuth = response._kmd.authtoken;
- sessionStorage.setItem('authToken',userAuth);
- showHideMenuLinks();
- listBooks();
- showInfo('Login successful.');
- }
- }
- 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() {
- const kinveyRegisterUrl = KinveyBaseUrl + "user/" + KinveyAppKey + "/";
- const kinveyAuthHeaders = {
- 'Authorization': "Basic" + btoa(KinveyAppKey + ":" + KinveyAppSecret)
- };
- let userData = {
- username: $('#registerUser').val(),
- password: $('#registerPass').val()
- };
- $.ajax({
- method: "POST",
- url: kinveyRegisterUrl,
- headers: kinveyAuthHeaders,
- data: userData,
- success: registerSuccess,
- error: handleAjaxError
- });
- function registerSuccess(response) {
- let userAuth = response._kmd.authtoken;
- sessionStorage.setItem('authToken',userAuth);
- showHideMenuLinks();
- listBooks();
- showInfo('User registration successful.');
- }
- }
- function listBooks() {
- $('#books').empty();
- showView('viewBooks');
- const kinveyBooksUrl = KinveyBaseUrl + "/appdata" + KinveyAppKey + "/books";
- const kinveyAuthHeaders = {
- 'Authorization': "Basic" + sessionStorage.getItem('authToken'),
- };
- $.ajax({
- method: "GET",
- URL: kinveyBooksUrl,
- headers: kinveyAuthHeaders,
- success: loadBooksSuccess,
- error: handleAjaxError
- });
- function loadBooksSuccess() {
- 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 in books) {
- booksTable.append($('<tr>').append(
- $('<td>').text(book.title),
- $('<td>').text(book.author),
- $('<td>').text(book.description))
- );
- }
- $('#books').append(booksTable);
- }
- }
- }
- function showCreateBookView () {
- showView('viewCreateBook');
- }
- function createBook() {
- const kinveyBooksUrl = KinveyBaseUrl + "/appdata" + KinveyAppKey + "/books";
- const kinveyAuthHeaders = {
- 'Authorization': "Basic" + sessionStorage.getItem('authToken'),
- };
- let bookData = {
- title: $('#bookTitle').val(),
- author: $('#bookAuthor').val(),
- description: $('#bookDescription').val()
- };
- $.ajax({
- method: "POST",
- url: kinveyBooksUrl,
- headers: kinveyAuthHeaders,
- data: bookData,
- success: createBookSuccess,
- error: handleAjaxError
- });
- function createBookSuccess() {
- listBooks();
- showInfo('Nook created.')
- ; }
- }
- function logout() {
- sessionStorage.clear();
- showHideMenuLinks();
- showView('viewHome');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement