sidebars.js 39 KB

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