transactionDetails.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. recipeList.appendChild(recipe);
  19. totalRecipes += transaction.recipes[i].quantity;
  20. totalPrice += price;
  21. }
  22. let months = ["January", "Fecbruary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  23. let days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
  24. let dateString = `${days[transaction.date.getDay()]}, ${months[transaction.date.getMonth()]} ${transaction.date.getDate()}, ${transaction.date.getFullYear()}`;
  25. document.getElementById("transactionDate").innerText = dateString;
  26. document.getElementById("transactionTime").innerText = transaction.date.toLocaleTimeString();
  27. document.getElementById("totalRecipes").innerText = `${totalRecipes} recipes`;
  28. document.getElementById("totalPrice").innerText = `$${totalPrice.toFixed(2)}`;
  29. if(merchant.pos === "none"){
  30. document.getElementById("removeTransBtn").onclick = ()=>{this.remove()};
  31. }
  32. },
  33. remove: function(){
  34. let loader = document.getElementById("loaderContainer");
  35. loader.style.display = "flex";
  36. fetch(`/transaction/${this.transaction.id}`, {
  37. method: "delete",
  38. headers: {
  39. "Content-Type": "application/json;charset=utf-8"
  40. },
  41. })
  42. .then(response => response.json())
  43. .then((response)=>{
  44. if(typeof(response) === "string"){
  45. banner.createError(response);
  46. }else{
  47. merchant.removeTransaction(this.transaction);
  48. controller.openStrand("transactions");
  49. banner.createNotification("TRANSACTION REMOVED");
  50. }
  51. })
  52. .catch((err)=>{
  53. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  54. })
  55. .finally(()=>{
  56. loader.style.display = "none";
  57. });
  58. },
  59. }
  60. module.exports = transactionDetails;