Advertisement
Guest User

Fisher Game

a guest
Apr 3rd, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. function attachEvents() {
  2. const baseUrl = "https://baas.kinvey.com"
  3. const appKey = "kid_HkkAw4MtV";
  4. const endPoint = "fishCatches";
  5. const username = "vlad";
  6. const password = "12345";
  7. const headers = {
  8. "Authorization": `Basic ${btoa(username + ':' + password)}`,
  9. "Content-Type": "application/json"
  10. };
  11.  
  12.  
  13. $(`.load`).on("click", loadCatches);
  14. $(`.add`).on("click", addCatch);
  15.  
  16. async function loadCatches() {
  17. $(`#catches`).empty();
  18. let catches = await $.ajax({
  19. method: "GET",
  20. url: baseUrl + "/appdata/" + appKey + "/" + endPoint,
  21. headers,
  22. }) ;
  23. for(let value of catches) {
  24. let id = value._id;
  25. let angler = value.angler;
  26. let weight = value.weight;
  27. let bait = value.bait;
  28. let captureTime = value.captureTime;
  29. let location = value.location;
  30. let species = value.species;
  31.  
  32. let $div = $(` <div class="catch" data-id="${id}">
  33. <label>Angler</label>
  34. <input type="text" class="angler" value="${angler}"/>
  35. <label>Weight</label>
  36. <input type="number" class="weight" value="${weight}"/>
  37. <label>Species</label>
  38. <input type="text" class="species" value="${species}"/>
  39. <label>Location</label>
  40. <input type="text" class="location" value="${location}"/>
  41. <label>Bait</label>
  42. <input type="text" class="bait" value="${bait}"/>
  43. <label>Capture Time</label>
  44. <input type="number" class="captureTime" value="${captureTime}"/>
  45. </div>`);
  46. let updateButton = $(`<button class="update">Update</button>`);
  47. updateButton.on("click", updateCatch);
  48. let deleteButton = $(`<button class="delete">Delete</button>`);
  49. deleteButton.on("click", deleteCatch);
  50. $div.append(updateButton);
  51. $div.append(deleteButton);
  52. $(`#catches`).append($div);
  53. }
  54.  
  55.  
  56.  
  57. }
  58.  
  59. async function addCatch() {
  60. let angler = $(".angler").val();
  61. let weight = $(".weight").val();
  62. let bait = $(".bait").val();
  63. let captureTime = $(".captureTime").val();
  64. let location = $(".location").val();
  65. let species = $(".species").val();
  66.  
  67. let currentCatch = {
  68. angler,
  69. weight,
  70. species,
  71. location,
  72. bait,
  73. captureTime
  74. };
  75.  
  76. await $.ajax({
  77. method: "POST",
  78. url: baseUrl + `/appdata/${appKey}/${endPoint}`,
  79. headers,
  80. data: JSON.stringify(currentCatch)
  81. });
  82. loadCatches();
  83. }
  84.  
  85. async function updateCatch() {
  86. let catchId = $(this).parent().data("id");
  87. let angler = $(this).parent().find(`input.angler`).val();
  88. let weight = $(this).parent().find(`input.weight`).val();
  89. let bait = $(this).parent().find(`input.bait`).val();
  90. let captureTime = $(this).parent().find(`input.captureTime`).val();
  91. let location = $(this).parent().find(`input.location`).val();
  92. let species = $(this).parent().find(`input.species`).val();
  93.  
  94. let catchUpdate = {
  95. angler,
  96. weight,
  97. bait,
  98. captureTime,
  99. location,
  100. species
  101. };
  102.  
  103. await $.ajax({
  104. method: "PUT",
  105. url: baseUrl + "/appdata/" + appKey + "/" + endPoint + "/" + catchId,
  106. headers,
  107. data: JSON.stringify(catchUpdate)
  108. });
  109. //console.log("Updated");
  110. loadCatches();
  111. }
  112.  
  113. async function deleteCatch() {
  114. let catchId = $(this).parent().data(`id`);
  115. await $.ajax({
  116. method: "DELETE",
  117. url: baseUrl + "/appdata/" + appKey + "/" + endPoint + "/" + catchId,
  118. headers,
  119. });
  120. $(this).parent().remove();
  121.  
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement