Advertisement
Guest User

Untitled

a guest
Nov 25th, 2019
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. function solve() {
  2. function validateExisting(x) {
  3. if (x) {
  4. return x;
  5. }
  6. debugger;
  7. throw new Error("Non existing element!");
  8. }
  9.  
  10. function resetValues(...params) {
  11. params.map(x => {
  12. if (x.value === "") {
  13. x.innerHTML = "";
  14. return;
  15. }
  16. x.value = "";
  17. });
  18. }
  19.  
  20. function createElementWithContent(tag, content_s) {
  21. //Content is optional
  22. let result = document.createElement(tag);
  23. if (["string", "number"].includes(typeof content_s)) {
  24. result.textContent = content_s;
  25. } else if (Array.isArray(content_s)) {
  26. content_s.forEach(x => {
  27. result.appendChild(x);
  28. });
  29. } else if (content_s instanceof HTMLElement) {
  30. result.appendChild(content_s);
  31. }
  32. return result;
  33. }
  34.  
  35. function appendToParent(parrent, element_s) {
  36. element_s = Array.isArray(element_s) ? element_s : [element_s];
  37. if (element_s.some(x => !(x instanceof HTMLElement))) {
  38. debugger;
  39. throw new Error("Arguments contain Non - Html Elements!");
  40. }
  41. element_s.forEach(x => {
  42. parrent.appendChild(x);
  43. });
  44. return parrent;
  45. }
  46. ////////////////////////////////////////////////////////////////////////////
  47. const addButtonText = `Add to Client's List`;
  48. let cost = 0;
  49. const [nameEl, quantityEl, priceEl] = Array.from(
  50. document.querySelectorAll("#add-new > input")
  51. ).map(x => validateExisting(x));
  52.  
  53. const searchEl = validateExisting(document.getElementById("filter"));
  54. const searchBtn = validateExisting(
  55. document.querySelector("#products > div > button")
  56. );
  57.  
  58. const addBtn = validateExisting(document.querySelector("#add-new > button"));
  59. const shopItemsUl = validateExisting(
  60. document.querySelector("#products > ul")
  61. );
  62. const userItemsUl = validateExisting(
  63. document.querySelector("#myProducts > ul")
  64. );
  65.  
  66. const totalPriceEl = validateExisting(
  67. document.querySelector("body > h1:nth-child(4)")
  68. );
  69.  
  70. const buyBtn = validateExisting(
  71. document.querySelector("#myProducts > button")
  72. );
  73.  
  74. function refreshPrice() {
  75. totalPriceEl.innerText = `Total Price: ${cost.toFixed(2)}`; //ToFixed???
  76. }
  77.  
  78. addBtn.addEventListener("click", addToShopList);
  79.  
  80. function addToShopList(evnt) {
  81. evnt.preventDefault();
  82. let [name, quantity, price] = [nameEl, quantityEl, priceEl]
  83. .map(x => x.value)
  84. .map(x => (isNaN(x) || x === "" ? x : Number(x)));
  85. if ([name, quantity, price].some(x => x === "" || Number(x) <= 0)) {
  86. console.log("invalid Input!");
  87. return;
  88. }
  89.  
  90. let newElement = productToShopItem({
  91. name,
  92. quantity,
  93. price
  94. });
  95. shopItemsUl.appendChild(newElement);
  96. }
  97.  
  98. function productToShopItem(product) {
  99. let newEl = createElementWithContent("li", [
  100. createElementWithContent("span", `${product.name}`),
  101. createElementWithContent("strong", `Available: ${product.quantity}`),
  102. appendToParent(createElementWithContent("div"), [
  103. createElementWithContent("strong", `${product.price.toFixed(2)}`),
  104. createElementWithContent("button", addButtonText)
  105. ])
  106. ]);
  107. newEl.querySelector("div > button").addEventListener("click", addToCart);
  108. return newEl;
  109. }
  110.  
  111. function addToCart(evnt) {
  112. let name = evnt.target.parentNode.parentNode.firstChild.textContent;
  113. let price = Number(evnt.target.previousSibling.textContent);
  114. cost += price;
  115. refreshPrice();
  116. let quantity = Number(
  117. evnt.target.parentNode.previousSibling.textContent.split(": ")[1]
  118. );
  119. if (quantity > 1) {
  120. evnt.target.parentNode.previousSibling.textContent = `Available: ${quantity -
  121. 1}`;
  122. } else {
  123. evnt.target.parentNode.parentNode.parentNode.removeChild(
  124. evnt.target.parentNode.parentNode
  125. );
  126. }
  127. let newPurchase = createPurchaseItem(name, price);
  128. userItemsUl.appendChild(newPurchase);
  129. }
  130.  
  131. function createPurchaseItem(name, price) {
  132. return appendToParent(
  133. createElementWithContent("li", `${name}`),
  134. createElementWithContent("strong", `${price.toFixed(2)}`) //ToFixed???
  135. );
  136. }
  137.  
  138. searchBtn.addEventListener("click", showHide);
  139.  
  140. function showHide() {
  141. let phrase = searchEl.value.toLowerCase();
  142. let selectedItems = Array.from(shopItemsUl.children).map(el => {
  143. if (el.firstChild.textContent.toLowerCase().includes(phrase)) {
  144. el.style.display = "";
  145. } else {
  146. el.style.display = "none";
  147. }
  148. return el;
  149. });
  150. }
  151.  
  152. buyBtn.addEventListener("click", function() {
  153. userItemsUl.innerHTML = "";
  154. cost = 0;
  155. refreshPrice();
  156. });
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement