newRecipe.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. let newRecipe = {
  2. unchosen: [],
  3. display: function(){
  4. document.getElementById("sidebarDiv").classList.add("sidebarWide");
  5. document.getElementById("submitNewRecipe").onclick = ()=>{this.submit()};
  6. for(let i = 0; i < merchant.ingredients.length; i++){
  7. this.unchosen.push(merchant.ingredients[i].ingredient);
  8. }
  9. this.populateChoices();
  10. },
  11. populateChoices: function(){
  12. this.unchosen.sort((a, b) => (a.name > b.name) ? 1 : -1);
  13. let list = document.getElementById("recipeChoices");
  14. while(list.children.length > 0){
  15. list.removeChild(list.firstChild);
  16. }
  17. for(let i = 0; i < this.unchosen.length; i++){
  18. let ingredient = document.createElement("button");
  19. ingredient.innerText = this.unchosen[i].name;
  20. ingredient.classList.add("choosable");
  21. ingredient.onclick = ()=>{
  22. this.add(this.unchosen[i]);
  23. this.unchosen.splice(i, 1);
  24. this.populateChoices();
  25. };
  26. list.appendChild(ingredient);
  27. }
  28. },
  29. add: function(ingredient){
  30. let element = document.getElementById("newRecipeChosenIngredient").content.children[0].cloneNode(true);
  31. element.children[0].innerText = ingredient.name;
  32. element.children[1].children[0].placeholder = `QUANTITY (${ingredient.unit.toUpperCase()})`;
  33. element.children[1].children[1].onclick = ()=>{
  34. this.unchosen.push(ingredient);
  35. element.parentElement.removeChild(element);
  36. this.populateChoices();
  37. };
  38. element.ingredient = ingredient;
  39. document.getElementById("newRecipeChosenList").appendChild(element);
  40. },
  41. submit: function(){
  42. let data = {
  43. name: document.getElementById("newRecipeName").value,
  44. price: document.getElementById("newRecipePrice").value,
  45. ingredients: []
  46. };
  47. let ingredients = document.getElementById("newRecipeChosenList").children;
  48. for(let i = 0; i < ingredients.length; i++){
  49. data.ingredients.push({
  50. ingredient: ingredients[i].ingredient.id,
  51. quantity: ingredients[i].children[1].children[0].value
  52. })
  53. }
  54. console.log(data);
  55. }
  56. };
  57. module.exports = newRecipe;