UIWindowChangeUsername.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import UIWindow from './UIWindow.js'
  2. import update_username_in_gui from '../helpers/update_username_in_gui.js'
  3. async function UIWindowChangeUsername(){
  4. const internal_id = window.uuidv4();
  5. let h = '';
  6. h += `<div class="change-username" style="padding: 20px; border-bottom: 1px solid #ced7e1;">`;
  7. // error msg
  8. h += `<div class="form-error-msg"></div>`;
  9. // success msg
  10. h += `<div class="form-success-msg"></div>`;
  11. // new username
  12. h += `<div style="overflow: hidden; margin-top: 10px; margin-bottom: 30px;">`;
  13. h += `<label for="confirm-new-username-${internal_id}">New Username</label>`;
  14. h += `<input id="confirm-new-username-${internal_id}" type="text" name="new-username" class="new-username" autocomplete="off" />`;
  15. h += `</div>`;
  16. // Change Username
  17. h += `<button class="change-username-btn button button-primary button-block button-normal">Change Username</button>`;
  18. h += `</div>`;
  19. const el_window = await UIWindow({
  20. title: 'Change Username',
  21. app: 'change-username',
  22. single_instance: true,
  23. icon: null,
  24. uid: null,
  25. is_dir: false,
  26. body_content: h,
  27. draggable_body: false,
  28. has_head: true,
  29. selectable_body: false,
  30. draggable_body: false,
  31. allow_context_menu: false,
  32. is_resizable: false,
  33. is_droppable: false,
  34. init_center: true,
  35. allow_native_ctxmenu: false,
  36. allow_user_select: false,
  37. width: 350,
  38. height: 'auto',
  39. dominant: true,
  40. show_in_taskbar: false,
  41. onAppend: function(this_window){
  42. $(this_window).find(`.new-username`).get(0)?.focus({preventScroll:true});
  43. },
  44. window_class: 'window-publishWebsite',
  45. body_css: {
  46. width: 'initial',
  47. height: '100%',
  48. 'background-color': 'rgb(245 247 249)',
  49. 'backdrop-filter': 'blur(3px)',
  50. }
  51. })
  52. $(el_window).find('.change-username-btn').on('click', function(e){
  53. // hide previous error/success msg
  54. $(el_window).find('.form-success-msg, .form-success-msg').hide();
  55. const new_username = $(el_window).find('.new-username').val();
  56. if(!new_username){
  57. $(el_window).find('.form-error-msg').html('All fields are required.');
  58. $(el_window).find('.form-error-msg').fadeIn();
  59. return;
  60. }
  61. $(el_window).find('.form-error-msg').hide();
  62. // disable button
  63. $(el_window).find('.change-username-btn').addClass('disabled');
  64. // disable input
  65. $(el_window).find('.new-username').attr('disabled', true);
  66. $.ajax({
  67. url: api_origin + "/change_username",
  68. type: 'POST',
  69. async: true,
  70. headers: {
  71. "Authorization": "Bearer "+auth_token
  72. },
  73. contentType: "application/json",
  74. data: JSON.stringify({
  75. new_username: new_username,
  76. }),
  77. success: function (data){
  78. $(el_window).find('.form-success-msg').html('Username updated successfully.');
  79. $(el_window).find('.form-success-msg').fadeIn();
  80. $(el_window).find('input').val('');
  81. // update auth data
  82. update_username_in_gui(new_username);
  83. // update username
  84. window.user.username = new_username;
  85. // enable button
  86. $(el_window).find('.change-username-btn').removeClass('disabled');
  87. // enable input
  88. $(el_window).find('.new-username').attr('disabled', false);
  89. },
  90. error: function (err){
  91. $(el_window).find('.form-error-msg').html(html_encode(err.responseJSON?.message));
  92. $(el_window).find('.form-error-msg').fadeIn();
  93. // enable button
  94. $(el_window).find('.change-username-btn').removeClass('disabled');
  95. // enable input
  96. $(el_window).find('.new-username').attr('disabled', false);
  97. }
  98. });
  99. })
  100. }
  101. export default UIWindowChangeUsername