Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. function attachEvents() {
  2. const LoadCreateURL = "https://fisher-game.firebaseio.com/catches.json"
  3. const UpdateDeleteURl = id => `https://fisher-game.firebaseio.com/catches/${id}.json`
  4. const catches = document.getElementById("catches")
  5.  
  6. const crudOperation = {
  7. load: loadCatchesAsync,
  8. add: addCatch,
  9. update: updateCatchAsync,
  10. delete: deleteCatchAsync
  11. }
  12.  
  13. window.addEventListener("click", (e) => {
  14. let operation = e.target.className
  15. if (operation === "load" ||
  16. operation === "add" ||
  17. operation === "update" ||
  18. operation === "delete") {
  19. crudOperation[operation].call(this, e)
  20. }
  21. })
  22.  
  23. async function loadCatchesAsync() {
  24. await fetch(LoadCreateURL)
  25. .then(x => x.json())
  26. .then(x => displayAllCatches(x))
  27. .catch(e => console.log(e))
  28. }
  29.  
  30. function displayAllCatches(obj) {
  31. for (const catchId of Object.keys(obj)) {
  32. let div = document.createElement("div")
  33. div.classList.add("catch")
  34. div.setAttribute("data-id", `${catchId}`)
  35.  
  36. div.innerHTML = `<label>Angler</label>
  37. <input type="text" class="angler" value="${obj[catchId].angler}" />
  38. <hr>
  39. <label>Weight</label>
  40. <input type="number" class="weight" value="${obj[catchId].weight}" />
  41. <hr>
  42. <label>Species</label>
  43. <input type="text" class="species" value="${obj[catchId].species}" />
  44. <hr>
  45. <label>Location</label>
  46. <input type="text" class="location" value="${obj[catchId].location}" />
  47. <hr>
  48. <label>Bait</label>
  49. <input type="text" class="bait" value="${obj[catchId].bait}" />
  50. <hr>
  51. <label>Capture Time</label>
  52. <input type="number" class="captureTime" value="${obj[catchId].captureTime}" />
  53. <hr>
  54. <button class="update">Update</button>
  55. <button class="delete">Delete</button>`
  56. catches.appendChild(div);
  57. }
  58. }
  59.  
  60. function addCatch() {
  61. const inputs = document.querySelectorAll("aside fieldset#addForm input");
  62. let obj = JSON.stringify(getDataFromInputForm(inputs))
  63.  
  64. fetch(LoadCreateURL,
  65. {
  66. method: "post",
  67. body: obj
  68. })
  69. .catch()
  70. }
  71.  
  72. async function updateCatchAsync(e) {
  73. let divCatch = e.target.parentNode
  74. let catchId = divCatch.attributes["data-id"].value
  75. let obj = getDataFromInputForm(divCatch.getElementsByTagName("input"))
  76.  
  77. await fetch(UpdateDeleteURl(catchId),
  78. {
  79. method: "put",
  80. body: JSON.stringify(obj)
  81. })
  82. .catch(e => console.error(e))
  83. }
  84.  
  85. async function deleteCatchAsync(e) {
  86. let catchId = e.target.parentNode.attributes["data-id"].value
  87.  
  88. await fetch(UpdateDeleteURl(catchId),
  89. {
  90. method: "delete",
  91. })
  92. .catch(e => console.error(e))
  93. }
  94.  
  95. function getDataFromInputForm(input) {
  96. let obj = {};
  97. [...input].map(e => (obj[e.className] = e.value));
  98. return obj;
  99. }
  100. }
  101.  
  102. attachEvents();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement