components.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  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. console.log(err);
  302. banner.createError("Something went wrong. Try refreshing the page");
  303. });
  304. // }
  305. }
  306. }
  307. let orderDetailsComp = {
  308. display: function(order){
  309. openSidebar(document.querySelector("#orderDetails"));
  310. document.querySelector("#removeOrderBtn").onclick = ()=>{this.remove(order)};
  311. document.querySelector("#orderDetails h1").innerText = order.name;
  312. document.querySelector("#orderDetails h3").innerText = order.date.toLocaleDateString("en-US");
  313. let ingredientList = document.querySelector("#orderIngredients");
  314. while(ingredientList.children.length > 0){
  315. ingredientList.removeChild(ingredientList.firstChild);
  316. }
  317. let template = document.querySelector("#orderIngredient").content.children[0];
  318. let grandTotal = 0;
  319. for(let i = 0; i < order.ingredients.length; i++){
  320. let ingredient = template.cloneNode(true);
  321. let price = (order.ingredients[i].quantity * order.ingredients[i].price) / 100;
  322. grandTotal += price;
  323. ingredient.children[0].innerText = order.ingredients[i].ingredient.name;
  324. ingredient.children[1].innerText = `${order.ingredients[i].quantity} x $${(order.ingredients[i].price / 100).toFixed(2)}`;
  325. ingredient.children[2].innerText = `$${price.toFixed(2)}`;
  326. ingredientList.appendChild(ingredient);
  327. }
  328. document.querySelector("#orderTotalPrice p").innerText = `$${grandTotal.toFixed(2)}`;
  329. },
  330. remove: function(order){
  331. let loader = document.getElementById("loaderContainer");
  332. loader.style.display = "flex";
  333. fetch(`/order/${order.id}`, {
  334. method: "DELETE",
  335. headers: {
  336. "Content-Type": "application/json;charset=utf-8"
  337. }
  338. })
  339. .then((response) => response.json())
  340. .then((response)=>{
  341. loader.style.display = "none";
  342. if(typeof(response) === "string"){
  343. banner.createError(response);
  344. }else{
  345. merchant.editOrders([order], true);
  346. banner.createNotification("Order successfully removed");
  347. }
  348. })
  349. .catch((err)=>{
  350. banner.createError("Something went wrong, try refreshing the page");
  351. });
  352. }
  353. }
  354. let addIngredientsComp = {
  355. isPopulated: false,
  356. fakeMerchant: {},
  357. chosenIngredients: [],
  358. display: function(){
  359. let sidebar = document.querySelector("#addIngredients");
  360. if(!this.isPopulated){
  361. let loader = document.getElementById("loaderContainer");
  362. loader.style.display = "flex";
  363. fetch("/ingredients")
  364. .then((response) => response.json())
  365. .then((response)=>{
  366. loader.style.display = "none";
  367. if(typeof(response) === "string"){
  368. banner.createError(response);
  369. }else{
  370. for(let i = 0; i < merchant.ingredients.length; i++){
  371. for(let j = 0; j < response.length; j++){
  372. if(merchant.ingredients[i].ingredient.id === response[j]._id){
  373. response.splice(j, 1);
  374. break;
  375. }
  376. }
  377. }
  378. for(let i = 0; i < response.length; i++){
  379. response[i] = {ingredient: response[i]}
  380. }
  381. this.fakeMerchant = new Merchant(
  382. {
  383. name: "none",
  384. inventory: response,
  385. recipes: [],
  386. },
  387. []
  388. );
  389. this.populateAddIngredients();
  390. }
  391. })
  392. .catch((err)=>{
  393. banner.createError("Unable to retrieve data");
  394. });
  395. this.isPopulated = true;
  396. }
  397. openSidebar(sidebar);
  398. },
  399. populateAddIngredients: function(){
  400. let addIngredientsDiv = document.getElementById("addIngredientList");
  401. let categoryTemplate = document.getElementById("addIngredientsCategory");
  402. let ingredientTemplate = document.getElementById("addIngredientsIngredient");
  403. let categories = this.fakeMerchant.categorizeIngredients();
  404. while(addIngredientsDiv.children.length > 0){
  405. addIngredientsDiv.removeChild(addIngredientsDiv.firstChild);
  406. }
  407. for(let i = 0; i < categories.length; i++){
  408. let categoryDiv = categoryTemplate.content.children[0].cloneNode(true);
  409. categoryDiv.children[0].children[0].innerText = categories[i].name;
  410. categoryDiv.children[0].children[1].onclick = ()=>{addIngredientsComp.toggleAddIngredient(categoryDiv)};
  411. categoryDiv.children[1].style.display = "none";
  412. categoryDiv.children[0].children[1].children[1].style.display = "none";
  413. addIngredientsDiv.appendChild(categoryDiv);
  414. for(let j = 0; j < categories[i].ingredients.length; j++){
  415. let ingredientDiv = ingredientTemplate.content.children[0].cloneNode(true);
  416. ingredientDiv.children[0].innerText = categories[i].ingredients[j].ingredient.name;
  417. ingredientDiv.children[1].onclick = ()=>{this.addOne(ingredientDiv)};
  418. ingredientDiv.ingredient = categories[i].ingredients[j].ingredient;
  419. categoryDiv.children[1].appendChild(ingredientDiv);
  420. }
  421. }
  422. },
  423. toggleAddIngredient: function(categoryElement){
  424. let button = categoryElement.children[0].children[1];
  425. let ingredientDisplay = categoryElement.children[1];
  426. if(ingredientDisplay.style.display === "none"){
  427. ingredientDisplay.style.display = "flex";
  428. button.children[0].style.display = "none";
  429. button.children[1].style.display = "block";
  430. }else{
  431. ingredientDisplay.style.display = "none";
  432. button.children[0].style.display = "block";
  433. button.children[1].style.display = "none";
  434. }
  435. },
  436. addOne: function(element){
  437. element.parentElement.removeChild(element);
  438. document.getElementById("myIngredients").appendChild(element);
  439. document.getElementById("myIngredientsDiv").style.display = "flex";
  440. for(let i = 0; i < this.fakeMerchant.ingredients.length; i++){
  441. if(this.fakeMerchant.ingredients[i].ingredient === element.ingredient){
  442. this.fakeMerchant.ingredients.splice(i, 1);
  443. this.chosenIngredients.push(element.ingredient);
  444. break;
  445. }
  446. }
  447. let input = document.createElement("input");
  448. input.type = "number";
  449. input.min = "0";
  450. input.step = "0.01";
  451. input.placeholder = element._unit;
  452. element.insertBefore(input, element.children[1]);
  453. element.children[2].innerText = "-";
  454. element.children[2].onclick = ()=>{this.removeOne(element)};
  455. },
  456. removeOne: function(element){
  457. element.parentElement.removeChild(element);
  458. element.removeChild(element.children[1]);
  459. element.children[1].innerText = "+";
  460. element.children[1].onclick = ()=>{this.addOne(element)};
  461. if(document.getElementById("myIngredients").children.length === 0){
  462. document.getElementById("myIngredientsDiv").style.display = "none";
  463. }
  464. for(let i = 0; i < this.chosenIngredients.length; i++){
  465. if(this.chosenIngredients[i] === element.ingredient){
  466. this.chosenIngredients.splice(i, 1);
  467. this.fakeMerchant.ingredients.push({
  468. ingredient: element.ingredient
  469. });
  470. break;
  471. }
  472. }
  473. this.populateAddIngredients();
  474. },
  475. submit: function(){
  476. let ingredients = document.getElementById("myIngredients").children;
  477. let newIngredients = [];
  478. let fetchable = [];
  479. for(let i = 0; i < ingredients.length; i++){
  480. if(ingredients[i].children[1].value === ""){
  481. banner.createError("Please enter a quantity for each ingredient you want to add to your inventory");
  482. return;
  483. }
  484. newIngredients.push({
  485. ingredient: ingredients[i].ingredient,
  486. quantity: ingredients[i].children[1].value
  487. });
  488. fetchable.push({
  489. id: ingredients[i].ingredient.id,
  490. quantity: ingredients[i].children[1].value
  491. });
  492. }
  493. let loader = document.getElementById("loaderContainer");
  494. loader.style.display = "flex";
  495. fetch("/merchant/ingredients/add", {
  496. method: "POST",
  497. headers: {
  498. "Content-Type": "application/json;charset=utf-8"
  499. },
  500. body: JSON.stringify(fetchable)
  501. })
  502. .then((response)=>{
  503. loader.style.display = "none";
  504. if(typeof(response) === "string"){
  505. banner.createError(response);
  506. }else{
  507. merchant.editIngredients(newIngredients);
  508. banner.createNotification("All ingredients added successfully");
  509. }
  510. })
  511. .catch((err)=>{
  512. banner.createError("Something went wrong. Try refreshing the page");
  513. });
  514. }
  515. }
  516. let ingredientDetailsComp = {
  517. ingredient: {},
  518. display: function(ingredient){
  519. this.ingredient = ingredient;
  520. sidebar = document.querySelector("#ingredientDetails");
  521. document.querySelector("#ingredientDetails p").innerText = ingredient.ingredient.category;
  522. document.querySelector("#ingredientDetails h1").innerText = ingredient.ingredient.name;
  523. document.querySelector("#ingredientStock").innerText = `${ingredient.quantity} ${ingredient.ingredient.unit}`;
  524. document.querySelector("#ingredientInput").placeholder = `${ingredient.quantity} ${ingredient.ingredient.unit}`;
  525. let quantities = [];
  526. let now = new Date();
  527. for(let i = 1; i < 31; i++){
  528. let endDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i)
  529. let startDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i - 1);
  530. let indices = merchant.transactionIndices(startDay, endDay);
  531. if(indices === false){
  532. quantities.push(0);
  533. }else{
  534. quantities.push(merchant.singleIngredientSold(indices, ingredient));
  535. }
  536. }
  537. let sum = 0;
  538. for(let quantity of quantities){
  539. sum += quantity;
  540. }
  541. document.querySelector("#dailyUse").innerText = `${(sum/quantities.length).toFixed(2)} ${ingredient.ingredient.unit}`;
  542. let ul = document.querySelector("#ingredientRecipeList");
  543. let recipes = merchant.getRecipesForIngredient(ingredient.ingredient);
  544. while(ul.children.length > 0){
  545. ul.removeChild(ul.firstChild);
  546. }
  547. for(let i = 0; i < recipes.length; i++){
  548. let li = document.createElement("li");
  549. li.innerText = recipes[i].name;
  550. li.onclick = ()=>{
  551. changeStrand("recipeBookStrand");
  552. recipeDetailsComp.display(recipes[i]);
  553. }
  554. ul.appendChild(li);
  555. }
  556. openSidebar(sidebar);
  557. },
  558. remove: function(){
  559. for(let i = 0; i < merchant.recipes.length; i++){
  560. for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
  561. if(this.ingredient.id === merchant.recipes[i].ingredients[j].ingredient._id){
  562. banner.createError("Must remove ingredient from all recipes before removing");
  563. return;
  564. }
  565. }
  566. }
  567. let loader = document.getElementById("loaderContainer");
  568. loader.style.display = "flex";
  569. fetch(`/merchant/ingredients/remove/${this.ingredient.ingredient.id}`, {
  570. method: "DELETE",
  571. })
  572. .then((response) => response.json())
  573. .then((response)=>{
  574. loader.style.display = "none";
  575. if(typeof(response) === "string"){
  576. banner.createError(response);
  577. }else{
  578. banner.createNotification("Ingredient removed");
  579. merchant.editIngredients([this.ingredient], true);
  580. }
  581. })
  582. .catch((err)=>{});
  583. },
  584. edit: function(){
  585. document.querySelector("#ingredientStock").style.display = "none";
  586. document.querySelector("#ingredientInput").style.display = "block";
  587. document.querySelector("#editSubmitButton").style.display = "block";
  588. },
  589. editSubmit: function(){
  590. this.ingredient.quantity = Number(document.getElementById("ingredientInput").value);
  591. let data = [{
  592. id: this.ingredient.ingredient.id,
  593. quantity: this.ingredient.quantity
  594. }];
  595. let loader = document.getElementById("loaderContainer");
  596. loader.style.display = "flex";
  597. if(validator.ingredientQuantity(data[0].quantity)){
  598. fetch("/merchant/ingredients/update", {
  599. method: "PUT",
  600. headers: {
  601. "Content-Type": "application/json;charset=utf-8"
  602. },
  603. body: JSON.stringify(data)
  604. })
  605. .then((response) => response.json())
  606. .then((response)=>{
  607. loader.style.display = "none";
  608. if(typeof(response) === "string"){
  609. banner.createError(response);
  610. }else{
  611. merchant.editIngredients([this.ingredient]);
  612. banner.createNotification("Ingredient updated");
  613. }
  614. })
  615. .catch((err)=>{
  616. banner.createError("Something went wrong, try refreshing the page");
  617. });
  618. }
  619. }
  620. }
  621. let newRecipeComp = {
  622. display: function(){
  623. let ingredientsSelect = document.querySelector("#recipeInputIngredients select");
  624. let categories = merchant.categorizeIngredients();
  625. for(let category of categories){
  626. let optgroup = document.createElement("optgroup");
  627. optgroup.label = category.name;
  628. ingredientsSelect.appendChild(optgroup);
  629. for(let ingredient of category.ingredients){
  630. let option = document.createElement("option");
  631. option.value = ingredient.ingredient.id;
  632. option.innerText = ingredient.ingredient.name;
  633. optgroup.appendChild(option);
  634. }
  635. }
  636. openSidebar(document.querySelector("#addRecipe"));
  637. },
  638. //Updates the number of ingredient inputs displayed for new recipes
  639. changeRecipeCount: function(){
  640. let newCount = document.querySelector("#ingredientCount").value;
  641. let ingredientsDiv = document.querySelector("#recipeInputIngredients");
  642. let oldCount = ingredientsDiv.children.length;
  643. if(newCount > oldCount){
  644. let newDivs = newCount - oldCount;
  645. for(let i = 0; i < newDivs; i++){
  646. let newNode = ingredientsDiv.children[0].cloneNode(true);
  647. newNode.children[2].children[0].value = "";
  648. ingredientsDiv.appendChild(newNode);
  649. }
  650. for(let i = 0; i < newCount; i++){
  651. ingredientsDiv.children[i].children[0].innerText = `Ingredient ${i + 1}`;
  652. }
  653. }else if(newCount < oldCount){
  654. let newDivs = oldCount - newCount;
  655. for(let i = 0; i < newDivs; i++){
  656. ingredientsDiv.removeChild(ingredientsDiv.children[ingredientsDiv.children.length-1]);
  657. }
  658. }
  659. },
  660. submit: function(){
  661. let newRecipe = {
  662. name: document.querySelector("#newRecipeName").value,
  663. price: document.querySelector("#newRecipePrice").value,
  664. ingredients: []
  665. }
  666. let inputs = document.querySelectorAll("#recipeInputIngredients > div");
  667. for(let input of inputs){
  668. newRecipe.ingredients.push({
  669. ingredient: input.children[1].children[0].value,
  670. quantity: input.children[2].children[0].value
  671. });
  672. }
  673. if(!validator.recipe(newRecipe)){
  674. return;
  675. }
  676. let loader = document.getElementById("loaderContainer");
  677. loader.style.display = "flex";
  678. fetch("/recipe/create", {
  679. method: "POST",
  680. headers: {
  681. "Content-Type": "application/json;charset=utf-8"
  682. },
  683. body: JSON.stringify(newRecipe)
  684. })
  685. .then((response) => response.json())
  686. .then((response)=>{
  687. loader.style.display = "none";
  688. if(typeof(response) === "string"){
  689. banner.createError(response);
  690. }else{
  691. let recipe = new Recipe(
  692. response._id,
  693. response.name,
  694. response.price,
  695. response.ingredients,
  696. merchant,
  697. );
  698. merchant.editRecipe(recipe);
  699. banner.createNotification("New recipe successfully created");
  700. }
  701. })
  702. .catch((err)=>{
  703. banner.createError("Refresh page to update data");
  704. });
  705. },
  706. }