components.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. for(let i = 0; i < order.ingredients.length; i++){
  265. let ingredient = template.cloneNode(true);
  266. for(let j = 0; j < merchant.inventory.length; j++){
  267. if(order.ingredients[i].ingredient === merchant.inventory[j].ingredient._id){
  268. ingredient.children[0].innerText = `${merchant.inventory[j].ingredient.name}: ${order.ingredients[i].quantity}`;
  269. break;
  270. }
  271. }
  272. ingredient.children[1].innerText = `$${(order.ingredients[i].price / 100).toFixed(2)}`;
  273. ingredient.children[2].innerText = ((order.ingredients[i].quantity * order.ingredients[i].price) / 100).toFixed(2);
  274. ingredientList.appendChild(ingredient);
  275. }
  276. }
  277. }
  278. let addIngredientsComp = {
  279. isPopulated: false,
  280. display: function(){
  281. let sidebar = document.querySelector("#addIngredients");
  282. if(!this.isPopulated){
  283. let addIngredientsDiv = document.getElementById("addIngredientList");
  284. let categoryTemplate = document.getElementById("addIngredientsCategory");
  285. let ingredientTemplate = document.getElementById("addIngredientsIngredient");
  286. fetch("/ingredients")
  287. .then((response) => response.json())
  288. .then((response)=>{
  289. if(typeof(response) === "string"){
  290. banner.createError(response);
  291. }else{
  292. for(let i = 0; i < merchant.inventory.length; i++){
  293. for(let j = 0; j < response.length; j++){
  294. if(merchant.inventory[i].ingredient._id === response[j]._id){
  295. response.splice(j, 1);
  296. break;
  297. }
  298. }
  299. }
  300. let categories = categorizeIngredientsFromDB(response);
  301. while(addIngredientsDiv.children.length > 0){
  302. addIngredientsDiv.removeChild(addIngredientsDiv.firstChild);
  303. }
  304. this.addIngredientsDiv = [];
  305. for(let i = 0; i < categories.length; i++){
  306. let categoryDiv = categoryTemplate.content.children[0].cloneNode(true);
  307. categoryDiv.children[0].children[0].innerText = categories[i].name;
  308. categoryDiv.children[0].children[1].onclick = ()=>{addIngredientsComp.toggleAddIngredient(categoryDiv)};
  309. categoryDiv.children[1].style.display = "none";
  310. categoryDiv.children[0].children[1].children[1].style.display = "none";
  311. addIngredientsDiv.appendChild(categoryDiv);
  312. for(let j = 0; j < categories[i].ingredients.length; j++){
  313. let ingredientDiv = ingredientTemplate.content.children[0].cloneNode(true);
  314. ingredientDiv.children[0].innerText = categories[i].ingredients[j].name;
  315. ingredientDiv._id = categories[i].ingredients[j].id;
  316. ingredientDiv._name = categories[i].ingredients[j].name;
  317. ingredientDiv._unit = categories[i].ingredients[j].unit;
  318. ingredientDiv._category = categories[i].name;
  319. categoryDiv.children[1].appendChild(ingredientDiv);
  320. this.addIngredientsDiv.push(ingredientDiv);
  321. }
  322. }
  323. }
  324. })
  325. .catch((err)=>{
  326. banner.createError("Unable to retrieve data");
  327. });
  328. this.isPopulated = true;
  329. }
  330. openSidebar(sidebar);
  331. },
  332. toggleAddIngredient: function(categoryElement){
  333. let button = categoryElement.children[0].children[1];
  334. let ingredientDisplay = categoryElement.children[1];
  335. if(ingredientDisplay.style.display === "none"){
  336. ingredientDisplay.style.display = "flex";
  337. button.children[0].style.display = "none";
  338. button.children[1].style.display = "block";
  339. }else{
  340. ingredientDisplay.style.display = "none";
  341. button.children[0].style.display = "block";
  342. button.children[1].style.display = "none";
  343. }
  344. },
  345. submitAddIngredients: function(){
  346. let addIngredients = [];
  347. for(let i = 0; i < this.addIngredientsDiv.length; i++){
  348. let ingredient = this.addIngredientsDiv[i];
  349. if(ingredient.children[1].value !== ""){
  350. if(!validator.ingredientQuantity(ingredient.children[1].value)){
  351. return;
  352. }
  353. addIngredients.push({
  354. ingredient: {
  355. _id: ingredient._id,
  356. name: ingredient._name,
  357. category: ingredient._category,
  358. unit: ingredient._unit
  359. },
  360. quantity: ingredient.children[1].value,
  361. });
  362. }
  363. }
  364. if(addIngredients.length > 0){
  365. fetch("/merchant/ingredients/add", {
  366. method: "PUT",
  367. headers: {
  368. "Content-Type": "application/json;charset=utf-8"
  369. },
  370. body: JSON.stringify(addIngredients)
  371. })
  372. .then((response) => response.json())
  373. .then((response)=>{
  374. if(typeof(response) === "string"){
  375. banner.createError(response);
  376. }else{
  377. banner.createNotification("Ingredients added");
  378. updateInventory(addIngredients);
  379. }
  380. })
  381. .catch((err)=>{
  382. banner.createError("Unable to update data. Please refresh the page");
  383. });
  384. }
  385. }
  386. }