sidebars.js 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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.value = this.recipe.name;
  37. let price = document.querySelector("#recipePrice");
  38. price.children[1].style.display = "none";
  39. price.children[2].style.display = "block";
  40. price.children[2].value = parseFloat((this.recipe.price / 100).toFixed(2));
  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].value = parseFloat(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
  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.editRecipes([this.recipe]);
  99. banner.createNotification("RECIPE UPDATE");
  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.editRecipes([this.recipe], true);
  119. banner.createNotification("RECIPE REMOVED");
  120. }
  121. })
  122. .catch((err)=>{
  123. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH 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. name: 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.name,
  242. response.date,
  243. response.ingredients,
  244. merchant
  245. )
  246. merchant.editOrders([order]);
  247. merchant.editIngredients(order.ingredients, false, true);
  248. banner.createNotification("ORDER CREATED");
  249. }
  250. })
  251. .catch((err)=>{
  252. banner.createError("SOEMTHING WENT WRONG. PLEASE REFRESH 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. },
  266. submit: function(){
  267. let unitSelector = document.getElementById("unitSelector");
  268. let options = document.querySelectorAll("#unitSelector option");
  269. let newIngredient = {
  270. ingredient: {
  271. name: document.getElementById("newIngName").value,
  272. category: document.getElementById("newIngCategory").value,
  273. unitType: options[unitSelector.selectedIndex].getAttribute("type"),
  274. },
  275. quantity: document.querySelector("#newIngQuantity").value,
  276. defaultUnit: unitSelector.value
  277. }
  278. let loader = document.getElementById("loaderContainer");
  279. loader.style.display = "flex";
  280. fetch("/ingredients/create", {
  281. method: "POST",
  282. headers: {
  283. "Content-Type": "application/json;charset=utf-8"
  284. },
  285. body: JSON.stringify(newIngredient)
  286. })
  287. .then((response) => response.json())
  288. .then((response)=>{
  289. if(typeof(response) === "string"){
  290. banner.createError(response);
  291. }else{
  292. merchant.editIngredients([{
  293. ingredient: new Ingredient(
  294. response.ingredient._id,
  295. response.ingredient.name,
  296. response.ingredient.category,
  297. response.ingredient.unitType,
  298. response.defaultUnit,
  299. merchant
  300. ),
  301. quantity: response.quantity
  302. }]);
  303. banner.createNotification("INGREDIENT CREATED");
  304. }
  305. })
  306. .catch((err)=>{
  307. console.log(err);
  308. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  309. })
  310. .finally(()=>{
  311. loader.style.display = "none";
  312. });
  313. }
  314. }
  315. let orderDetailsComp = {
  316. display: function(order){
  317. openSidebar(document.querySelector("#orderDetails"));
  318. document.querySelector("#removeOrderBtn").onclick = ()=>{this.remove(order)};
  319. document.querySelector("#orderDetails h1").innerText = order.name;
  320. document.querySelector("#orderDetails h3").innerText = order.date.toLocaleDateString("en-US");
  321. let ingredientList = document.querySelector("#orderIngredients");
  322. while(ingredientList.children.length > 0){
  323. ingredientList.removeChild(ingredientList.firstChild);
  324. }
  325. let template = document.querySelector("#orderIngredient").content.children[0];
  326. let grandTotal = 0;
  327. for(let i = 0; i < order.ingredients.length; i++){
  328. let ingredient = template.cloneNode(true);
  329. let price = (order.ingredients[i].quantity * order.ingredients[i].price) / 100;
  330. grandTotal += price;
  331. ingredient.children[0].innerText = order.ingredients[i].ingredient.name;
  332. ingredient.children[1].innerText = `${order.ingredients[i].quantity} x $${(order.ingredients[i].price / 100).toFixed(2)}`;
  333. ingredient.children[2].innerText = `$${price.toFixed(2)}`;
  334. ingredientList.appendChild(ingredient);
  335. }
  336. document.querySelector("#orderTotalPrice p").innerText = `$${grandTotal.toFixed(2)}`;
  337. },
  338. remove: function(order){
  339. let loader = document.getElementById("loaderContainer");
  340. loader.style.display = "flex";
  341. fetch(`/order/${order.id}`, {
  342. method: "DELETE",
  343. headers: {
  344. "Content-Type": "application/json;charset=utf-8"
  345. }
  346. })
  347. .then((response) => response.json())
  348. .then((response)=>{
  349. if(typeof(response) === "string"){
  350. banner.createError(response);
  351. }else{
  352. merchant.editOrders([order], true);
  353. banner.createNotification("ORDER REMOVED");
  354. }
  355. })
  356. .catch((err)=>{
  357. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  358. })
  359. .finally(()=>{
  360. loader.style.display = "none";
  361. });
  362. }
  363. }
  364. let addIngredientsComp = {
  365. isPopulated: false,
  366. fakeMerchant: {},
  367. chosenIngredients: [],
  368. display: function(){
  369. let sidebar = document.querySelector("#addIngredients");
  370. if(!this.isPopulated){
  371. let loader = document.getElementById("loaderContainer");
  372. loader.style.display = "flex";
  373. fetch("/ingredients")
  374. .then((response) => response.json())
  375. .then((response)=>{
  376. if(typeof(response) === "string"){
  377. banner.createError(response);
  378. }else{
  379. for(let i = 0; i < merchant.ingredients.length; i++){
  380. for(let j = 0; j < response.length; j++){
  381. if(merchant.ingredients[i].ingredient.id === response[j]._id){
  382. response.splice(j, 1);
  383. break;
  384. }
  385. }
  386. }
  387. for(let i = 0; i < response.length; i++){
  388. response[i] = {ingredient: response[i]}
  389. }
  390. this.fakeMerchant = new Merchant(
  391. {
  392. name: "none",
  393. inventory: response,
  394. recipes: [],
  395. },
  396. []
  397. );
  398. this.populateAddIngredients();
  399. }
  400. })
  401. .catch((err)=>{
  402. banner.createError("UNABLE TO RETRIEVE DATA");
  403. })
  404. .finally(()=>{
  405. loader.style.display = "none";
  406. });
  407. this.isPopulated = true;
  408. }
  409. openSidebar(sidebar);
  410. },
  411. populateAddIngredients: function(){
  412. let addIngredientsDiv = document.getElementById("addIngredientList");
  413. let categoryTemplate = document.getElementById("addIngredientsCategory");
  414. let ingredientTemplate = document.getElementById("addIngredientsIngredient");
  415. let categories = this.fakeMerchant.categorizeIngredients();
  416. while(addIngredientsDiv.children.length > 0){
  417. addIngredientsDiv.removeChild(addIngredientsDiv.firstChild);
  418. }
  419. for(let i = 0; i < categories.length; i++){
  420. let categoryDiv = categoryTemplate.content.children[0].cloneNode(true);
  421. categoryDiv.children[0].children[0].innerText = categories[i].name;
  422. categoryDiv.children[0].children[1].onclick = ()=>{addIngredientsComp.toggleAddIngredient(categoryDiv)};
  423. categoryDiv.children[1].style.display = "none";
  424. categoryDiv.children[0].children[1].children[1].style.display = "none";
  425. addIngredientsDiv.appendChild(categoryDiv);
  426. for(let j = 0; j < categories[i].ingredients.length; j++){
  427. let ingredientDiv = ingredientTemplate.content.children[0].cloneNode(true);
  428. ingredientDiv.children[0].innerText = categories[i].ingredients[j].ingredient.name;
  429. ingredientDiv.children[1].onclick = ()=>{this.addOne(ingredientDiv)};
  430. ingredientDiv.ingredient = categories[i].ingredients[j].ingredient;
  431. categoryDiv.children[1].appendChild(ingredientDiv);
  432. }
  433. }
  434. },
  435. toggleAddIngredient: function(categoryElement){
  436. let button = categoryElement.children[0].children[1];
  437. let ingredientDisplay = categoryElement.children[1];
  438. if(ingredientDisplay.style.display === "none"){
  439. ingredientDisplay.style.display = "flex";
  440. button.children[0].style.display = "none";
  441. button.children[1].style.display = "block";
  442. }else{
  443. ingredientDisplay.style.display = "none";
  444. button.children[0].style.display = "block";
  445. button.children[1].style.display = "none";
  446. }
  447. },
  448. addOne: function(element){
  449. element.parentElement.removeChild(element);
  450. document.getElementById("myIngredients").appendChild(element);
  451. document.getElementById("myIngredientsDiv").style.display = "flex";
  452. for(let i = 0; i < this.fakeMerchant.ingredients.length; i++){
  453. if(this.fakeMerchant.ingredients[i].ingredient === element.ingredient){
  454. this.fakeMerchant.ingredients.splice(i, 1);
  455. this.chosenIngredients.push(element.ingredient);
  456. break;
  457. }
  458. }
  459. let input = document.createElement("input");
  460. input.type = "number";
  461. input.min = "0";
  462. input.step = "0.01";
  463. input.placeholder = element._unit;
  464. element.insertBefore(input, element.children[1]);
  465. element.children[2].innerText = "-";
  466. element.children[2].onclick = ()=>{this.removeOne(element)};
  467. },
  468. removeOne: function(element){
  469. element.parentElement.removeChild(element);
  470. element.removeChild(element.children[1]);
  471. element.children[1].innerText = "+";
  472. element.children[1].onclick = ()=>{this.addOne(element)};
  473. if(document.getElementById("myIngredients").children.length === 0){
  474. document.getElementById("myIngredientsDiv").style.display = "none";
  475. }
  476. for(let i = 0; i < this.chosenIngredients.length; i++){
  477. if(this.chosenIngredients[i] === element.ingredient){
  478. this.chosenIngredients.splice(i, 1);
  479. this.fakeMerchant.ingredients.push({
  480. ingredient: element.ingredient
  481. });
  482. break;
  483. }
  484. }
  485. this.populateAddIngredients();
  486. },
  487. submit: function(){
  488. let ingredients = document.getElementById("myIngredients").children;
  489. let newIngredients = [];
  490. let fetchable = [];
  491. for(let i = 0; i < ingredients.length; i++){
  492. if(ingredients[i].children[1].value === ""){
  493. banner.createError("PLEASE ENTER A QUANTITY FOR EACH INGREDIENT YOU WANT TO ADD TO YOUR INVENTORY");
  494. return;
  495. }
  496. newIngredients.push({
  497. ingredient: ingredients[i].ingredient,
  498. quantity: ingredients[i].children[1].value
  499. });
  500. fetchable.push({
  501. id: ingredients[i].ingredient.id,
  502. quantity: ingredients[i].children[1].value
  503. });
  504. }
  505. let loader = document.getElementById("loaderContainer");
  506. loader.style.display = "flex";
  507. fetch("/merchant/ingredients/add", {
  508. method: "POST",
  509. headers: {
  510. "Content-Type": "application/json;charset=utf-8"
  511. },
  512. body: JSON.stringify(fetchable)
  513. })
  514. .then((response) => response.json())
  515. .then((response)=>{
  516. if(typeof(response) === "string"){
  517. banner.createError(response);
  518. }else{
  519. merchant.editIngredients(newIngredients);
  520. this.isPopulated = false;
  521. banner.createNotification("ALL INGREDIENTS ADDED");
  522. }
  523. })
  524. .catch((err)=>{
  525. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  526. })
  527. .finally(()=>{
  528. loader.style.display = "none";
  529. });
  530. }
  531. }
  532. let ingredientDetailsComp = {
  533. ingredient: {},
  534. display: function(ingredient){
  535. this.ingredient = ingredient;
  536. sidebar = document.querySelector("#ingredientDetails");
  537. document.querySelector("#ingredientDetails p").innerText = ingredient.ingredient.category;
  538. document.querySelector("#ingredientDetails h1").innerText = ingredient.ingredient.name;
  539. let ingredientStock = document.getElementById("ingredientStock");
  540. ingredientStock.innerText = `${ingredient.quantity} ${ingredient.ingredient.unit}`;
  541. ingredientStock.style.display = "block";
  542. let ingredientInput = document.getElementById("ingredientInput");
  543. ingredientInput.placeholder = `${ingredient.quantity} ${ingredient.ingredient.unit}`;
  544. ingredientInput.style.display = "none";
  545. let quantities = [];
  546. let now = new Date();
  547. for(let i = 1; i < 31; i++){
  548. let endDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i)
  549. let startDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i - 1);
  550. let indices = merchant.transactionIndices(startDay, endDay);
  551. if(indices === false){
  552. quantities.push(0);
  553. }else{
  554. quantities.push(merchant.singleIngredientSold(indices, ingredient));
  555. }
  556. }
  557. let sum = 0;
  558. for(let quantity of quantities){
  559. sum += quantity;
  560. }
  561. document.querySelector("#dailyUse").innerText = `${(sum/quantities.length).toFixed(2)} ${ingredient.ingredient.unit}`;
  562. let ul = document.querySelector("#ingredientRecipeList");
  563. let recipes = merchant.getRecipesForIngredient(ingredient.ingredient);
  564. while(ul.children.length > 0){
  565. ul.removeChild(ul.firstChild);
  566. }
  567. for(let i = 0; i < recipes.length; i++){
  568. let li = document.createElement("li");
  569. li.innerText = recipes[i].name;
  570. li.onclick = ()=>{
  571. changeStrand("recipeBookStrand");
  572. recipeDetailsComp.display(recipes[i]);
  573. }
  574. ul.appendChild(li);
  575. }
  576. openSidebar(sidebar);
  577. },
  578. remove: function(){
  579. for(let i = 0; i < merchant.recipes.length; i++){
  580. for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
  581. if(this.ingredient.ingredient === merchant.recipes[i].ingredients[j].ingredient){
  582. banner.createError("MUST REMOVE INGREDIENT FROM ALL RECIPES BEFORE REMOVING FROM INVENTORY");
  583. return;
  584. }
  585. }
  586. }
  587. let loader = document.getElementById("loaderContainer");
  588. loader.style.display = "flex";
  589. fetch(`/merchant/ingredients/remove/${this.ingredient.ingredient.id}`, {
  590. method: "DELETE",
  591. })
  592. .then((response) => response.json())
  593. .then((response)=>{
  594. if(typeof(response) === "string"){
  595. banner.createError(response);
  596. }else{
  597. banner.createNotification("INGREDIENT REMOVED");
  598. merchant.editIngredients([this.ingredient], true);
  599. }
  600. })
  601. .catch((err)=>{})
  602. .finally(()=>{
  603. loader.style.display = "none";
  604. });
  605. },
  606. edit: function(){
  607. document.querySelector("#ingredientStock").style.display = "none";
  608. document.querySelector("#ingredientInput").style.display = "block";
  609. document.querySelector("#editSubmitButton").style.display = "block";
  610. },
  611. editSubmit: function(){
  612. this.ingredient.quantity = Number(document.getElementById("ingredientInput").value);
  613. let data = [{
  614. id: this.ingredient.ingredient.id,
  615. quantity: this.ingredient.quantity
  616. }];
  617. let loader = document.getElementById("loaderContainer");
  618. loader.style.display = "flex";
  619. if(validator.ingredientQuantity(data[0].quantity)){
  620. fetch("/merchant/ingredients/update", {
  621. method: "PUT",
  622. headers: {
  623. "Content-Type": "application/json;charset=utf-8"
  624. },
  625. body: JSON.stringify(data)
  626. })
  627. .then((response) => response.json())
  628. .then((response)=>{
  629. if(typeof(response) === "string"){
  630. banner.createError(response);
  631. }else{
  632. merchant.editIngredients([this.ingredient]);
  633. banner.createNotification("INGREDIENT UPDATED");
  634. }
  635. })
  636. .catch((err)=>{
  637. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  638. })
  639. .finally(()=>{
  640. loader.style.display = "none";
  641. });
  642. }
  643. }
  644. }
  645. let newRecipeComp = {
  646. display: function(){
  647. let ingredientsSelect = document.querySelector("#recipeInputIngredients select");
  648. let categories = merchant.categorizeIngredients();
  649. while(ingredientsSelect.children.length > 0){
  650. ingredientsSelect.removeChild(ingredientsSelect.firstChild);
  651. }
  652. for(let category of categories){
  653. let optgroup = document.createElement("optgroup");
  654. optgroup.label = category.name;
  655. ingredientsSelect.appendChild(optgroup);
  656. for(let ingredient of category.ingredients){
  657. let option = document.createElement("option");
  658. option.value = ingredient.ingredient.id;
  659. option.innerText = ingredient.ingredient.name;
  660. optgroup.appendChild(option);
  661. }
  662. }
  663. openSidebar(document.querySelector("#addRecipe"));
  664. },
  665. //Updates the number of ingredient inputs displayed for new recipes
  666. changeRecipeCount: function(){
  667. let newCount = document.querySelector("#ingredientCount").value;
  668. let ingredientsDiv = document.querySelector("#recipeInputIngredients");
  669. let oldCount = ingredientsDiv.children.length;
  670. if(newCount > oldCount){
  671. let newDivs = newCount - oldCount;
  672. for(let i = 0; i < newDivs; i++){
  673. let newNode = ingredientsDiv.children[0].cloneNode(true);
  674. newNode.children[2].children[0].value = "";
  675. ingredientsDiv.appendChild(newNode);
  676. }
  677. for(let i = 0; i < newCount; i++){
  678. ingredientsDiv.children[i].children[0].innerText = `INGREDIENT ${i + 1}`;
  679. }
  680. }else if(newCount < oldCount){
  681. let newDivs = oldCount - newCount;
  682. for(let i = 0; i < newDivs; i++){
  683. ingredientsDiv.removeChild(ingredientsDiv.children[ingredientsDiv.children.length-1]);
  684. }
  685. }
  686. },
  687. submit: function(){
  688. let newRecipe = {
  689. name: document.querySelector("#newRecipeName").value,
  690. price: document.querySelector("#newRecipePrice").value,
  691. ingredients: []
  692. }
  693. let inputs = document.querySelectorAll("#recipeInputIngredients > div");
  694. for(let input of inputs){
  695. newRecipe.ingredients.push({
  696. ingredient: input.children[1].children[0].value,
  697. quantity: input.children[2].children[0].value
  698. });
  699. }
  700. if(!validator.recipe(newRecipe)){
  701. return;
  702. }
  703. let loader = document.getElementById("loaderContainer");
  704. loader.style.display = "flex";
  705. fetch("/recipe/create", {
  706. method: "POST",
  707. headers: {
  708. "Content-Type": "application/json;charset=utf-8"
  709. },
  710. body: JSON.stringify(newRecipe)
  711. })
  712. .then((response) => response.json())
  713. .then((response)=>{
  714. if(typeof(response) === "string"){
  715. banner.createError(response);
  716. }else{
  717. let recipe = new Recipe(
  718. response._id,
  719. response.name,
  720. response.price,
  721. response.ingredients,
  722. merchant,
  723. );
  724. merchant.editRecipes([recipe]);
  725. banner.createNotification("RECIPE CREATED");
  726. }
  727. })
  728. .catch((err)=>{
  729. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  730. })
  731. .finally(()=>{
  732. loader.style.display = "none";
  733. });
  734. },
  735. }
  736. let transactionDetailsComp = {
  737. transaction: {},
  738. display: function(transaction){
  739. this.transaction = transaction;
  740. let recipeList = document.getElementById("transactionRecipes");
  741. let template = document.getElementById("transactionRecipe").content.children[0];
  742. let totalRecipes = 0;
  743. let totalPrice = 0;
  744. while(recipeList.children.length > 0){
  745. recipeList.removeChild(recipeList.firstChild);
  746. }
  747. for(let i = 0; i < transaction.recipes.length; i++){
  748. let recipe = template.cloneNode(true);
  749. let price = transaction.recipes[i].quantity * transaction.recipes[i].recipe.price;
  750. recipe.children[0].innerText = transaction.recipes[i].recipe.name;
  751. recipe.children[1].innerText = `${transaction.recipes[i].quantity} x $${parseFloat(transaction.recipes[i].recipe.price / 100).toFixed(2)}`;
  752. recipe.children[2].innerText = `$${(price / 100).toFixed(2)}`;
  753. recipeList.appendChild(recipe);
  754. totalRecipes += transaction.recipes[i].quantity;
  755. totalPrice += price;
  756. }
  757. let months = ["January", "Fecbruary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  758. let days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
  759. let dateString = `${days[transaction.date.getDay()]}, ${months[transaction.date.getMonth()]} ${transaction.date.getDate()}, ${transaction.date.getFullYear()}`;
  760. document.getElementById("transactionDate").innerText = dateString;
  761. document.getElementById("transactionTime").innerText = transaction.date.toLocaleTimeString();
  762. document.getElementById("totalRecipes").innerText = `${totalRecipes} recipes`;
  763. document.getElementById("totalPrice").innerText = `$${(totalPrice / 100).toFixed(2)}`;
  764. openSidebar(document.getElementById("transactionDetails"));
  765. },
  766. remove: function(){
  767. let loader = document.getElementById("loaderContainer");
  768. loader.style.display = "flex";
  769. fetch(`/transaction/${this.transaction.id}`, {
  770. method: "delete",
  771. headers: {
  772. "Content-Type": "application/json;charset=utf-8"
  773. },
  774. })
  775. .then(response => response.json())
  776. .then((response)=>{
  777. if(typeof(response) === "string"){
  778. banner.createError(response);
  779. }else{
  780. merchant.editTransactions(this.transaction, true);
  781. banner.createNotification("TRANSACTION REMOVED");
  782. }
  783. })
  784. .catch((err)=>{
  785. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  786. })
  787. .finally(()=>{
  788. loader.style.display = "none";
  789. });
  790. },
  791. }
  792. let newTransactionComp = {
  793. display: function(){
  794. let recipeList = document.getElementById("newTransactionRecipes");
  795. let template = document.getElementById("createTransaction").content.children[0];
  796. while(recipeList.children.length > 0){
  797. recipeList.removeChild(recipeList.firstChild);
  798. }
  799. for(let i = 0; i < merchant.recipes.length; i++){
  800. let recipeDiv = template.cloneNode(true);
  801. recipeDiv.recipe = merchant.recipes[i];
  802. recipeList.appendChild(recipeDiv);
  803. recipeDiv.children[0].innerText = merchant.recipes[i].name;
  804. }
  805. openSidebar(document.getElementById("newTransaction"));
  806. },
  807. submit: function(){
  808. let recipeDivs = document.getElementById("newTransactionRecipes");
  809. let date = document.getElementById("newTransactionDate").valueAsDate;
  810. if(date > new Date()){
  811. banner.createError("CANNOT HAVE A DATE IN THE FUTURE");
  812. return;
  813. }
  814. let newTransaction = {
  815. date: date,
  816. recipes: []
  817. };
  818. for(let i = 0; i < recipeDivs.children.length; i++){
  819. let quantity = recipeDivs.children[i].children[1].value;
  820. if(quantity !== "" && quantity > 0){
  821. newTransaction.recipes.push({
  822. recipe: recipeDivs.children[i].recipe.id,
  823. quantity: quantity
  824. });
  825. }else if(quantity < 0){
  826. banner.createError("CANNOT HAVE NEGATIVE VALUES");
  827. return;
  828. }
  829. }
  830. if(newTransaction.recipes.length > 0){
  831. let loader = document.getElementById("loaderContainer");
  832. loader.style.display = "flex";
  833. fetch("/transaction", {
  834. method: "post",
  835. headers: {
  836. "Content-Type": "application/json;charset=utf-8"
  837. },
  838. body: JSON.stringify(newTransaction)
  839. })
  840. .then(response => response.json())
  841. .then((response)=>{
  842. if(typeof(response) === "string"){
  843. banner.createError(response);
  844. }else{
  845. let transaction = new Transaction(
  846. response._id,
  847. response.date,
  848. response.recipes,
  849. merchant
  850. );
  851. merchant.editTransactions(transaction);
  852. banner.createNotification("NEW TRANSACTION CREATED");
  853. }
  854. })
  855. .catch((err)=>{
  856. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  857. })
  858. .finally(()=>{
  859. loader.style.display = "none";
  860. });
  861. }
  862. }
  863. }