EditUser.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <template>
  2. <div>
  3. <modal title='Edit User'>
  4. <div slot='body'>
  5. <p class="control has-addons">
  6. <input class='input is-expanded' type='text' placeholder='Username' v-model='editing.username' autofocus>
  7. <a class="button is-info" @click='updateUsername()'>Update Username</a>
  8. </p>
  9. <p class="control has-addons">
  10. <input class='input is-expanded' type='text' placeholder='Username' v-model='editing.email' autofocus>
  11. <a class="button is-info" @click='updateEmail()'>Update Email Address</a>
  12. </p>
  13. <p class="control has-addons">
  14. <span class="select">
  15. <select v-model="editing.role">
  16. <option>default</option>
  17. <option>admin</option>
  18. </select>
  19. </span>
  20. <a class="button is-info" @click='updateRole()'>Update Role</a>
  21. </p>
  22. </div>
  23. <div slot='footer'>
  24. <!--button class='button is-warning'>
  25. <span>&nbsp;Send Verification Email</span>
  26. </button>
  27. <button class='button is-warning'>
  28. <span>&nbsp;Send Password Reset Email</span>
  29. </button-->
  30. <button class='button is-danger' @click='$parent.toggleModal()'>
  31. <span>&nbsp;Close</span>
  32. </button>
  33. </div>
  34. </modal>
  35. </div>
  36. </template>
  37. <script>
  38. import io from '../../io';
  39. import { Toast } from 'vue-roaster';
  40. import Modal from './Modal.vue';
  41. import validation from '../../validation';
  42. export default {
  43. components: { Modal },
  44. data() {
  45. return {
  46. editing: {}
  47. }
  48. },
  49. methods: {
  50. updateUsername: function () {
  51. const username = this.editing.username;
  52. if (!validation.isLength(username, 2, 32)) return Toast.methods.addToast('Username must have between 2 and 32 characters.', 8000);
  53. if (!validation.regex.azAZ09_.test(username)) return Toast.methods.addToast('Invalid username format. Allowed characters: a-z, A-Z, 0-9 and _.', 8000);
  54. this.socket.emit(`users.updateUsername`, this.editing._id, username, res => {
  55. Toast.methods.addToast(res.message, 4000);
  56. });
  57. },
  58. updateEmail: function () {
  59. const email = this.editing.email;
  60. if (!validation.isLength(email, 3, 254)) return Toast.methods.addToast('Email must have between 3 and 254 characters.', 8000);
  61. if (email.indexOf('@') !== email.lastIndexOf('@') || !validation.regex.emailSimple.test(email)) return Toast.methods.addToast('Invalid email format.', 8000);
  62. this.socket.emit(`users.updateEmail`, this.editing._id, email, res => {
  63. Toast.methods.addToast(res.message, 4000);
  64. });
  65. },
  66. updateRole: function () {
  67. this.socket.emit(`users.updateRole`, this.editing._id, this.editing.role, res => {
  68. Toast.methods.addToast(res.message, 4000);
  69. if (
  70. res.status === 'success' &&
  71. this.editing.role === 'default' &&
  72. this.editing._id === this.$parent.$parent.$parent.userId
  73. ) location.reload();
  74. });
  75. }
  76. },
  77. ready: function () {
  78. let _this = this;
  79. io.getSocket(socket => _this.socket = socket );
  80. },
  81. events: {
  82. closeModal: function () {
  83. this.$parent.modals.editUser = false;
  84. },
  85. editUser: function (user) {
  86. this.editing = {
  87. _id: user._id,
  88. username: user.username,
  89. email: user.email.address,
  90. role: user.role
  91. };
  92. this.$parent.toggleModal();
  93. }
  94. }
  95. }
  96. </script>
  97. <style type='scss' scoped>
  98. .save-changes { color: #fff; }
  99. .tag:not(:last-child) { margin-right: 5px; }
  100. .select:after { border-color: #029ce3; }
  101. </style>