|
|
@@ -70,7 +70,7 @@ let filter = ()=>{
|
|
|
name: item.ingredient.name,
|
|
|
category: item.ingredient.category,
|
|
|
quantity: item.quantity,
|
|
|
- unit: item.ingredient.unitType
|
|
|
+ unit: item.ingredient.unit
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
@@ -154,48 +154,113 @@ let removeIngredient = (id, row)=>{
|
|
|
|
|
|
//Display the modal to allow for adding a new ingredient
|
|
|
let displayAdd = ()=>{
|
|
|
+ document.querySelector("#existingIngredient").style.display = "block";
|
|
|
+ document.querySelector("#quantityInput").style.display = "none";
|
|
|
+ document.querySelector("#newIngredient").style.display = "none";
|
|
|
+ document.querySelector("#createNew").onclick = ()=>{displayNew();};
|
|
|
+
|
|
|
let modal = document.querySelector(".add-ingredient");
|
|
|
|
|
|
let removeModal = (modal)=>{modal.style.visibility = "hidden";}
|
|
|
|
|
|
- modal.onclick = ()=>{removeModal(modal);}
|
|
|
+ modal.onclick = ()=>{removeModal(modal);};
|
|
|
|
|
|
modal.style.visibility = "visible";
|
|
|
+
|
|
|
+ axios.get("/ingredients")
|
|
|
+ .then((ingredients)=>{
|
|
|
+ let tbody = document.querySelector("#existingIngredient table tbody");
|
|
|
+
|
|
|
+ while(tbody.children.length > 0){
|
|
|
+ tbody.removeChild(tbody.firstChild);
|
|
|
+ }
|
|
|
+
|
|
|
+ for(let ingredient of ingredients.data){
|
|
|
+ let row = document.createElement("tr");
|
|
|
+ tbody.appendChild(row);
|
|
|
+
|
|
|
+ let name = document.createElement("td");
|
|
|
+ name.innerText = ingredient.name;
|
|
|
+ row.appendChild(name);
|
|
|
+
|
|
|
+ let category = document.createElement("td");
|
|
|
+ category.innerText = ingredient.category;
|
|
|
+ row.appendChild(category);
|
|
|
+
|
|
|
+ let unit = document.createElement("td");
|
|
|
+ unit.innerText = ingredient.unit;
|
|
|
+ row.appendChild(unit);
|
|
|
+
|
|
|
+ let addButton = document.createElement("button");
|
|
|
+ addButton.innerText = "Add";
|
|
|
+ addButton.onclick = ()=>{configureAddIngredient(ingredient);};
|
|
|
+ row.appendChild(addButton);
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .catch((err)=>{
|
|
|
+ banner.createError("Failed to retrieve ingredients list");
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
-//Update new ingredient on both the page and the database
|
|
|
-//Close the modal
|
|
|
-let addIngredient = ()=>{
|
|
|
- let content = document.querySelector(".modal-content");
|
|
|
+let configureAddIngredient = (ingredient)=>{
|
|
|
+ document.querySelector("#existingIngredient").style.display = "none";
|
|
|
+ document.querySelector("#quantityInput").style.display = "block";
|
|
|
+ document.querySelector("#newIngredient").style.display = "none";
|
|
|
+
|
|
|
+ document.querySelector("#quantityInputTitle").innerText = `${ingredient.name} (${ingredient.unit})`;
|
|
|
|
|
|
- let newIngredient = {
|
|
|
+ let button = document.querySelector("#addIngredient");
|
|
|
+ let input = document.querySelector("#quantityInput input");
|
|
|
+
|
|
|
+ button.onclick = ()=>{addIngredient({ingredient: ingredient, quantity: input.value});};
|
|
|
+}
|
|
|
+
|
|
|
+let displayNew = ()=>{
|
|
|
+ document.querySelector("#existingIngredient").style.display = "none";
|
|
|
+ document.querySelector("#quantityInput").style.display = "none";
|
|
|
+ document.querySelector("#newIngredient").style.display = "block";
|
|
|
+ document.querySelector("#createIngredient").onclick = ()=>{createIngredient();};
|
|
|
+}
|
|
|
+
|
|
|
+let createIngredient = ()=>{
|
|
|
+ let newItem = {
|
|
|
ingredient: {
|
|
|
- name: content.children[0].children[0].value,
|
|
|
- category: content.children[1].children[0].value,
|
|
|
- unitType: content.children[3].children[0].value
|
|
|
+ name: document.querySelector("#newName").value,
|
|
|
+ category: document.querySelector("#newCategory").value,
|
|
|
+ unit: document.querySelector("#newUnit").value
|
|
|
},
|
|
|
- quantity: content.children[2].children[0].value
|
|
|
+ quantity: document.querySelector("#newQuantity").value
|
|
|
}
|
|
|
|
|
|
- if(validator.ingredient.all(newIngredient.ingredient, newIngredient.quantity)){
|
|
|
- axios.post("/ingredients/createone", newIngredient)
|
|
|
- .then((ingredient)=>{
|
|
|
- items.push({
|
|
|
- id: ingredient._id,
|
|
|
- name: newIngredient.ingredient.name,
|
|
|
- category: newIngredient.ingredient.category,
|
|
|
- quantity: newIngredient.quantity,
|
|
|
- unit: newIngredient.ingredient.unitType
|
|
|
+ addIngredient(newItem);
|
|
|
+}
|
|
|
+
|
|
|
+//Update new ingredient on both the page and the database
|
|
|
+//Close the modal
|
|
|
+let addIngredient = (item)=>{
|
|
|
+ if(validator.ingredient.all(item.ingredient, item.quantity)){
|
|
|
+ merchant.inventory.push(item);
|
|
|
+ if(item.ingredient._id){
|
|
|
+ axios.post("/merchant/ingredients/create", {ingredient: item, merchantId: merchant._id})
|
|
|
+ .then((newMerchant)=>{
|
|
|
+ filter();
|
|
|
+ banner.createNotification("The new ingredient has been successfully added to your inventory");
|
|
|
})
|
|
|
-
|
|
|
- sortIngredients("name");
|
|
|
- renderIngredients();
|
|
|
- banner.createNotification("The new ingredient has been successfully added to your inventory");
|
|
|
- })
|
|
|
- .catch((err)=>{
|
|
|
- banner.createError("There was an error and the ingredient could not be added to your inventory");
|
|
|
- console.log(err);
|
|
|
- });
|
|
|
+ .catch((err)=>{
|
|
|
+ banner.createError("There was an error and the ingredient could not be added to your inventory");
|
|
|
+ console.log(err);
|
|
|
+ });
|
|
|
+ }else{
|
|
|
+ axios.post("/ingredients/createone", {ingredient: item, merchantId: merchant._id})
|
|
|
+ .then((newMerchant)=>{
|
|
|
+ filter();
|
|
|
+ banner.createNotification("The new ingredient has been successfully added to your inventory");
|
|
|
+ })
|
|
|
+ .catch((err)=>{
|
|
|
+ console.log(err);
|
|
|
+ banner.createError("There was an error and the ingredient could not be added to your inventory");
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
let modal = document.querySelector(".add-ingredient");
|