Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function showVew(vewID) {
- $("main > section").hide();
- $('#' + vewID).show();
- }
- function showHideNavLinks() {
- let loggedIn = sessionStorage.authToken != null;
- if(loggedIn){
- $("#linkLogin").hide();
- $("#linkRegister").hide();
- $("#linkListBooks").show();/*TODO List of books*/
- $("#linkCreateBook").show();
- $("#linkLogout").show();
- }
- else {
- $("#linkLogin").show();
- $("#linkRegister").show();
- $("#linkListBooks").hide();
- $("#linkCreateBook").hide();
- $("#linkLogout").hide();
- }
- }
- function showHomeVew() {
- showVew('vewHome')
- }
- function showLoginVew() {
- showVew('vewLogin')
- }
- function showRegisterVew() {
- showVew('vewRegister')
- }
- function showListBooksVew() {
- showVew('vewListBooks')
- }
- function showCreateBookVew() {
- showVew('vewCreatedBooks')
- }
- function logout() {
- alert('logout');
- showVew('vewHome')
- }
- $(function () {
- $("#linkHome").click(showHomeVew);
- $("#linkLogin").click(showLoginVew);
- $("#linkRegister").click(showRegisterVew);
- $("#linkListBooks").click(showListBooksVew);
- $("#linkCreateBook").click(showCreateBookVew);
- $("#linkLogout").click(logout);
- $("#loginButton").click(login);
- $("#registerButton").click(register);
- $("#createBookButton").click(createBook);
- showHomeVew();
- showHideNavLinks();
- });
- /*
- $("#formLogin").submit(function (e) {
- e.preventDefault(); login();
- });
- $("#formRegister").submit(function (e) { /!*!!!!!!!!!!*!/
- e.preventDefault(); register();
- });
- $("#formCreateBook").submit(function (e) {
- e.preventDefault(); createBook();
- });*/
- const kinveyBaseUrl = "https://baas.kinvey.com/";
- const kinveyAppKey = "kid_SJOSvBYB";
- const kinveyAppSecret = "0274d97941d7422e8954ba4fb3fefea0";
- function showView(viewName) {
- $('main > section').hide();
- $('#' + viewName).show();
- }
- function showInfo(message) {
- $('#infoBox').text(message);
- $('#infoBox').show();
- setTimeout(function () {
- $('#infobox').fadeOut()}, 3000)
- }
- function showError(errorMsg) {
- $('#errorBox').text("Error: " + errorMsg);
- $('#errorBox').show();
- }
- $(function () {
- showHideNavLinks();
- showVew('viewHome');
- })
- $("#linkHome").click(showHomeVew());
- $("#linkLogin").click(showLoginVew());
- $("#linkRegister").click(showRegisterVew());
- $("#linkListBooks").click(showListBooksVew());
- $("#linkCreateBook").click(showCreateBookVew());
- $("#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 login() {
- const kinveyLoginUrl = kinveyBaseUrl + "user/" + kinveyAppKey + "/login";
- const kinveyAuthHeaders = {
- 'Authorization' : "Basic " + btoa(kinveyAppKey + ":" + kinveyAppSecret)
- };
- let userData = {
- username: $('#loginUser').val(),
- password: $('#userPass').val()
- };
- console.log("logna se");
- $.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);
- showHideNavLinks();
- showListBooksVew();
- 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);
- showHideNavLinks();
- showListBooksVew();
- showInfo('User registration successful.')
- }
- function listBooks() {
- $('#books').empty();
- showView('vewListBooks');
- const kinveyBooksUrl = kinveyBaseUrl + "appdata/" + kinveyAppKey + "/books";
- const kinveyAuthHeaders = {'Authorization' : "Kinvey " + sessionStorage.getItem('authToken')};
- $.ajax({
- method: "GET",
- url: kinveyBooksUrl,
- headers: kinveyAuthHeaders,
- success: loadBooksSuccess,
- error: handleAjaxError
- })
- }
- function loadBooksSuccess(books) {
- showInfo('Book 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))
- );
- }
- $('#books').append(booksTable);
- }
- }
- function showCreateBookView() {
- showView('viewCreateBook');
- }
- function createBook() {
- console.log("vlezna v function-a");
- const kinveyBooksUrl = kinveyBaseUrl + "appdata/" + kinveyAppKey + "/books";
- const kinveyAuthHeaders = {'Authorization' : "Kinvey " + 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(response) {
- listBooks();
- showInfo('Book created.');
- }
- console.log("suzdade kniga");
- }
- function logout() {
- sessionStorage.clear();
- showHideNavLinks();
- showVew('viewHome');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement