sidebars.js 41 KB

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