UIWindowChangeUsername.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * Copyright (C) 2024 Puter Technologies Inc.
  3. *
  4. * This file is part of Puter.
  5. *
  6. * Puter is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published
  8. * by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. import UIWindow from './UIWindow.js'
  20. import update_username_in_gui from '../helpers/update_username_in_gui.js'
  21. async function UIWindowChangeUsername(){
  22. const internal_id = window.uuidv4();
  23. let h = '';
  24. h += `<div class="change-username" style="padding: 20px; border-bottom: 1px solid #ced7e1;">`;
  25. // error msg
  26. h += `<div class="form-error-msg"></div>`;
  27. // success msg
  28. h += `<div class="form-success-msg"></div>`;
  29. // new username
  30. h += `<div style="overflow: hidden; margin-top: 10px; margin-bottom: 30px;">`;
  31. h += `<label for="confirm-new-username-${internal_id}">New Username</label>`;
  32. h += `<input id="confirm-new-username-${internal_id}" type="text" name="new-username" class="new-username" autocomplete="off" />`;
  33. h += `</div>`;
  34. // Change Username
  35. h += `<button class="change-username-btn button button-primary button-block button-normal">Change Username</button>`;
  36. h += `</div>`;
  37. const el_window = await UIWindow({
  38. title: 'Change Username',
  39. app: 'change-username',
  40. single_instance: true,
  41. icon: null,
  42. uid: null,
  43. is_dir: false,
  44. body_content: h,
  45. draggable_body: false,
  46. has_head: true,
  47. selectable_body: false,
  48. draggable_body: false,
  49. allow_context_menu: false,
  50. is_resizable: false,
  51. is_droppable: false,
  52. init_center: true,
  53. allow_native_ctxmenu: false,
  54. allow_user_select: false,
  55. width: 350,
  56. height: 'auto',
  57. dominant: true,
  58. show_in_taskbar: false,
  59. onAppend: function(this_window){
  60. $(this_window).find(`.new-username`).get(0)?.focus({preventScroll:true});
  61. },
  62. window_class: 'window-publishWebsite',
  63. body_css: {
  64. width: 'initial',
  65. height: '100%',
  66. 'background-color': 'rgb(245 247 249)',
  67. 'backdrop-filter': 'blur(3px)',
  68. }
  69. })
  70. $(el_window).find('.change-username-btn').on('click', function(e){
  71. // hide previous error/success msg
  72. $(el_window).find('.form-success-msg, .form-success-msg').hide();
  73. const new_username = $(el_window).find('.new-username').val();
  74. if(!new_username){
  75. $(el_window).find('.form-error-msg').html('All fields are required.');
  76. $(el_window).find('.form-error-msg').fadeIn();
  77. return;
  78. }
  79. $(el_window).find('.form-error-msg').hide();
  80. // disable button
  81. $(el_window).find('.change-username-btn').addClass('disabled');
  82. // disable input
  83. $(el_window).find('.new-username').attr('disabled', true);
  84. $.ajax({
  85. url: api_origin + "/change_username",
  86. type: 'POST',
  87. async: true,
  88. headers: {
  89. "Authorization": "Bearer "+auth_token
  90. },
  91. contentType: "application/json",
  92. data: JSON.stringify({
  93. new_username: new_username,
  94. }),
  95. success: function (data){
  96. $(el_window).find('.form-success-msg').html('Username updated successfully.');
  97. $(el_window).find('.form-success-msg').fadeIn();
  98. $(el_window).find('input').val('');
  99. // update auth data
  100. update_username_in_gui(new_username);
  101. // update username
  102. window.user.username = new_username;
  103. // enable button
  104. $(el_window).find('.change-username-btn').removeClass('disabled');
  105. // enable input
  106. $(el_window).find('.new-username').attr('disabled', false);
  107. },
  108. error: function (err){
  109. $(el_window).find('.form-error-msg').html(html_encode(err.responseJSON?.message));
  110. $(el_window).find('.form-error-msg').fadeIn();
  111. // enable button
  112. $(el_window).find('.change-username-btn').removeClass('disabled');
  113. // enable input
  114. $(el_window).find('.new-username').attr('disabled', false);
  115. }
  116. });
  117. })
  118. }
  119. export default UIWindowChangeUsername