account.js 3.6 KB

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