transactionDetails.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. let transactionDetails = {
  2. transaction: {},
  3. display: function(transaction){
  4. this.transaction = transaction;
  5. let recipeList = document.getElementById("transactionRecipes");
  6. let template = document.getElementById("transactionRecipe").content.children[0];
  7. let totalRecipes = 0;
  8. let totalPrice = 0;
  9. while(recipeList.children.length > 0){
  10. recipeList.removeChild(recipeList.firstChild);
  11. }
  12. for(let i = 0; i < transaction.recipes.length; i++){
  13. let recipe = template.cloneNode(true);
  14. let price = transaction.recipes[i].quantity * transaction.recipes[i].recipe.price;
  15. recipe.children[0].innerText = transaction.recipes[i].recipe.name;
  16. recipe.children[1].innerText = `${transaction.recipes[i].quantity} x $${transaction.recipes[i].recipe.price.toFixed(2)}`;
  17. recipe.children[2].innerText = `$${price.toFixed(2)}`;
  18. recipe.onclick = ()=>{
  19. controller.openStrand("recipeBook");
  20. controller.openSidebar("recipeDetails", transaction.recipes[i].recipe);
  21. }
  22. recipeList.appendChild(recipe);
  23. totalRecipes += transaction.recipes[i].quantity;
  24. totalPrice += price;
  25. }
  26. let months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  27. let days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
  28. let dateString = `${days[transaction.date.getDay()]}, ${months[transaction.date.getMonth()]} ${transaction.date.getDate()}, ${transaction.date.getFullYear()}`;
  29. document.getElementById("transactionDate").innerText = dateString;
  30. document.getElementById("totalRecipes").innerText = `${totalRecipes} recipes`;
  31. document.getElementById("totalPrice").innerText = `$${totalPrice.toFixed(2)}`;
  32. if(merchant.pos === "none"){
  33. document.getElementById("removeTransBtn").onclick = ()=>{this.remove()};
  34. }
  35. },
  36. remove: function(){
  37. let loader = document.getElementById("loaderContainer");
  38. loader.style.display = "flex";
  39. fetch(`/transaction/${this.transaction.id}`, {
  40. method: "delete",
  41. headers: {
  42. "Content-Type": "application/json;charset=utf-8"
  43. },
  44. })
  45. .then((response)=>{
  46. if(typeof(response) === "string"){
  47. controller.createBanner(response, "error");
  48. }else{
  49. merchant.removeTransaction(this.transaction);
  50. controller.updateAnalytics();
  51. controller.openStrand("transactions", merchant.getTransactions());
  52. controller.createBanner("TRANSACTION REMOVED", "success");
  53. }
  54. })
  55. .catch((err)=>{
  56. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  57. })
  58. .finally(()=>{
  59. loader.style.display = "none";
  60. });
  61. },
  62. }
  63. module.exports = transactionDetails;