| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- let addIngredientObj = {
- isPopulated: false,
- display: function(){
- controller.clearScreen();
- controller.addIngredientStrand.style.display = "flex";
- if(!this.isPopulated){
- this.populateIngredients();
- this.isPopulated = true;
- }
- },
- populateIngredients: function(){
- axios.get("/ingredients")
- .then((ingredients)=>{
- let select = document.querySelector("#addIngredientStrand select");
- for(let ingredient of ingredients.data){
- let option = document.createElement("option");
- option.value = ingredient._id;
- option.innerText = `${ingredient.name} (${ingredient.unit})`;
- select.appendChild(option);
- }
- })
- .catch((err)=>{
- banner.createError("Could not reach the database");
- });
- },
- submitAdd: function(){
- event.preventDefault();
- let item = {
- ingredient: document.querySelector("#addName").value,
- quantity: document.querySelector("#addQuantity").value
- }
- if(validator.ingredient.quantity){
- axios.post("/merchant/ingredients/create", item)
- .then((ingredient)=>{
- if(typeof(ingredient.data) === "string"){
- banner.createError(ingredient.data);
- }else{
- merchant.inventory.push(ingredient.data);
- inventoryObj.display();
- inventoryObj.filter();
- }
- })
- .catch((err)=>{
- banner.createError("Something went wrong and the ingredient could not be added");
- });
- }
- },
- submitNew: function(){
- event.preventDefault();
- let ingredient = {
- name: document.querySelector("#newName").value,
- category: document.querySelector("#newCategory").value,
- unit: document.querySelector("#newUnit").value
- }
- let quantity = document.querySelector("#newQuantity").value;
- if(validator.ingredient.all(ingredient, quantity)){
- axios.post("/ingredients/createone", {ingredient: ingredient, quantity: quantity})
- .then((item)=>{
- merchant.inventory.push(item.data);
- inventoryObj.display();
- inventoryObj.filter();
- })
- .catch((err)=>{
- banner.createError("Something went wrong and the ingredient could not be created");
- });
- }
- }
- }
|