| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- let newRecipe = {
- unchosen: [],
- display: function(){
- document.getElementById("sidebarDiv").classList.add("sidebarWide");
- document.getElementById("newRecipeName").value = "";
- document.getElementById("newRecipeCategory").value = "";
- document.getElementById("newRecipePrice").value = "";
- let chosen = document.getElementById("newRecipeChosenList");
-
- while(chosen.children.length > 0){
- chosen.removeChild(chosen.firstChild);
- }
- document.getElementById("submitNewRecipe").onclick = ()=>{this.gatherData()};
- document.getElementById("recipeFileUpload").onclick = ()=>{controller.openModal("recipeSpreadsheet")};
- document.getElementById("newRecipeSearch").onkeyup = ()=>{this.populateChoices()};
- this.unchosen = [];
- for(let i = 0; i < merchant.inventory.length; i++){
- this.unchosen.push(merchant.inventory[i].ingredient);
- }
- this.populateChoices();
- },
- populateChoices: function(){
- this.unchosen.sort((a, b) => (a.name > b.name) ? 1 : -1);
- let searchStr = document.getElementById("newRecipeSearch").value;
- let list = document.getElementById("recipeChoices");
- while(list.children.length > 0){
- list.removeChild(list.firstChild);
- }
- for(let i = 0; i < this.unchosen.length; i++){
- if(searchStr === "" || this.unchosen[i].name.toLowerCase().includes(searchStr)){
- let ingredient = document.createElement("button");
- ingredient.innerText = this.unchosen[i].name;
- ingredient.classList.add("choosable");
- ingredient.classList.add("selection");
- ingredient.onclick = ()=>{
- this.add(this.unchosen[i]);
- this.unchosen.splice(i, 1);
- this.populateChoices();
- };
- list.appendChild(ingredient);
- }
- }
- },
- add: function(ingredient){
- let element = document.getElementById("newRecipeChosenIngredient").content.children[0].cloneNode(true);
- element.children[0].children[0].innerText = ingredient.name;
- element.children[1].children[0].placeholder = "QUANTITY";
- element.children[0].children[1].onclick = ()=>{
- this.unchosen.push(ingredient);
- element.parentElement.removeChild(element);
- this.populateChoices();
- };
- element.ingredient = ingredient;
- document.getElementById("newRecipeChosenList").appendChild(element);
- },
- gatherData: function(){
- let data = {
- name: document.getElementById("newRecipeName").value,
- category: document.getElementById("newRecipeCategory").value,
- price: parseInt(document.getElementById("newRecipePrice").value * 100),
- ingredients: []
- };
-
- let mismatchUnits = [];
- let ingredients = document.getElementById("newRecipeChosenList").children;
- for(let i = 0; i < ingredients.length; i++){
- let ingredient = ingredients[i].ingredient;
- let newIngredient = {
- ingredient: ingredient.id,
- quantity: ingredients[i].children[1].children[0].value,
- unit: ingredients[i].children[1].children[1].value
- }
-
- // if(ingredient.getPotentialUnits().includes(newIngredient.unit) === false){
- // mismatchUnits.push({ingredient: ingredient, newIngredient: newIngredient});
- // }else{
- // newIngredient.baseUnitMultiplier = 1 / controller.baseUnit(1, newIngredient.unit);
- // }
-
-
- data.ingredients.push(newIngredient);
- }
- if(mismatchUnits.length === 0){
- this.submit(data);
- return;
- }
- controller.openModal("alternateUnitConversion", {mismatchUnits: mismatchUnits, recipe: data, submit: this.submit});
- },
- submit: function(data){
- let loader = document.getElementById("loaderContainer");
- loader.style.display = "flex";
- fetch("/recipe/create", {
- method: "post",
- headers: {
- "Content-Type": "application/json"
- },
- body: JSON.stringify(data)
- })
- .then(response => response.json())
- .then((response)=>{
- if(typeof(response) === "string"){
- controller.createBanner(response, "error");
- }else{
- merchant.addRecipes([response]);
- state.updateRecipes();
- controller.createBanner("RECIPE CREATED", "success");
- controller.openStrand("recipeBook");
- }
- })
- .catch((err)=>{
- controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
- })
- .finally(()=>{
- loader.style.display = "none";
- });
- },
- submitSpreadsheet: function(){
- event.preventDefault();
- controller.closeModal();
- const file = document.getElementById("spreadsheetInput").files[0];
- let data = new FormData();
- data.append("recipes", file);
- let loader = document.getElementById("loaderContainer");
- loader.style.display = "flex";
- fetch("/recipes/create/spreadsheet", {
- method: "post",
- body: data
- })
- .then(response => response.json())
- .then((response)=>{
- if(typeof(response) === "string"){
- controller.createBanner(response, "error");
- }else{
- merchant.addRecipes(response);
- state.updateRecipes();
- controller.createBanner("ALL RECIPES SUCCESSFULLY CREATED", "success");
- controller.openStrand("recipeBook");
- }
- })
- .catch((err)=>{
- controller.createBanner("UNABLE TO DISPLAY NEW RECIPES. PLEASE REFRESH THE PAGE", "error");
- })
- .finally(()=>{
- loader.style.display = "none";
- });
- }
- };
- module.exports = newRecipe;
|