components.js 32 KB

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