components.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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._id = recipe.ingredients[i].ingredient._id;
  19. ingredientDiv.name = recipe.ingredients[i].ingredient.name;
  20. ingredientList.appendChild(ingredientDiv);
  21. }
  22. document.querySelector("#addRecIng").style.display = "none";
  23. let price = document.querySelector("#recipePrice");
  24. price.children[1].style.display = "block";
  25. price.children[2].style.display = "none";
  26. price.children[1].innerText = `$${(recipe.price / 100).toFixed(2)}`;
  27. document.querySelector("#recipeUpdate").style.display = "none";
  28. },
  29. edit: function(){
  30. let ingredientDivs = document.querySelector("#recipeIngredientList");
  31. if(merchant.pos === "none"){
  32. let name = document.querySelector("#recipeName");
  33. let nameIn = document.querySelector("#recipeNameIn");
  34. name.style.display = "none";
  35. nameIn.style.display = "block";
  36. nameIn.placeholder = name.innerText;
  37. let price = document.querySelector("#recipePrice");
  38. price.children[1].style.display = "none";
  39. price.children[2].style.display = "block";
  40. price.children[2].placeholder = price.children[1].innerText;
  41. }
  42. for(let i = 0; i < ingredientDivs.children.length; i++){
  43. let div = ingredientDivs.children[i];
  44. div.children[2].innerText = this.recipe.ingredients[i].ingredient.unit;
  45. div.children[1].style.display = "block";
  46. div.children[1].placeholder = this.recipe.ingredients[i].quantity;
  47. div.children[3].style.display = "block";
  48. div.children[3].onclick = ()=>{div.parentElement.removeChild(div)};
  49. }
  50. document.querySelector("#addRecIng").style.display = "flex";
  51. document.querySelector("#recipeUpdate").style.display = "flex";
  52. },
  53. update: function(){
  54. let updatedRecipe = {
  55. _id: this.recipe._id,
  56. name: document.querySelector("#recipeNameIn").value || this.recipe.name,
  57. price: Math.round((document.querySelector("#recipePrice").children[2].value * 100)) || this.recipe.price,
  58. ingredients: []
  59. }
  60. let divs = document.querySelector("#recipeIngredientList").children;
  61. for(let i = 0; i < divs.length; i++){
  62. if(divs[i].name === "new"){
  63. updatedRecipe.ingredients.push({
  64. ingredient: divs[i].children[0].value,
  65. quantity: divs[i].children[1].value
  66. })
  67. }else{
  68. updatedRecipe.ingredients.push({
  69. ingredient: divs[i]._id,
  70. quantity: divs[i].children[1].value || divs[i].children[1].placeholder
  71. });
  72. }
  73. }
  74. if(validator.recipe(updatedRecipe)){
  75. fetch("/recipe/update", {
  76. method: "PUT",
  77. headers: {
  78. "Content-Type": "application/json;charset=utf-8"
  79. },
  80. body: JSON.stringify(updatedRecipe)
  81. })
  82. .then((response) => response.json())
  83. .then((response)=>{
  84. if(typeof(response) === "string"){
  85. banner.createError(response);
  86. }else{
  87. updateRecipes(updatedRecipe);
  88. banner.createNotification("Recipe successfully updated");
  89. }
  90. })
  91. .catch((err)=>{
  92. banner.createError("Something went wrong. Please refresh the page");
  93. })
  94. }
  95. },
  96. remove: function(){
  97. fetch(`/merchant/recipes/remove/${this.recipe._id}`, {
  98. method: "DELETE"
  99. })
  100. .then((response) => response.json())
  101. .then((response)=>{
  102. if(typeof(response) === "string"){
  103. banner.createError(response);
  104. }else{
  105. updateRecipes(this.recipe, true);
  106. banner.createNotification("Recipe removed");
  107. }
  108. })
  109. .catch((err)=>{
  110. banner.createError("Something went wrong. Try refreshing the page");
  111. });
  112. },
  113. displayAddIngredient: function(){
  114. let template = document.querySelector("#addRecIngredient").content.children[0].cloneNode(true);
  115. template.name = "new";
  116. document.querySelector("#recipeIngredientList").appendChild(template);
  117. let categories = categorizeIngredients(merchant.inventory);
  118. for(let i = 0; i < categories.length; i++){
  119. let optGroup = document.createElement("optgroup");
  120. optGroup.label = categories[i].name;
  121. template.children[0].appendChild(optGroup);
  122. for(let j = 0; j < categories[i].ingredients.length; j++){
  123. let option = document.createElement("option");
  124. option.innerText = `${categories[i].ingredients[j].name} (${categories[i].ingredients[j].unit})`;
  125. option.value = categories[i].ingredients[j].id;
  126. optGroup.appendChild(option);
  127. }
  128. }
  129. }
  130. }
  131. let newOrderComp = {
  132. isPopulated: false,
  133. display: function(){
  134. openSidebar(document.querySelector("#newOrder"));
  135. if(!this.isPopulated){
  136. let categories = categorizeIngredients(merchant.inventory);
  137. let categoriesList = document.querySelector("#newOrderCategories");
  138. let template = document.querySelector("#addIngredientsCategory").content.children[0];
  139. let ingredientTemplate = document.querySelector("#addIngredientsIngredient").content.children[0];
  140. for(let i = 0; i < categories.length; i++){
  141. let category = template.cloneNode(true);
  142. category.children[0].children[0].innerText = categories[i].name;
  143. category.children[0].children[1].onclick = ()=>{addIngredientsComp.toggleAddIngredient(category)};
  144. category.children[0].children[1].children[1].style.display = "none";
  145. category.children[1].style.display = "none";
  146. categoriesList.appendChild(category);
  147. for(let j = 0; j < categories[i].ingredients.length; j++){
  148. let ingredientDiv = ingredientTemplate.cloneNode(true);
  149. ingredientDiv.children[0].innerText = categories[i].ingredients[j].name;
  150. ingredientDiv.children[1].placeholder = categories[i].ingredients[j].unit;
  151. ingredientDiv._id = categories[i].ingredients[j].id;
  152. ingredientDiv._name = categories[i].ingredients[j].name;
  153. ingredientDiv._unit = categories[i].ingredients[j].unit;
  154. ingredientDiv._category = categories[i].name;
  155. let priceInput = document.createElement("input");
  156. priceInput.type = "number";
  157. priceInput.min = "0";
  158. priceInput.step = "0.01";
  159. priceInput.placeholder = "Price Per Unit";
  160. ingredientDiv.appendChild(priceInput);
  161. category.children[1].appendChild(ingredientDiv);
  162. }
  163. }
  164. this.isPopulated = true;
  165. }
  166. },
  167. submit: function(){
  168. let categoriesList = document.querySelector("#newOrderCategories");
  169. let newOrder = {
  170. orderId: document.querySelector("#orderName").value,
  171. date: new Date(document.querySelector("#orderDate").value),
  172. ingredients: []
  173. }
  174. for(let i = 0; i < categoriesList.children.length; i++){
  175. for(let j = 0; j < categoriesList.children[i].children[1].children.length; j++){
  176. let ingredientDiv = categoriesList.children[i].children[1].children[j];
  177. let quantity = ingredientDiv.children[1].value;
  178. let price = ingredientDiv.children[2].value;
  179. if(quantity !== "" || price !== ""){
  180. let newIngredient = {
  181. id: ingredientDiv._id,
  182. ingredient: ingredientDiv._id,
  183. quantity: parseFloat(quantity),
  184. price: parseInt(price * 100)
  185. }
  186. newOrder.ingredients.push(newIngredient);
  187. }
  188. }
  189. }
  190. if(validator.order(newOrder)){
  191. fetch("/order", {
  192. method: "POST",
  193. headers: {
  194. "Content-Type": "application/json;charset=utf-8"
  195. },
  196. body: JSON.stringify(newOrder)
  197. })
  198. .then(response => response.json())
  199. .then((response)=>{
  200. if(typeof(response) === "string"){
  201. banner.createError(response);
  202. }else{
  203. banner.createNotification("New order created");
  204. updateOrders(newOrder);
  205. updateInventory(newOrder.ingredients);
  206. }
  207. })
  208. .catch((err)=>{
  209. banner.createError("Something went wrong. Try refreshing the page");
  210. });
  211. }
  212. },
  213. }
  214. let newIngredientComp = {
  215. display: function(){
  216. openSidebar(document.querySelector("#newIngredient"));
  217. document.querySelector("#newIngName").value = "";
  218. document.querySelector("#newIngCategory").value = "";
  219. document.querySelector("#newIngQuantity").value = 0;
  220. document.querySelector("#newIngUnit").value = ""
  221. },
  222. submit: function(){
  223. let newIngredient = {
  224. ingredient: {
  225. name: document.querySelector("#newIngName").value,
  226. category: document.querySelector("#newIngCategory").value,
  227. unit: document.querySelector("#newIngUnit").value
  228. },
  229. quantity: document.querySelector("#newIngQuantity").value
  230. }
  231. if(validator.ingredient(newIngredient)){
  232. fetch("/ingredients/create", {
  233. method: "POST",
  234. headers: {
  235. "Content-Type": "application/json;charset=utf-8"
  236. },
  237. body: JSON.stringify(newIngredient)
  238. })
  239. .then((response) => response.json())
  240. .then((response)=>{
  241. if(typeof(response) === "string"){
  242. banner.createError(response);
  243. }else{
  244. updateInventory([response]);
  245. banner.createNotification("Ingredient successfully created");
  246. }
  247. })
  248. .catch((err)=>{
  249. banner.createError("Something went wrong. Try refreshing the page");
  250. });
  251. }
  252. }
  253. }
  254. let orderDetailsComp = {
  255. display: function(order){
  256. openSidebar(document.querySelector("#orderDetails"));
  257. document.querySelector("#orderDetails h1").innerText = order.orderId || order._id;
  258. document.querySelector("#orderDetails h3").innerText = new Date(order.date).toLocaleDateString("en-US");
  259. let ingredientList = document.querySelector("#orderIngredients");
  260. while(ingredientList.children.length > 0){
  261. ingredientList.removeChild(ingredientList.firstChild);
  262. }
  263. let template = document.querySelector("#orderIngredient").content.children[0];
  264. let grandTotal = 0;
  265. for(let i = 0; i < order.ingredients.length; i++){
  266. let ingredient = template.cloneNode(true);
  267. let price = (order.ingredients[i].quantity * order.ingredients[i].price) / 100;
  268. grandTotal += price;
  269. for(let j = 0; j < merchant.inventory.length; j++){
  270. if(order.ingredients[i].ingredient === merchant.inventory[j].ingredient._id){
  271. ingredient.children[0].innerText = `${merchant.inventory[j].ingredient.name}: ${order.ingredients[i].quantity}`;
  272. break;
  273. }
  274. }
  275. ingredient.children[1].innerText = `$${(order.ingredients[i].price / 100).toFixed(2)}`;
  276. ingredient.children[2].innerText = price.toFixed(2);
  277. ingredientList.appendChild(ingredient);
  278. }
  279. document.querySelector("#orderTotalPrice p").innerText = `$${grandTotal.toFixed(2)}`;
  280. }
  281. }
  282. let addIngredientsComp = {
  283. isPopulated: false,
  284. display: function(){
  285. let sidebar = document.querySelector("#addIngredients");
  286. if(!this.isPopulated){
  287. let addIngredientsDiv = document.getElementById("addIngredientList");
  288. let categoryTemplate = document.getElementById("addIngredientsCategory");
  289. let ingredientTemplate = document.getElementById("addIngredientsIngredient");
  290. fetch("/ingredients")
  291. .then((response) => response.json())
  292. .then((response)=>{
  293. if(typeof(response) === "string"){
  294. banner.createError(response);
  295. }else{
  296. for(let i = 0; i < merchant.inventory.length; i++){
  297. for(let j = 0; j < response.length; j++){
  298. if(merchant.inventory[i].ingredient._id === response[j]._id){
  299. response.splice(j, 1);
  300. break;
  301. }
  302. }
  303. }
  304. let categories = categorizeIngredientsFromDB(response);
  305. while(addIngredientsDiv.children.length > 0){
  306. addIngredientsDiv.removeChild(addIngredientsDiv.firstChild);
  307. }
  308. this.addIngredientsDiv = [];
  309. for(let i = 0; i < categories.length; i++){
  310. let categoryDiv = categoryTemplate.content.children[0].cloneNode(true);
  311. categoryDiv.children[0].children[0].innerText = categories[i].name;
  312. categoryDiv.children[0].children[1].onclick = ()=>{addIngredientsComp.toggleAddIngredient(categoryDiv)};
  313. categoryDiv.children[1].style.display = "none";
  314. categoryDiv.children[0].children[1].children[1].style.display = "none";
  315. addIngredientsDiv.appendChild(categoryDiv);
  316. for(let j = 0; j < categories[i].ingredients.length; j++){
  317. let ingredientDiv = ingredientTemplate.content.children[0].cloneNode(true);
  318. ingredientDiv.children[0].innerText = categories[i].ingredients[j].name;
  319. ingredientDiv._id = categories[i].ingredients[j].id;
  320. ingredientDiv._name = categories[i].ingredients[j].name;
  321. ingredientDiv._unit = categories[i].ingredients[j].unit;
  322. ingredientDiv._category = categories[i].name;
  323. categoryDiv.children[1].appendChild(ingredientDiv);
  324. this.addIngredientsDiv.push(ingredientDiv);
  325. }
  326. }
  327. }
  328. })
  329. .catch((err)=>{
  330. banner.createError("Unable to retrieve data");
  331. });
  332. this.isPopulated = true;
  333. }
  334. openSidebar(sidebar);
  335. },
  336. toggleAddIngredient: function(categoryElement){
  337. let button = categoryElement.children[0].children[1];
  338. let ingredientDisplay = categoryElement.children[1];
  339. if(ingredientDisplay.style.display === "none"){
  340. ingredientDisplay.style.display = "flex";
  341. button.children[0].style.display = "none";
  342. button.children[1].style.display = "block";
  343. }else{
  344. ingredientDisplay.style.display = "none";
  345. button.children[0].style.display = "block";
  346. button.children[1].style.display = "none";
  347. }
  348. },
  349. submitAddIngredients: function(){
  350. let addIngredients = [];
  351. for(let i = 0; i < this.addIngredientsDiv.length; i++){
  352. let ingredient = this.addIngredientsDiv[i];
  353. if(ingredient.children[1].value !== ""){
  354. if(!validator.ingredientQuantity(ingredient.children[1].value)){
  355. return;
  356. }
  357. addIngredients.push({
  358. ingredient: {
  359. _id: ingredient._id,
  360. name: ingredient._name,
  361. category: ingredient._category,
  362. unit: ingredient._unit
  363. },
  364. quantity: ingredient.children[1].value,
  365. });
  366. }
  367. }
  368. if(addIngredients.length > 0){
  369. fetch("/merchant/ingredients/add", {
  370. method: "PUT",
  371. headers: {
  372. "Content-Type": "application/json;charset=utf-8"
  373. },
  374. body: JSON.stringify(addIngredients)
  375. })
  376. .then((response) => response.json())
  377. .then((response)=>{
  378. if(typeof(response) === "string"){
  379. banner.createError(response);
  380. }else{
  381. banner.createNotification("Ingredients added");
  382. updateInventory(addIngredients);
  383. }
  384. })
  385. .catch((err)=>{
  386. banner.createError("Unable to update data. Please refresh the page");
  387. });
  388. }
  389. }
  390. }
  391. let ingredientDetailsComp = {
  392. ingredient: {},
  393. display: function(ingredient, category){
  394. this.ingredient = ingredient;
  395. sidebar = document.querySelector("#ingredientDetails");
  396. openSidebar(sidebar);
  397. document.querySelector("#ingredientDetails p").innerText = category.name;
  398. document.querySelector("#ingredientDetails h1").innerText = ingredient.name;
  399. document.querySelector("#ingredientStock").innerText = `${ingredient.quantity} ${ingredient.unit}`;
  400. document.querySelector("#ingredientInput").placeholder = `${ingredient.quantity} ${ingredient.unit}`;
  401. let quantities = [];
  402. let now = new Date();
  403. for(let i = 1; i < 31; i++){
  404. let endDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i)
  405. let startDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i - 1);
  406. quantities.push(ingredientSold(dateIndices(startDay, endDay), ingredient.id));
  407. }
  408. let sum = 0;
  409. for(let quantity of quantities){
  410. sum += quantity;
  411. }
  412. document.querySelector("#dailyUse").innerText = `${(sum/quantities.length).toFixed(2)} ${ingredient.unit}`;
  413. let ul = document.querySelector("#ingredientRecipeList");
  414. let recipes = recipesForIngredient(ingredient.id);
  415. while(ul.children.length > 0){
  416. ul.removeChild(ul.firstChild);
  417. }
  418. for(let i = 0; i < recipes.length; i++){
  419. let li = document.createElement("li");
  420. console.log(recipes[i]);
  421. li.innerText = recipes[i].name;
  422. li.onclick = ()=>{
  423. changeStrand("recipeBookStrand");
  424. recipeDetailsComp.display(recipes[i]);
  425. }
  426. ul.appendChild(li);
  427. }
  428. },
  429. remove: function(){
  430. for(let i = 0; i < merchant.recipes.length; i++){
  431. for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
  432. if(this.ingredient.id === merchant.recipes[i].ingredients[j].ingredient._id){
  433. banner.createError("Must remove ingredient from all recipes before removing");
  434. return;
  435. }
  436. }
  437. }
  438. fetch(`/merchant/ingredients/remove/${this.ingredient.id}`, {
  439. method: "DELETE",
  440. })
  441. .then((response) => response.json())
  442. .then((response)=>{
  443. if(typeof(response) === "string"){
  444. banner.createError(response);
  445. }else{
  446. banner.createNotification("Ingredient removed");
  447. updateInventory([this.ingredient], true);
  448. }
  449. })
  450. .catch((err)=>{});
  451. },
  452. edit: function(){
  453. document.querySelector("#ingredientStock").style.display = "none";
  454. document.querySelector("#ingredientInput").style.display = "block";
  455. document.querySelector("#editSubmitButton").style.display = "block";
  456. },
  457. editSubmit: function(){
  458. let data = [{
  459. id: this.ingredient.id,
  460. quantity: Number(document.querySelector("#ingredientInput").value)
  461. }];
  462. if(validator.ingredientQuantity(data[0].quantity)){
  463. fetch("/merchant/ingredients/update", {
  464. method: "PUT",
  465. headers: {
  466. "Content-Type": "application/json;charset=utf-8"
  467. },
  468. body: JSON.stringify(data)
  469. })
  470. .then((response) => response.json())
  471. .then((response)=>{
  472. if(typeof(response) === "string"){
  473. banner.createError(response);
  474. }else{
  475. updateInventory(data);
  476. banner.createNotification("Ingredient updated");
  477. }
  478. })
  479. .catch((err)=>{
  480. banner.createError("Something went wrong, try refreshing the page");
  481. });
  482. }
  483. }
  484. }