Advertisement
Guest User

Untitled

a guest
Mar 27th, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function attachEvents() {
  2.  
  3.     let username = 'guest';
  4.     let password = 'g';
  5.     let base64encode = btoa(username + ':' + password);
  6.     let authorization = {"Authorization": "Basic " + base64encode};
  7.  
  8.     let countriesInDataBase = [];
  9.     loadData();
  10.  
  11.     $('#addBtn').on('click', addCountry);
  12.     $('#deleteBtn').on('click', deleteCountry);
  13.  
  14.     function addCountry() {
  15.         let name = $('#countryInput').val();
  16.  
  17.         if (name.length > 0 && !countriesInDataBase.includes(name)) {
  18.  
  19.             let jsonData = JSON.stringify({name: name});
  20.  
  21.             countriesInDataBase.push(name);
  22.  
  23.             $.ajax({
  24.                 method: 'POST',
  25.                 url: `https://baas.kinvey.com/appdata/kid_HJxYjhw9z/countries`,
  26.                 headers: authorization,
  27.                 data: jsonData,
  28.                 contentType: 'application/json'
  29.             }).then(appendCountry).catch(handleError);
  30.  
  31.             function appendCountry() {
  32.                 $('#countries').append($(`<option>${name}</option>`));
  33.                 $('#func').text(`${name} added!`).fadeIn(300).fadeOut(2000);
  34.             }
  35.         } else {
  36.             $('#func').text(`Invalid country!`).fadeIn(300).fadeOut(2000);
  37.         }
  38.         $('#countryInput').val('');
  39.     }
  40.  
  41.     function deleteCountry() {
  42.         let selected = $('#countries').find(':selected').val();
  43.         let idToDelete;
  44.         $.ajax({
  45.             method: 'GET',
  46.             url: `https://baas.kinvey.com/appdata/kid_HJxYjhw9z/countries`,
  47.             headers: authorization
  48.         }).then(findAndDeleteCountry).catch(handleError);
  49.  
  50.         function findAndDeleteCountry(res) {
  51.             for(let country of res){
  52.                 if(country.name = selected){
  53.                     idToDelete = country._id;
  54.                     break;
  55.                 }
  56.             }
  57.             $.ajax({
  58.                 method: 'DELETE',
  59.                 url: `https://baas.kinvey.com/appdata/kid_HJxYjhw9z/countries/${idToDelete}`,
  60.                 headers: authorization,
  61.                 contentType: "application/json",
  62.             }).then().catch(handleError)
  63.         }
  64.     }
  65.  
  66.     function loadData() {
  67.         $.ajax({
  68.             method: 'GET',
  69.             url: `https://baas.kinvey.com/appdata/kid_HJxYjhw9z/countries`,
  70.             headers: authorization
  71.         }).then(addCountries).catch(handleError);
  72.  
  73.         function addCountries(res) {
  74.             for (let country of res) {
  75.                 $('#countries').append($(`<option>${country.name}</option>`));
  76.                 countriesInDataBase.push(country.name);
  77.             }
  78.         }
  79.     }
  80.  
  81.     function handleError(err) {
  82.         console.log('Error: ' + err.status + " " + err.statusText)
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement