| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- let recipeDetailsComp = {
- recipe: {},
- display: function(recipe){
- this.recipe = recipe;
- openSidebar(document.querySelector("#recipeDetails"));
- document.querySelector("#recipeName").style.display = "block";
- document.querySelector("#recipeNameIn").style.display = "none";
- document.querySelector("#recipeDetails h1").innerText = recipe.name;
- let ingredientList = document.querySelector("#recipeIngredientList");
- while(ingredientList.children.length > 0){
- ingredientList.removeChild(ingredientList.firstChild);
- }
- let template = document.querySelector("#recipeIngredient").content.children[0];
- for(let i = 0; i < recipe.ingredients.length; i++){
- ingredientDiv = template.cloneNode(true);
- ingredientDiv.children[0].innerText = recipe.ingredients[i].ingredient.name;
- ingredientDiv.children[2].innerText = `${recipe.ingredients[i].quantity} ${recipe.ingredients[i].ingredient.unit}`;
- ingredientDiv._id = recipe.ingredients[i].ingredient._id;
- ingredientDiv.name = recipe.ingredients[i].ingredient.name;
- ingredientList.appendChild(ingredientDiv);
- }
- document.querySelector("#addRecIng").style.display = "none";
- let price = document.querySelector("#recipePrice");
- price.children[1].style.display = "block";
- price.children[2].style.display = "none";
- price.children[1].innerText = `$${(recipe.price / 100).toFixed(2)}`;
- document.querySelector("#recipeUpdate").style.display = "none";
- },
- edit: function(){
- let ingredientDivs = document.querySelector("#recipeIngredientList");
- let name = document.querySelector("#recipeName");
- let nameIn = document.querySelector("#recipeNameIn");
- name.style.display = "none";
- nameIn.style.display = "block";
- nameIn.placeholder = name.innerText;
- for(let i = 0; i < ingredientDivs.children.length; i++){
- let div = ingredientDivs.children[i];
- div.children[2].innerText = this.recipe.ingredients[i].ingredient.unit;
- div.children[1].style.display = "block";
- div.children[1].placeholder = this.recipe.ingredients[i].quantity;
- div.children[3].style.display = "block";
- div.children[3].onclick = ()=>{div.parentElement.removeChild(div)};
- }
- document.querySelector("#addRecIng").style.display = "flex";
- let price = document.querySelector("#recipePrice");
- price.children[1].style.display = "none";
- price.children[2].style.display = "block";
- price.children[2].placeholder = price.children[1].innerText;
- document.querySelector("#recipeUpdate").style.display = "flex";
- },
- update: function(){
- let updatedRecipe = {
- _id: this.recipe._id,
- name: document.querySelector("#recipeNameIn").value || this.recipe.name,
- price: Math.round((document.querySelector("#recipePrice").children[2].value * 100)) || this.recipe.price,
- ingredients: []
- }
- let divs = document.querySelector("#recipeIngredientList").children;
- for(let i = 0; i < divs.length; i++){
- if(divs[i].name === "new"){
- updatedRecipe.ingredients.push({
- ingredient: divs[i].children[0].value,
- quantity: divs[i].children[1].value
- })
- }else{
- updatedRecipe.ingredients.push({
- ingredient: divs[i]._id,
- quantity: divs[i].children[1].value || divs[i].children[1].placeholder
- });
- }
- }
- if(validator.recipe(updatedRecipe)){
- fetch("/recipe/update", {
- method: "PUT",
- headers: {
- "Content-Type": "application/json;charset=utf-8"
- },
- body: JSON.stringify(updatedRecipe)
- })
- .then((response) => response.json())
- .then((response)=>{
- if(typeof(response) === "string"){
- banner.createError(response);
- }else{
- updateRecipes(updatedRecipe);
- banner.createNotification("Recipe successfully updated");
- }
- })
- .catch((err)=>{
- banner.createError("Something went wrong. Please refresh the page");
- })
- }
- },
- remove: function(){
- fetch(`/merchant/recipes/remove/${this.recipe._id}`, {
- method: "DELETE"
- })
- .then((response) => response.json())
- .then((response)=>{
- if(typeof(response) === "string"){
- banner.createError(response);
- }else{
- updateRecipes(this.recipe, true);
- banner.createNotification("Recipe removed");
- }
- })
- .catch((err)=>{
- banner.createError("Something went wrong. Try refreshing the page");
- });
- },
- displayAddIngredient: function(){
- let template = document.querySelector("#addRecIngredient").content.children[0].cloneNode(true);
- template.name = "new";
- document.querySelector("#recipeIngredientList").appendChild(template);
- let categories = categorizeIngredients(merchant.inventory);
- for(let i = 0; i < categories.length; i++){
- let optGroup = document.createElement("optgroup");
- optGroup.label = categories[i].name;
- template.children[0].appendChild(optGroup);
- for(let j = 0; j < categories[i].ingredients.length; j++){
- let option = document.createElement("option");
- option.innerText = `${categories[i].ingredients[j].name} (${categories[i].ingredients[j].unit})`;
- option.value = categories[i].ingredients[j].id;
- optGroup.appendChild(option);
- }
- }
- }
- }
- let newOrderComp = {
- isPopulated: false,
- display: function(){
- openSidebar(document.querySelector("#newOrder"));
- if(!this.isPopulated){
- let categories = categorizeIngredients(merchant.inventory);
- let categoriesList = document.querySelector("#newOrderCategories");
- let template = document.querySelector("#addIngredientsCategory").content.children[0];
- let ingredientTemplate = document.querySelector("#addIngredientsIngredient").content.children[0];
- for(let i = 0; i < categories.length; i++){
- let category = template.cloneNode(true);
- category.children[0].children[0].innerText = categories[i].name;
- category.children[0].children[1].onclick = ()=>{addIngredientsComp.toggleAddIngredient(category)};
- category.children[0].children[1].children[1].style.display = "none";
- category.children[1].style.display = "none";
-
- categoriesList.appendChild(category);
- for(let j = 0; j < categories[i].ingredients.length; j++){
- let ingredientDiv = ingredientTemplate.cloneNode(true);
- ingredientDiv.children[0].innerText = categories[i].ingredients[j].name;
- ingredientDiv.children[1].placeholder = categories[i].ingredients[j].unit;
- ingredientDiv._id = categories[i].ingredients[j].id;
- ingredientDiv._name = categories[i].ingredients[j].name;
- ingredientDiv._unit = categories[i].ingredients[j].unit;
- ingredientDiv._category = categories[i].name;
-
- let priceInput = document.createElement("input");
- priceInput.type = "number";
- priceInput.min = "0";
- priceInput.step = "0.01";
- priceInput.placeholder = "Total Cost";
- ingredientDiv.appendChild(priceInput);
- category.children[1].appendChild(ingredientDiv);
- }
- }
- this.isPopulated = true;
- }
- },
- submit: function(){
- let categoriesList = document.querySelector("#newOrderCategories");
- let newOrder = {
- orderId: "none",
- date: new Date(),
- ingredients: []
- }
- for(let i = 0; i < categoriesList.children.length; i++){
- for(let j = 0; j < categoriesList.children[i].children[1].children.length; j++){
- let ingredientDiv = categoriesList.children[i].children[1].children[j];
- let quantity = ingredientDiv.children[1].value;
- if(quantity !== ""){
- let newIngredient = {
- ingredient: ingredientDiv._id,
- quantity: quantity,
- price: ingredientDiv.children[2].value
- }
- newOrder.ingredients.push(newIngredient);
- }
- }
- }
- fetch("/order", {
- method: "POST",
- headers: {
- "Content-Type": "application/json;charset=utf-8"
- },
- body: JSON.stringify(newOrder)
- })
- .then(response => response.json())
- .then((response)=>{
- if(typeof(response) === "string"){
- banner.createError(response);
- }else{
- banner.createNotification("New order created");
- //update orders list
- }
- })
- .catch((err)=>{
- console.log(err);
- banner.createError("Something went wrong. Try refreshing the page");
- });
- }
- }
|