account.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. let account = {
  2. display: function(){
  3. document.getElementById("accountStrandTitle").innerText = merchant.name;
  4. document.getElementById("accountEmail").value = merchant.email;
  5. document.getElementById("accountUpdate").onclick = ()=>{this.updateData()};
  6. //Display alternate locations
  7. document.getElementById("settingsAddMerchant").onclick = ()=>{controller.openModal("newMerchant")};
  8. //Handle the password changey stuffs
  9. let passButton = document.getElementById("accountShowPassword");
  10. let passBox = document.getElementById("changePasswordBox");
  11. passButton.onclick = ()=>{
  12. passButton.style.display = "none";
  13. passBox.style.display = "flex";
  14. };
  15. document.getElementById("cancelPasswordChange").onclick = ()=>{
  16. passButton.style.display = "block";
  17. passBox.style.display = "none";
  18. document.getElementById("accountCurrentPassword").value = "";
  19. document.getElementById("accountNewPassword").value = "";
  20. document.getElementById("accountConfirmPassword").value = "";
  21. };
  22. document.getElementById("changePasswordButton").onclick = ()=>{this.updatePassword()};
  23. },
  24. updateData: function(){
  25. let data = {
  26. email: document.getElementById("accountEmail").value
  27. }
  28. let loader = document.getElementById("loaderContainer");
  29. loader.style.display = "flex";
  30. fetch("/merchant/update", {
  31. method: "put",
  32. headers: {
  33. "Content-Type": "application/json;charset=utf-8"
  34. },
  35. body: JSON.stringify(data)
  36. })
  37. .then(response => response.json())
  38. .then((response)=>{
  39. if(typeof(response) === "string"){
  40. controller.createBanner(response, "error");
  41. }else{
  42. controller.createBanner("DATA UPDATED", "success");
  43. if(response.email !== merchant.email){
  44. controller.createBanner("YOU MUST VALIDATE YOUR NEW EMAIL ADDRESS BEFORE YOU CAN LOG IN AGAIN", "alert");
  45. }
  46. merchant.email = response.email;
  47. document.getElementById("accountEmail").value = merchant.email;
  48. }
  49. })
  50. .catch((err)=>{
  51. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  52. })
  53. .finally(()=>{
  54. loader.style.display = "none";
  55. });
  56. },
  57. updatePassword: function(){
  58. let data = {
  59. current: document.getElementById("accountCurrentPassword").value,
  60. new: document.getElementById("accountNewPassword").value,
  61. confirm: document.getElementById("accountConfirmPassword").value
  62. }
  63. if(data.new !== data.confirm){
  64. return controller.createBanner("PASSWORDS DO NOT MATCH");
  65. }
  66. let loader = document.getElementById("loaderContainer");
  67. loader.style.display = "flex";
  68. fetch("/merchant/password", {
  69. method: "put",
  70. headers: {
  71. "Content-Type": "application/json;charset=utf-8"
  72. },
  73. body: JSON.stringify(data)
  74. })
  75. .then(response => response.json())
  76. .then((response)=>{
  77. if(typeof(response) === "string"){
  78. controller.createBanner(response, "error");
  79. }else{
  80. window.location.href = response.redirect;
  81. }
  82. })
  83. .catch((err)=>{
  84. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  85. })
  86. .finally(()=>{
  87. loader.style.display = "none";
  88. });
  89. }
  90. }
  91. module.exports = account;