components.js 32 KB

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