addIngredients.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. quantityInput.classList = "inputField";
  35. quantity.appendChild(quantityInput);
  36. let unit = document.createElement("td");
  37. unit.innerText = ingredient.unit;
  38. row.appendChild(unit);
  39. }
  40. },
  41. submit: function(){
  42. controller.data.inventory = [];
  43. let tbody = document.querySelector("#ingredient-display tbody");
  44. let isValid = true;
  45. for(let row of tbody.children){
  46. if(row.children[0].children[0].checked){
  47. let quantity = row.children[3].children[0].value;
  48. if(validator.ingredient.quantity(quantity)){
  49. controller.data.inventory.push({
  50. ingredient: {
  51. id: row.id,
  52. name: row.children[1].innerText,
  53. category: row.children[2].innerText,
  54. unit: row.children[4].innerText
  55. },
  56. quantity: quantity
  57. });
  58. }else{
  59. isValid = false;
  60. break;
  61. }
  62. }
  63. }
  64. if(isValid){
  65. createIngredientsObj.display();
  66. }
  67. }
  68. }