Advertisement
Guest User

Untitled

a guest
Jul 31st, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function attachEvents(){
  2.     const baseUrl = `https://baas.kinvey.com/appdata/kid_ryDho3sUb`;
  3.     const kinveyUsername = 'peshata';
  4.     const kinveyPassword = 'softuni';
  5.     const authHeader = { "Authorization": `Basic ${btoa(kinveyUsername + ':' + kinveyPassword)}` };
  6.  
  7.     let editCountryDiv = $('#editCountryDiv');
  8.     let addNewTownRow =  $('#addNewTownForm');
  9.     let countryTowns = $('#countryTowns');
  10.     let results = $('#results');
  11.     loadCountriesTable();
  12.  
  13.     function loadCountriesTable(){
  14.         editCountryDiv.hide();
  15.         results.empty();
  16.         $.ajax({
  17.             method: "GET",
  18.             url: baseUrl + '/Countries',
  19.             headers: authHeader
  20.         }).then(fillCountriesTable);
  21.     }
  22.  
  23.     function fillCountriesTable(data){
  24.         results.append($('<tr>').append($('<th>').text('Name')).append($('<th>').text('Action')));
  25.         for (let country of data){
  26.             $('#results').append($('<tr>')
  27.                 .append($('<td>').text(country.name).click(() => showCountryTowns(country._id)))
  28.                 .append($('<td>').append($('<button>')
  29.                     .text('Delete')
  30.                     .click(() => deleteCountry(country._id)))
  31.                         .append(($('<button>').text('Edit').click(() => editCountry(country._id)))
  32.                 )));
  33.         }
  34.  
  35.         function deleteCountry(id){
  36.             $.ajax({
  37.                 method: "DELETE",
  38.                 url: baseUrl + `/Countries/${id}`,
  39.                 headers: authHeader
  40.             })
  41.                 .then(loadCountriesTable)
  42.         }
  43.  
  44.         function editCountry(id) {
  45.             $.get({
  46.                 url: baseUrl + `/Countries/${id}`,
  47.                 headers: authHeader
  48.             })
  49.                 .then(populateEditCountryInput);
  50.  
  51.             function populateEditCountryInput(country) {
  52.                 editCountryDiv.show();
  53.                 $('#editCountryName').val(country.name);
  54.                 $('#countryToEditId').val(country._id);
  55.             }
  56.         }
  57.  
  58.         function showCountryTowns(id) {
  59.             let townsFromCountryUrl = `${baseUrl}/Towns/?query={"countryId":"${id}"}`;
  60.             $.get({
  61.                 url: townsFromCountryUrl,
  62.                 headers: authHeader
  63.             }).then((data) => {
  64.                 countryTowns.empty();
  65.                 countryTowns.append($('<tr id="addNewTownForm">').append($('<td colspan="2"></td>')
  66.                     .append($('<label>').text('Add new town: '))
  67.                     .append($('<input type="text" id="addTownName">'))
  68.                     .append($('<input type="hidden" id="countryToAddTownTo">').val(id))
  69.                     .append($('<button>').text('Add Town').click(addNewTownToSelectedCountry))));
  70.                 if (data.length > 0){
  71.                     for (let town of data){
  72.                         let actionButtons = $('<td>');
  73.                         actionButtons.append($('<button>').text('Delete').click(() => deleteTown(town._id, town.countryId)));
  74.                         actionButtons.append(' ');
  75.                         actionButtons.append($('<button>').text('Edit').click(editTown));
  76.                         countryTowns
  77.                             .append($('<tr>')
  78.                                 .append($('<td>').text(town.name))
  79.                                 .append(actionButtons));
  80.                     }
  81.                 }
  82.                 else {
  83.                     countryTowns.append($('<li>').text('No towns for current country'))
  84.                 }
  85.             });
  86.  
  87.             function deleteTown(townId, countryId){
  88.                 $.ajax({
  89.                     method: "DELETE",
  90.                     url: baseUrl + `/Towns/${townId}`,
  91.                     headers: authHeader
  92.                 })
  93.                     .then(() => {
  94.                         showCountryTowns(countryId)
  95.                     })
  96.             }
  97.  
  98.             function addNewTownToSelectedCountry() {
  99.                 let countryForNewTown = $('#countryToAddTownTo').val();
  100.                 let newTownName = $('#addTownName').val();
  101.  
  102.                 if (newTownName !== ''){
  103.                     $.post({
  104.                         url: baseUrl + '/Towns',
  105.                         data: JSON.stringify({ name: newTownName, countryId: countryForNewTown }),
  106.                         contentType: 'application/json',
  107.                         headers: authHeader
  108.                     })
  109.                         .then(showCountryTowns(countryForNewTown));
  110.                 }
  111.             }
  112.  
  113.             function editTown(){
  114.  
  115.             }
  116.         }
  117.     }
  118.  
  119.     $('#btnSubmit').click(createCountry);
  120.  
  121.     function createCountry(){
  122.         let name = $('#name').val();
  123.         if (name !== ''){
  124.             $.post({
  125.                 url: baseUrl + '/Countries',
  126.                 data: JSON.stringify({ name: name }),
  127.                 contentType: 'application/json',
  128.                 headers: authHeader
  129.             })
  130.                 .then(() => {
  131.                 $('#name').val('');
  132.                 loadCountriesTable();
  133.                 });
  134.         }
  135.     }
  136.  
  137.     $('#btnEditCountry').click(changeCountryName);
  138.  
  139.     function changeCountryName() {
  140.         let countryToEditId = $('#countryToEditId').val();
  141.         let newCountryName = $('#editCountryName').val();
  142.         let putUrl = baseUrl + `/Countries/${countryToEditId}`;
  143.         if (newCountryName !== '') {
  144.             $.ajax({
  145.                 url: putUrl,
  146.                 method: "PUT",
  147.                 contentType: 'application/json',
  148.                 data: JSON.stringify({ name: newCountryName}),
  149.                 headers: authHeader
  150.             })
  151.                 .then(loadCountriesTable)
  152.         }
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement