components.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. let recipeDetailsComp = {
  2. recipe: {},
  3. display: function(recipe){
  4. this.recipe = recipe;
  5. openSidebar(document.querySelector("#recipeDetails"));
  6. document.querySelector("#recipeName").style.display = "block";
  7. document.querySelector("#recipeNameIn").style.display = "none";
  8. document.querySelector("#recipeDetails h1").innerText = recipe.name;
  9. let ingredientList = document.querySelector("#recipeIngredientList");
  10. while(ingredientList.children.length > 0){
  11. ingredientList.removeChild(ingredientList.firstChild);
  12. }
  13. let template = document.querySelector("#recipeIngredient").content.children[0];
  14. for(let i = 0; i < recipe.ingredients.length; i++){
  15. ingredientDiv = template.cloneNode(true);
  16. ingredientDiv.children[0].innerText = recipe.ingredients[i].ingredient.name;
  17. ingredientDiv.children[2].innerText = `${recipe.ingredients[i].quantity} ${recipe.ingredients[i].ingredient.unit}`;
  18. ingredientDiv.ingredient = recipe.ingredients[i].ingredient;
  19. ingredientDiv.name = recipe.ingredients[i].ingredient.name;
  20. ingredientList.appendChild(ingredientDiv);
  21. }
  22. document.querySelector("#addRecIng").style.display = "none";
  23. let price = document.querySelector("#recipePrice");
  24. price.children[1].style.display = "block";
  25. price.children[2].style.display = "none";
  26. price.children[1].innerText = `$${(recipe.price / 100).toFixed(2)}`;
  27. document.querySelector("#recipeUpdate").style.display = "none";
  28. },
  29. edit: function(){
  30. let ingredientDivs = document.querySelector("#recipeIngredientList");
  31. if(merchant.pos === "none"){
  32. let name = document.querySelector("#recipeName");
  33. let nameIn = document.querySelector("#recipeNameIn");
  34. name.style.display = "none";
  35. nameIn.style.display = "block";
  36. nameIn.placeholder = name.innerText;
  37. let price = document.querySelector("#recipePrice");
  38. price.children[1].style.display = "none";
  39. price.children[2].style.display = "block";
  40. price.children[2].placeholder = price.children[1].innerText;
  41. }
  42. for(let i = 0; i < ingredientDivs.children.length; i++){
  43. let div = ingredientDivs.children[i];
  44. div.children[2].innerText = this.recipe.ingredients[i].ingredient.unit;
  45. div.children[1].style.display = "block";
  46. div.children[1].placeholder = this.recipe.ingredients[i].quantity;
  47. div.children[3].style.display = "block";
  48. div.children[3].onclick = ()=>{div.parentElement.removeChild(div)};
  49. }
  50. document.querySelector("#addRecIng").style.display = "flex";
  51. document.querySelector("#recipeUpdate").style.display = "flex";
  52. },
  53. update: function(){
  54. this.recipe.name = document.querySelector("#recipeNameIn").value || this.recipe.name;
  55. this.recipe.price = Math.round((document.querySelector("#recipePrice").children[2].value * 100)) || this.recipe.price;
  56. this.recipe.ingredients = [];
  57. let divs = document.querySelector("#recipeIngredientList").children;
  58. for(let i = 0; i < divs.length; i++){
  59. if(divs[i].name === "new"){
  60. let select = divs[i].children[0];
  61. this.recipe.ingredients.push({
  62. ingredient: select.options[select.selectedIndex].ingredient,
  63. quantity: divs[i].children[1].value
  64. })
  65. }else{
  66. this.recipe.ingredients.push({
  67. ingredient: divs[i].ingredient,
  68. quantity: divs[i].children[1].value || divs[i].children[1].placeholder
  69. });
  70. }
  71. }
  72. let data = {
  73. id: this.recipe.id,
  74. name: this.recipe.name,
  75. price: this.recipe.price,
  76. ingredients: []
  77. }
  78. for(let i = 0; i < this.recipe.ingredients.length; i++){
  79. data.ingredients.push({
  80. ingredient: this.recipe.ingredients[i].ingredient.id,
  81. quantity: this.recipe.ingredients[i].quantity
  82. });
  83. }
  84. fetch("/recipe/update", {
  85. method: "PUT",
  86. headers: {
  87. "Content-Type": "application/json;charset=utf-8"
  88. },
  89. body: JSON.stringify(data)
  90. })
  91. .then((response) => response.json())
  92. .then((response)=>{
  93. if(typeof(response) === "string"){
  94. banner.createError(response);
  95. }else{
  96. merchant.editRecipe(this.recipe);
  97. banner.createNotification("Recipe successfully updated");
  98. }
  99. })
  100. .catch((err)=>{
  101. banner.createError("Something went wrong. Please refresh the page");
  102. })
  103. },
  104. remove: function(){
  105. fetch(`/merchant/recipes/remove/${this.recipe.id}`, {
  106. method: "DELETE"
  107. })
  108. .then((response) => response.json())
  109. .then((response)=>{
  110. if(typeof(response) === "string"){
  111. banner.createError(response);
  112. }else{
  113. merchant.editRecipe(this.recipe, true);
  114. banner.createNotification("Recipe removed");
  115. }
  116. })
  117. .catch((err)=>{
  118. banner.createError("Something went wrong. Try refreshing the page");
  119. });
  120. },
  121. displayAddIngredient: function(){
  122. let template = document.querySelector("#addRecIngredient").content.children[0].cloneNode(true);
  123. template.name = "new";
  124. document.querySelector("#recipeIngredientList").appendChild(template);
  125. let categories = merchant.categorizeIngredients();
  126. for(let i = 0; i < categories.length; i++){
  127. let optGroup = document.createElement("optgroup");
  128. optGroup.label = categories[i].name;
  129. template.children[0].appendChild(optGroup);
  130. for(let j = 0; j < categories[i].ingredients.length; j++){
  131. let option = document.createElement("option");
  132. option.innerText = `${categories[i].ingredients[j].ingredient.name} (${categories[i].ingredients[j].ingredient.unit})`;
  133. option.ingredient = categories[i].ingredients[j].ingredient;
  134. optGroup.appendChild(option);
  135. }
  136. }
  137. }
  138. }
  139. let newOrderComp = {
  140. isPopulated: false,
  141. unused: [],
  142. display: function(){
  143. if(!this.isPopulated){
  144. let categories = merchant.categorizeIngredients();
  145. let categoriesList = document.querySelector("#newOrderCategories");
  146. let template = document.querySelector("#addIngredientsCategory").content.children[0];
  147. let ingredientTemplate = document.querySelector("#addIngredientsIngredient").content.children[0];
  148. for(let i = 0; i < categories.length; i++){
  149. let category = template.cloneNode(true);
  150. category.children[0].children[0].innerText = categories[i].name;
  151. category.children[0].children[1].onclick = ()=>{addIngredientsComp.toggleAddIngredient(category)};
  152. category.children[0].children[1].children[1].style.display = "none";
  153. category.children[1].style.display = "none";
  154. categoriesList.appendChild(category);
  155. for(let j = 0; j < categories[i].ingredients.length; j++){
  156. let ingredientDiv = ingredientTemplate.cloneNode(true);
  157. ingredientDiv.children[0].innerText = categories[i].ingredients[j].ingredient.name;
  158. ingredientDiv.children[1].onclick = ()=>{this.addOne(ingredientDiv, category.children[1])};
  159. ingredientDiv.ingredient = categories[i].ingredients[j].ingredient;
  160. this.unused.push(categories[i].ingredients[j]);
  161. category.children[1].appendChild(ingredientDiv);
  162. }
  163. }
  164. this.isPopulated = true;
  165. }
  166. openSidebar(document.querySelector("#newOrder"));
  167. },
  168. addOne: function(ingredientDiv, container){
  169. for(let i = 0; i < this.unused.length; i++){
  170. if(this.unused[i] === ingredientDiv){
  171. this.unused.splice(i, 1);
  172. break;
  173. }
  174. }
  175. let quantityInput = document.createElement("input");
  176. quantityInput.type = "number";
  177. quantityInput.placeholder = ingredientDiv.ingredient.unit;
  178. quantityInput.min = "0";
  179. quantityInput.step = "0.01";
  180. ingredientDiv.insertBefore(quantityInput, ingredientDiv.children[1]);
  181. let priceInput = document.createElement("input");
  182. priceInput.type = "number";
  183. priceInput.placeholder = "Price Per Unit";
  184. priceInput.min = "0";
  185. priceInput.step = "0.01";
  186. ingredientDiv.insertBefore(priceInput, ingredientDiv.children[2]);
  187. ingredientDiv.children[3].innerText = "-";
  188. ingredientDiv.children[3].onclick = ()=>{this.removeOne(ingredientDiv, container)};
  189. container.removeChild(ingredientDiv);
  190. document.getElementById("newOrderAdded").appendChild(ingredientDiv);
  191. },
  192. removeOne: function(ingredientDiv, container){
  193. this.unused.push(ingredientDiv.ingredient);
  194. ingredientDiv.removeChild(ingredientDiv.children[1]);
  195. ingredientDiv.removeChild(ingredientDiv.children[1]);
  196. ingredientDiv.children[1].innerText = "+";
  197. ingredientDiv.children[1].onclick = ()=>{this.addOne(ingredientDiv, container)};
  198. ingredientDiv.parentElement.removeChild(ingredientDiv);
  199. container.appendChild(ingredientDiv);
  200. },
  201. submit: function(){
  202. let categoriesList = document.getElementById("newOrderAdded");
  203. let ingredients = [];
  204. for(let i = 0; i < categoriesList.children.length; i++){
  205. let quantity = categoriesList.children[i].children[1].value;
  206. let price = categoriesList.children[i].children[2].value;
  207. if(quantity !== "" && price !== ""){
  208. ingredients.push({
  209. ingredient: categoriesList.children[i].ingredient.id,
  210. quantity: parseFloat(quantity),
  211. price: parseInt(price * 100)
  212. });
  213. }
  214. }
  215. let data = {
  216. orderId: document.getElementById("orderName").value,
  217. date: document.getElementById("orderDate").value,
  218. ingredients: ingredients
  219. }
  220. fetch("/order", {
  221. method: "POST",
  222. headers: {
  223. "Content-Type": "application/json;charset=utf-8"
  224. },
  225. body: JSON.stringify(data)
  226. })
  227. .then(response => response.json())
  228. .then((response)=>{
  229. if(typeof(response) === "string"){
  230. banner.createError(response);
  231. }else{
  232. let order = new Order(
  233. response._id,
  234. response.orderId,
  235. response.date,
  236. response.ingredients,
  237. merchant
  238. )
  239. merchant.editOrders([order]);
  240. banner.createNotification("New order created");
  241. // updateInventory(newOrder.ingredients);
  242. }
  243. })
  244. .catch((err)=>{
  245. banner.createError("Something went wrong. Try refreshing the page");
  246. });
  247. },
  248. }
  249. let newIngredientComp = {
  250. display: function(){
  251. openSidebar(document.querySelector("#newIngredient"));
  252. document.querySelector("#newIngName").value = "";
  253. document.querySelector("#newIngCategory").value = "";
  254. document.querySelector("#newIngQuantity").value = 0;
  255. document.querySelector("#newIngUnit").value = ""
  256. },
  257. submit: function(){
  258. let newIngredient = {
  259. ingredient: {
  260. name: document.querySelector("#newIngName").value,
  261. category: document.querySelector("#newIngCategory").value,
  262. unit: document.querySelector("#newIngUnit").value
  263. },
  264. quantity: document.querySelector("#newIngQuantity").value
  265. }
  266. if(validator.ingredient(newIngredient)){
  267. fetch("/ingredients/create", {
  268. method: "POST",
  269. headers: {
  270. "Content-Type": "application/json;charset=utf-8"
  271. },
  272. body: JSON.stringify(newIngredient)
  273. })
  274. .then((response) => response.json())
  275. .then((response)=>{
  276. if(typeof(response) === "string"){
  277. banner.createError(response);
  278. }else{
  279. merchant.editIngredients([{
  280. ingredient: new Ingredient(
  281. response.ingredient._id,
  282. response.ingredient.name,
  283. response.ingredient.category,
  284. response.ingredient.unit
  285. ),
  286. quantity: response.quantity
  287. }]);
  288. banner.createNotification("Ingredient successfully created");
  289. }
  290. })
  291. .catch((err)=>{
  292. banner.createError("Something went wrong. Try refreshing the page");
  293. });
  294. }
  295. }
  296. }
  297. let orderDetailsComp = {
  298. display: function(order){
  299. openSidebar(document.querySelector("#orderDetails"));
  300. document.querySelector("#removeOrderBtn").onclick = ()=>{this.remove(order)};
  301. document.querySelector("#orderDetails h1").innerText = order.name;
  302. document.querySelector("#orderDetails h3").innerText = order.date.toLocaleDateString("en-US");
  303. let ingredientList = document.querySelector("#orderIngredients");
  304. while(ingredientList.children.length > 0){
  305. ingredientList.removeChild(ingredientList.firstChild);
  306. }
  307. let template = document.querySelector("#orderIngredient").content.children[0];
  308. let grandTotal = 0;
  309. for(let i = 0; i < order.ingredients.length; i++){
  310. let ingredient = template.cloneNode(true);
  311. let price = (order.ingredients[i].quantity * order.ingredients[i].price) / 100;
  312. grandTotal += price;
  313. ingredient.children[0].innerText = order.ingredients[i].ingredient.name;
  314. ingredient.children[1].innerText = `$${(order.ingredients[i].price / 100).toFixed(2)}`;
  315. ingredient.children[2].innerText = price.toFixed(2);
  316. ingredientList.appendChild(ingredient);
  317. }
  318. document.querySelector("#orderTotalPrice p").innerText = `$${grandTotal.toFixed(2)}`;
  319. },
  320. remove: function(order){
  321. fetch(`/order/${order.id}`, {
  322. method: "DELETE",
  323. headers: {
  324. "Content-Type": "application/json;charset=utf-8"
  325. }
  326. })
  327. .then((response) => response.json())
  328. .then((response)=>{
  329. if(typeof(response) === "string"){
  330. banner.createError(response);
  331. }else{
  332. merchant.editOrders([order], true);
  333. banner.createNotification("Order successfully removed");
  334. }
  335. })
  336. .catch((err)=>{
  337. console.log(err);
  338. banner.createError("Something went wrong, try refreshing the page");
  339. });
  340. }
  341. }
  342. let addIngredientsComp = {
  343. isPopulated: false,
  344. fakeMerchant: {},
  345. chosenIngredients: [],
  346. display: function(){
  347. let sidebar = document.querySelector("#addIngredients");
  348. if(!this.isPopulated){
  349. fetch("/ingredients")
  350. .then((response) => response.json())
  351. .then((response)=>{
  352. if(typeof(response) === "string"){
  353. banner.createError(response);
  354. }else{
  355. for(let i = 0; i < merchant.ingredients.length; i++){
  356. for(let j = 0; j < response.length; j++){
  357. if(merchant.ingredients[i].ingredient.id === response[j]._id){
  358. response.splice(j, 1);
  359. break;
  360. }
  361. }
  362. }
  363. for(let i = 0; i < response.length; i++){
  364. response[i] = {ingredient: response[i]}
  365. }
  366. this.fakeMerchant = new Merchant(
  367. {
  368. name: "none",
  369. inventory: response,
  370. recipes: [],
  371. },
  372. []
  373. );
  374. this.populateAddIngredients();
  375. }
  376. })
  377. .catch((err)=>{
  378. banner.createError("Unable to retrieve data");
  379. });
  380. this.isPopulated = true;
  381. }
  382. openSidebar(sidebar);
  383. },
  384. populateAddIngredients: function(){
  385. let addIngredientsDiv = document.getElementById("addIngredientList");
  386. let categoryTemplate = document.getElementById("addIngredientsCategory");
  387. let ingredientTemplate = document.getElementById("addIngredientsIngredient");
  388. let categories = this.fakeMerchant.categorizeIngredients();
  389. while(addIngredientsDiv.children.length > 0){
  390. addIngredientsDiv.removeChild(addIngredientsDiv.firstChild);
  391. }
  392. for(let i = 0; i < categories.length; i++){
  393. let categoryDiv = categoryTemplate.content.children[0].cloneNode(true);
  394. categoryDiv.children[0].children[0].innerText = categories[i].name;
  395. categoryDiv.children[0].children[1].onclick = ()=>{addIngredientsComp.toggleAddIngredient(categoryDiv)};
  396. categoryDiv.children[1].style.display = "none";
  397. categoryDiv.children[0].children[1].children[1].style.display = "none";
  398. addIngredientsDiv.appendChild(categoryDiv);
  399. for(let j = 0; j < categories[i].ingredients.length; j++){
  400. let ingredientDiv = ingredientTemplate.content.children[0].cloneNode(true);
  401. ingredientDiv.children[0].innerText = categories[i].ingredients[j].ingredient.name;
  402. ingredientDiv.children[1].onclick = ()=>{this.addOne(ingredientDiv)};
  403. ingredientDiv.ingredient = categories[i].ingredients[j].ingredient;
  404. categoryDiv.children[1].appendChild(ingredientDiv);
  405. }
  406. }
  407. },
  408. toggleAddIngredient: function(categoryElement){
  409. let button = categoryElement.children[0].children[1];
  410. let ingredientDisplay = categoryElement.children[1];
  411. if(ingredientDisplay.style.display === "none"){
  412. ingredientDisplay.style.display = "flex";
  413. button.children[0].style.display = "none";
  414. button.children[1].style.display = "block";
  415. }else{
  416. ingredientDisplay.style.display = "none";
  417. button.children[0].style.display = "block";
  418. button.children[1].style.display = "none";
  419. }
  420. },
  421. addOne: function(element){
  422. element.parentElement.removeChild(element);
  423. document.getElementById("myIngredients").appendChild(element);
  424. document.getElementById("myIngredientsDiv").style.display = "flex";
  425. for(let i = 0; i < this.fakeMerchant.ingredients.length; i++){
  426. if(this.fakeMerchant.ingredients[i].ingredient === element.ingredient){
  427. this.fakeMerchant.ingredients.splice(i, 1);
  428. this.chosenIngredients.push(element.ingredient);
  429. break;
  430. }
  431. }
  432. let input = document.createElement("input");
  433. input.type = "number";
  434. input.min = "0";
  435. input.step = "0.01";
  436. input.placeholder = element._unit;
  437. element.insertBefore(input, element.children[1]);
  438. element.children[2].innerText = "-";
  439. element.children[2].onclick = ()=>{this.removeOne(element)};
  440. },
  441. removeOne: function(element){
  442. element.parentElement.removeChild(element);
  443. element.removeChild(element.children[1]);
  444. element.children[1].innerText = "+";
  445. element.children[1].onclick = ()=>{this.addOne(element)};
  446. if(document.getElementById("myIngredients").children.length === 0){
  447. document.getElementById("myIngredientsDiv").style.display = "none";
  448. }
  449. for(let i = 0; i < this.chosenIngredients.length; i++){
  450. if(this.chosenIngredients[i] === element.ingredient){
  451. this.chosenIngredients.splice(i, 1);
  452. this.fakeMerchant.ingredients.push({
  453. ingredient: element.ingredient
  454. });
  455. break;
  456. }
  457. }
  458. this.populateAddIngredients();
  459. },
  460. submit: function(){
  461. let ingredients = document.getElementById("myIngredients").children;
  462. let newIngredients = [];
  463. let fetchable = [];
  464. for(let i = 0; i < ingredients.length; i++){
  465. if(ingredients[i].children[1].value === ""){
  466. banner.createError("Please enter a quantity for each ingredient you want to add to your inventory");
  467. return;
  468. }
  469. newIngredients.push({
  470. ingredient: ingredients[i].ingredient,
  471. quantity: ingredients[i].children[1].value
  472. });
  473. fetchable.push({
  474. id: ingredients[i].ingredient.id,
  475. quantity: ingredients[i].children[1].value
  476. });
  477. }
  478. fetch("/merchant/ingredients/add", {
  479. method: "POST",
  480. headers: {
  481. "Content-Type": "application/json;charset=utf-8"
  482. },
  483. body: JSON.stringify(fetchable)
  484. })
  485. .then((response)=>{
  486. if(typeof(response) === "string"){
  487. banner.createError(response);
  488. }else{
  489. merchant.editIngredients(newIngredients);
  490. banner.createNotification("All ingredients added successfully");
  491. }
  492. })
  493. .catch((err)=>{
  494. banner.createError("Something went wrong. Try refreshing the page");
  495. });
  496. }
  497. }
  498. let ingredientDetailsComp = {
  499. ingredient: {},
  500. display: function(ingredient, category){
  501. this.ingredient = ingredient;
  502. sidebar = document.querySelector("#ingredientDetails");
  503. document.querySelector("#ingredientDetails p").innerText = category.name;
  504. document.querySelector("#ingredientDetails h1").innerText = ingredient.ingredient.name;
  505. document.querySelector("#ingredientStock").innerText = `${ingredient.quantity} ${ingredient.ingredient.unit}`;
  506. document.querySelector("#ingredientInput").placeholder = `${ingredient.quantity} ${ingredient.ingredient.unit}`;
  507. let quantities = [];
  508. let now = new Date();
  509. console.time("Single Ingredient Sold");
  510. for(let i = 1; i < 31; i++){
  511. let endDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i)
  512. let startDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i - 1);
  513. let indices = merchant.transactionIndices(startDay, endDay);
  514. if(indices === false){
  515. quantities.push(0);
  516. }else{
  517. quantities.push(merchant.singleIngredientSold(indices, ingredient));
  518. }
  519. }
  520. console.timeEnd("Single Ingredient Sold");
  521. let sum = 0;
  522. for(let quantity of quantities){
  523. sum += quantity;
  524. }
  525. document.querySelector("#dailyUse").innerText = `${(sum/quantities.length).toFixed(2)} ${ingredient.ingredient.unit}`;
  526. let ul = document.querySelector("#ingredientRecipeList");
  527. let recipes = merchant.getRecipesForIngredient(ingredient.ingredient);
  528. while(ul.children.length > 0){
  529. ul.removeChild(ul.firstChild);
  530. }
  531. for(let i = 0; i < recipes.length; i++){
  532. let li = document.createElement("li");
  533. li.innerText = recipes[i].name;
  534. li.onclick = ()=>{
  535. changeStrand("recipeBookStrand");
  536. recipeDetailsComp.display(recipes[i]);
  537. }
  538. ul.appendChild(li);
  539. }
  540. openSidebar(sidebar);
  541. },
  542. remove: function(){
  543. for(let i = 0; i < merchant.recipes.length; i++){
  544. for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
  545. if(this.ingredient.id === merchant.recipes[i].ingredients[j].ingredient._id){
  546. banner.createError("Must remove ingredient from all recipes before removing");
  547. return;
  548. }
  549. }
  550. }
  551. fetch(`/merchant/ingredients/remove/${this.ingredient.ingredient.id}`, {
  552. method: "DELETE",
  553. })
  554. .then((response) => response.json())
  555. .then((response)=>{
  556. if(typeof(response) === "string"){
  557. banner.createError(response);
  558. }else{
  559. banner.createNotification("Ingredient removed");
  560. merchant.editIngredients([this.ingredient], true);
  561. }
  562. })
  563. .catch((err)=>{});
  564. },
  565. edit: function(){
  566. document.querySelector("#ingredientStock").style.display = "none";
  567. document.querySelector("#ingredientInput").style.display = "block";
  568. document.querySelector("#editSubmitButton").style.display = "block";
  569. },
  570. editSubmit: function(){
  571. this.ingredient.quantity = Number(document.getElementById("ingredientInput").value);
  572. let data = [{
  573. id: this.ingredient.ingredient.id,
  574. quantity: this.ingredient.quantity
  575. }];
  576. if(validator.ingredientQuantity(data[0].quantity)){
  577. fetch("/merchant/ingredients/update", {
  578. method: "PUT",
  579. headers: {
  580. "Content-Type": "application/json;charset=utf-8"
  581. },
  582. body: JSON.stringify(data)
  583. })
  584. .then((response) => response.json())
  585. .then((response)=>{
  586. if(typeof(response) === "string"){
  587. banner.createError(response);
  588. }else{
  589. merchant.editIngredients([this.ingredient]);
  590. banner.createNotification("Ingredient updated");
  591. }
  592. })
  593. .catch((err)=>{
  594. banner.createError("Something went wrong, try refreshing the page");
  595. });
  596. }
  597. }
  598. }
  599. let newRecipeComp = {
  600. display: function(){
  601. let ingredientsSelect = document.querySelector("#recipeInputIngredients select");
  602. let categories = merchant.categorizeIngredients();
  603. for(let category of categories){
  604. let optgroup = document.createElement("optgroup");
  605. optgroup.label = category.name;
  606. ingredientsSelect.appendChild(optgroup);
  607. for(let ingredient of category.ingredients){
  608. let option = document.createElement("option");
  609. option.value = ingredient.ingredient.id;
  610. option.innerText = ingredient.ingredient.name;
  611. optgroup.appendChild(option);
  612. }
  613. }
  614. openSidebar(document.querySelector("#addRecipe"));
  615. },
  616. //Updates the number of ingredient inputs displayed for new recipes
  617. changeRecipeCount: function(){
  618. let newCount = document.querySelector("#ingredientCount").value;
  619. let ingredientsDiv = document.querySelector("#recipeInputIngredients");
  620. let oldCount = ingredientsDiv.children.length;
  621. if(newCount > oldCount){
  622. let newDivs = newCount - oldCount;
  623. for(let i = 0; i < newDivs; i++){
  624. let newNode = ingredientsDiv.children[0].cloneNode(true);
  625. newNode.children[2].children[0].value = "";
  626. ingredientsDiv.appendChild(newNode);
  627. }
  628. for(let i = 0; i < newCount; i++){
  629. ingredientsDiv.children[i].children[0].innerText = `Ingredient ${i + 1}`;
  630. }
  631. }else if(newCount < oldCount){
  632. let newDivs = oldCount - newCount;
  633. for(let i = 0; i < newDivs; i++){
  634. ingredientsDiv.removeChild(ingredientsDiv.children[ingredientsDiv.children.length-1]);
  635. }
  636. }
  637. },
  638. submit: function(){
  639. let newRecipe = {
  640. name: document.querySelector("#newRecipeName").value,
  641. price: document.querySelector("#newRecipePrice").value,
  642. ingredients: []
  643. }
  644. let inputs = document.querySelectorAll("#recipeInputIngredients > div");
  645. for(let input of inputs){
  646. newRecipe.ingredients.push({
  647. ingredient: input.children[1].children[0].value,
  648. quantity: input.children[2].children[0].value
  649. });
  650. }
  651. if(!validator.recipe(newRecipe)){
  652. return;
  653. }
  654. fetch("/recipe/create", {
  655. method: "POST",
  656. headers: {
  657. "Content-Type": "application/json;charset=utf-8"
  658. },
  659. body: JSON.stringify(newRecipe)
  660. })
  661. .then((response) => response.json())
  662. .then((response)=>{
  663. if(typeof(response) === "string"){
  664. banner.createError(response);
  665. }else{
  666. let recipe = new Recipe(
  667. response._id,
  668. response.name,
  669. response.price,
  670. response.ingredients,
  671. merchant,
  672. );
  673. merchant.editRecipe(recipe);
  674. banner.createNotification("New recipe successfully created");
  675. }
  676. })
  677. .catch((err)=>{
  678. banner.createError("Refresh page to update data");
  679. });
  680. },
  681. }