Account.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <template>
  2. <div class="content account-tab">
  3. <h4 class="section-title">Change account details</h4>
  4. <p class="section-description">Keep these details up-to-date.</p>
  5. <hr class="section-horizontal-rule" />
  6. <p class="control is-expanded margin-top-zero">
  7. <label for="username">Username</label>
  8. <input
  9. class="input"
  10. id="username"
  11. type="text"
  12. placeholder="Enter username here..."
  13. v-model="modifiedUser.username"
  14. maxlength="32"
  15. autocomplete="off"
  16. @keypress="onInput('username')"
  17. @paste="onInput('username')"
  18. />
  19. <span v-if="modifiedUser.username" class="character-counter"
  20. >{{ modifiedUser.username.length }}/32</span
  21. >
  22. </p>
  23. <transition name="fadein-helpbox">
  24. <input-help-box
  25. :entered="validation.username.entered"
  26. :valid="validation.username.valid"
  27. :message="validation.username.message"
  28. />
  29. </transition>
  30. <p class="control is-expanded">
  31. <label for="email">Email</label>
  32. <input
  33. class="input"
  34. id="email"
  35. type="text"
  36. placeholder="Enter email address here..."
  37. v-if="modifiedUser.email"
  38. v-model="modifiedUser.email.address"
  39. @keypress="onInput('email')"
  40. @paste="onInput('email')"
  41. autocomplete="off"
  42. />
  43. </p>
  44. <transition name="fadein-helpbox">
  45. <input-help-box
  46. :entered="validation.email.entered"
  47. :valid="validation.email.valid"
  48. :message="validation.email.message"
  49. />
  50. </transition>
  51. <save-button ref="saveButton" @clicked="saveChanges()" />
  52. <!-- <div class="section-margin-bottom" />
  53. <h4 class="section-title">Export my data</h4>
  54. <p class="section-description">
  55. Download a copy of all data we store on you in JSON format.
  56. </p>
  57. <hr class="section-horizontal-rule" /> -->
  58. <div class="section-margin-bottom" />
  59. <h4 class="section-title">Remove any data we hold on you</h4>
  60. <p class="section-description">
  61. Permanently remove your account and/or data we store on you.
  62. </p>
  63. <hr class="section-horizontal-rule" />
  64. <div class="row">
  65. <confirm @confirm="removeActivities()">
  66. <a class="button is-warning">
  67. <i class="material-icons icon-with-button">cancel</i>
  68. Clear my activities
  69. </a>
  70. </confirm>
  71. <a class="button is-danger" @click="openModal('removeAccount')">
  72. <i class="material-icons icon-with-button">delete</i>
  73. Remove my account
  74. </a>
  75. </div>
  76. </div>
  77. </template>
  78. <script>
  79. import { mapState, mapActions, mapGetters } from "vuex";
  80. import Toast from "toasters";
  81. import InputHelpBox from "@/components/InputHelpBox.vue";
  82. import SaveButton from "@/components/SaveButton.vue";
  83. import validation from "@/validation";
  84. import Confirm from "@/components/Confirm.vue";
  85. export default {
  86. components: {
  87. InputHelpBox,
  88. SaveButton,
  89. Confirm
  90. },
  91. data() {
  92. return {
  93. validation: {
  94. username: {
  95. entered: false,
  96. valid: false,
  97. message: "Please enter a valid username."
  98. },
  99. email: {
  100. entered: false,
  101. valid: false,
  102. message: "Please enter a valid email address."
  103. }
  104. }
  105. };
  106. },
  107. computed: {
  108. ...mapState({
  109. userId: state => state.user.auth.userId,
  110. originalUser: state => state.settings.originalUser,
  111. modifiedUser: state => state.settings.modifiedUser
  112. }),
  113. ...mapGetters({
  114. socket: "websockets/getSocket"
  115. })
  116. },
  117. watch: {
  118. // prettier-ignore
  119. // eslint-disable-next-line func-names
  120. "modifiedUser.username": function (value) {
  121. if (!validation.isLength(value, 2, 32)) {
  122. this.validation.username.message =
  123. "Username must have between 2 and 32 characters.";
  124. this.validation.username.valid = false;
  125. } else if (
  126. !validation.regex.azAZ09_.test(value) &&
  127. value !== this.originalUser.username // Sometimes a username pulled from GitHub won't succeed validation
  128. ) {
  129. this.validation.username.message =
  130. "Invalid format. Allowed characters: a-z, A-Z, 0-9 and _.";
  131. this.validation.username.valid = false;
  132. } else {
  133. this.validation.username.message = "Everything looks great!";
  134. this.validation.username.valid = true;
  135. }
  136. },
  137. // prettier-ignore
  138. // eslint-disable-next-line func-names
  139. "modifiedUser.email.address": function (value) {
  140. if (!validation.isLength(value, 3, 254)) {
  141. this.validation.email.message =
  142. "Email must have between 3 and 254 characters.";
  143. this.validation.email.valid = false;
  144. } else if (
  145. value.indexOf("@") !== value.lastIndexOf("@") ||
  146. !validation.regex.emailSimple.test(value)
  147. ) {
  148. this.validation.email.message = "Invalid format.";
  149. this.validation.email.valid = false;
  150. } else {
  151. this.validation.email.message = "Everything looks great!";
  152. this.validation.email.valid = true;
  153. }
  154. }
  155. },
  156. methods: {
  157. onInput(inputName) {
  158. this.validation[inputName].entered = true;
  159. },
  160. saveChanges() {
  161. const usernameChanged =
  162. this.modifiedUser.username !== this.originalUser.username;
  163. const emailAddressChanged =
  164. this.modifiedUser.email.address !==
  165. this.originalUser.email.address;
  166. if (usernameChanged) this.changeUsername();
  167. if (emailAddressChanged) this.changeEmail();
  168. if (!usernameChanged && !emailAddressChanged) {
  169. this.$refs.saveButton.handleFailedSave();
  170. new Toast("Please make a change before saving.");
  171. }
  172. },
  173. changeEmail() {
  174. const email = this.modifiedUser.email.address;
  175. if (!validation.isLength(email, 3, 254))
  176. return new Toast(
  177. "Email must have between 3 and 254 characters."
  178. );
  179. if (
  180. email.indexOf("@") !== email.lastIndexOf("@") ||
  181. !validation.regex.emailSimple.test(email)
  182. )
  183. return new Toast("Invalid email format.");
  184. this.$refs.saveButton.saveStatus = "disabled";
  185. return this.socket.dispatch(
  186. "users.updateEmail",
  187. this.userId,
  188. email,
  189. res => {
  190. if (res.status !== "success") {
  191. new Toast(res.message);
  192. this.$refs.saveButton.handleFailedSave();
  193. } else {
  194. new Toast("Successfully changed email address");
  195. this.updateOriginalUser({
  196. property: "email.address",
  197. value: email
  198. });
  199. this.$refs.saveButton.handleSuccessfulSave();
  200. }
  201. }
  202. );
  203. },
  204. changeUsername() {
  205. const { username } = this.modifiedUser;
  206. if (!validation.isLength(username, 2, 32))
  207. return new Toast(
  208. "Username must have between 2 and 32 characters."
  209. );
  210. if (!validation.regex.azAZ09_.test(username))
  211. return new Toast(
  212. "Invalid username format. Allowed characters: a-z, A-Z, 0-9 and _."
  213. );
  214. this.$refs.saveButton.saveStatus = "disabled";
  215. return this.socket.dispatch(
  216. "users.updateUsername",
  217. this.userId,
  218. username,
  219. res => {
  220. if (res.status !== "success") {
  221. new Toast(res.message);
  222. this.$refs.saveButton.handleFailedSave();
  223. } else {
  224. new Toast("Successfully changed username");
  225. this.updateOriginalUser({
  226. property: "username",
  227. value: username
  228. });
  229. this.$refs.saveButton.handleSuccessfulSave();
  230. }
  231. }
  232. );
  233. },
  234. removeActivities() {
  235. this.socket.dispatch("activities.removeAllForUser", res => {
  236. new Toast(res.message);
  237. });
  238. },
  239. ...mapActions("settings", ["updateOriginalUser"]),
  240. ...mapActions("modalVisibility", ["openModal"])
  241. }
  242. };
  243. </script>
  244. <style lang="scss" scoped>
  245. .control {
  246. margin-bottom: 2px !important;
  247. }
  248. .row {
  249. display: flex;
  250. }
  251. </style>