sidebars.js 41 KB

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