newRecipe.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. let ingredient = ingredients[i].ingredient;
  50. data.ingredients.push({
  51. ingredient: ingredient.id,
  52. quantity: controller.baseUnit(ingredients[i].children[1].children[0].value, ingredient.unit)
  53. });
  54. }
  55. let loader = document.getElementById("loaderContainer");
  56. loader.style.display = "flex";
  57. fetch("/recipe/create", {
  58. method: "post",
  59. headers: {
  60. "Content-Type": "application/json"
  61. },
  62. body: JSON.stringify(data)
  63. })
  64. .then(response => response.json())
  65. .then((response)=>{
  66. if(typeof(response) === "string"){
  67. controller.createBanner(response, "error");
  68. }else{
  69. merchant.addRecipes([response]);
  70. state.updateRecipes();
  71. controller.createBanner("RECIPE CREATED", "success");
  72. controller.openStrand("recipeBook");
  73. }
  74. })
  75. .catch((err)=>{
  76. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  77. })
  78. .finally(()=>{
  79. loader.style.display = "none";
  80. });
  81. }
  82. };
  83. module.exports = newRecipe;