addIngredients.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. addIngredientsObj = {
  2. isPopulated: false,
  3. display: function(){
  4. controller.clearScreen();
  5. controller.addIngredientsStrand.style.display = "flex";
  6. if(!this.isPopulated){
  7. this.populate();
  8. this.isPopulated = true;
  9. }
  10. },
  11. populate: function(){
  12. let tbody = document.querySelector("#ingredient-display tbody");
  13. for(let ingredient of ingredients){
  14. let row = document.createElement("tr");
  15. row.id = ingredient._id;
  16. tbody.appendChild(row);
  17. let add = document.createElement("td");
  18. row.appendChild(add);
  19. let checkbox = document.createElement("input");
  20. checkbox.type = "checkbox";
  21. add.appendChild(checkbox);
  22. let name = document.createElement("td");
  23. name.innerText = ingredient.name;
  24. row.appendChild(name);
  25. let category = document.createElement("td");
  26. category.innerText = ingredient.category;
  27. row.appendChild(category);
  28. let quantity = document.createElement("td");
  29. row.appendChild(quantity);
  30. let quantityInput = document.createElement("input");
  31. quantityInput.type = "number";
  32. quantityInput.step = "0.01";
  33. quantityInput.min = "0";
  34. quantity.appendChild(quantityInput);
  35. let unit = document.createElement("td");
  36. unit.innerText = ingredient.unit;
  37. row.appendChild(unit);
  38. }
  39. },
  40. submit: function(){
  41. controller.data.inventory = [];
  42. let tbody = document.querySelector("#ingredient-display tbody");
  43. let isValid = true;
  44. for(let row of tbody.children){
  45. if(row.children[0].children[0].checked){
  46. let quantity = row.children[3].children[0].value;
  47. if(validator.ingredient.quantity(quantity)){
  48. controller.data.inventory.push({
  49. ingredient: {
  50. id: row.id,
  51. name: row.children[1].innerText,
  52. category: row.children[2].innerText,
  53. unit: row.children[4].innerText
  54. },
  55. quantity: quantity
  56. });
  57. }else{
  58. isValid = false;
  59. break;
  60. }
  61. }
  62. }
  63. if(isValid){
  64. createIngredientsObj.display();
  65. }
  66. }
  67. }