Account.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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">Remove any data we hold on you</h4>
  54. <p class="section-description">
  55. Permanently remove your account and/or data we store on you
  56. </p>
  57. <hr class="section-horizontal-rule" />
  58. <div class="row">
  59. <quick-confirm @confirm="removeActivities()">
  60. <a class="button is-warning">
  61. <i class="material-icons icon-with-button">cancel</i>
  62. Clear my activities
  63. </a>
  64. </quick-confirm>
  65. <a class="button is-danger" @click="openModal('removeAccount')">
  66. <i class="material-icons icon-with-button">delete</i>
  67. Remove my account
  68. </a>
  69. </div>
  70. </div>
  71. </template>
  72. <script>
  73. import { mapState, mapActions, mapGetters } from "vuex";
  74. import Toast from "toasters";
  75. import InputHelpBox from "@/components/InputHelpBox.vue";
  76. import SaveButton from "@/components/SaveButton.vue";
  77. import validation from "@/validation";
  78. export default {
  79. components: {
  80. InputHelpBox,
  81. SaveButton
  82. },
  83. data() {
  84. return {
  85. validation: {
  86. username: {
  87. entered: false,
  88. valid: false,
  89. message: "Please enter a valid username."
  90. },
  91. email: {
  92. entered: false,
  93. valid: false,
  94. message: "Please enter a valid email address."
  95. }
  96. }
  97. };
  98. },
  99. computed: {
  100. ...mapState({
  101. userId: state => state.user.auth.userId,
  102. originalUser: state => state.settings.originalUser,
  103. modifiedUser: state => state.settings.modifiedUser
  104. }),
  105. ...mapGetters({
  106. socket: "websockets/getSocket"
  107. })
  108. },
  109. watch: {
  110. // prettier-ignore
  111. // eslint-disable-next-line func-names
  112. "modifiedUser.username": function (value) {
  113. if (!validation.isLength(value, 2, 32)) {
  114. this.validation.username.message =
  115. "Username must have between 2 and 32 characters.";
  116. this.validation.username.valid = false;
  117. } else if (
  118. !validation.regex.azAZ09_.test(value) &&
  119. value !== this.originalUser.username // Sometimes a username pulled from GitHub won't succeed validation
  120. ) {
  121. this.validation.username.message =
  122. "Invalid format. Allowed characters: a-z, A-Z, 0-9 and _.";
  123. this.validation.username.valid = false;
  124. } else if (value.replaceAll(/[_]/g, "").length === 0) {
  125. this.validation.username.message =
  126. "Invalid format. Allowed characters: a-z, A-Z, 0-9 and _, and there has to be at least one letter or number.";
  127. this.validation.username.valid = false;
  128. } else {
  129. this.validation.username.message = "Everything looks great!";
  130. this.validation.username.valid = true;
  131. }
  132. },
  133. // prettier-ignore
  134. // eslint-disable-next-line func-names
  135. "modifiedUser.email.address": function (value) {
  136. if (!validation.isLength(value, 3, 254)) {
  137. this.validation.email.message =
  138. "Email must have between 3 and 254 characters.";
  139. this.validation.email.valid = false;
  140. } else if (
  141. value.indexOf("@") !== value.lastIndexOf("@") ||
  142. !validation.regex.emailSimple.test(value)
  143. ) {
  144. this.validation.email.message = "Invalid format.";
  145. this.validation.email.valid = false;
  146. } else {
  147. this.validation.email.message = "Everything looks great!";
  148. this.validation.email.valid = true;
  149. }
  150. }
  151. },
  152. mounted() {
  153. if (
  154. this.$route.query.removeAccount === "relinked-github" &&
  155. !localStorage.getItem("github_redirect")
  156. ) {
  157. this.openModal("removeAccount");
  158. setTimeout(() => {
  159. const modal = this.$parent.$children.find(
  160. child => child.name === "RemoveAccount"
  161. );
  162. modal.confirmGithubLink();
  163. }, 50);
  164. }
  165. },
  166. methods: {
  167. onInput(inputName) {
  168. this.validation[inputName].entered = true;
  169. },
  170. saveChanges() {
  171. const usernameChanged =
  172. this.modifiedUser.username !== this.originalUser.username;
  173. const emailAddressChanged =
  174. this.modifiedUser.email.address !==
  175. this.originalUser.email.address;
  176. if (usernameChanged) this.changeUsername();
  177. if (emailAddressChanged) this.changeEmail();
  178. if (!usernameChanged && !emailAddressChanged) {
  179. this.$refs.saveButton.handleFailedSave();
  180. new Toast("Please make a change before saving.");
  181. }
  182. },
  183. changeEmail() {
  184. const email = this.modifiedUser.email.address;
  185. if (!validation.isLength(email, 3, 254))
  186. return new Toast(
  187. "Email must have between 3 and 254 characters."
  188. );
  189. if (
  190. email.indexOf("@") !== email.lastIndexOf("@") ||
  191. !validation.regex.emailSimple.test(email)
  192. )
  193. return new Toast("Invalid email format.");
  194. this.$refs.saveButton.saveStatus = "disabled";
  195. return this.socket.dispatch(
  196. "users.updateEmail",
  197. this.userId,
  198. email,
  199. res => {
  200. if (res.status !== "success") {
  201. new Toast(res.message);
  202. this.$refs.saveButton.handleFailedSave();
  203. } else {
  204. new Toast("Successfully changed email address");
  205. this.updateOriginalUser({
  206. property: "email.address",
  207. value: email
  208. });
  209. this.$refs.saveButton.handleSuccessfulSave();
  210. }
  211. }
  212. );
  213. },
  214. changeUsername() {
  215. const { username } = this.modifiedUser;
  216. if (!validation.isLength(username, 2, 32))
  217. return new Toast(
  218. "Username must have between 2 and 32 characters."
  219. );
  220. if (!validation.regex.azAZ09_.test(username))
  221. return new Toast(
  222. "Invalid username format. Allowed characters: a-z, A-Z, 0-9 and _."
  223. );
  224. if (username.replaceAll(/[_]/g, "").length === 0)
  225. return new Toast(
  226. "Invalid username format. Allowed characters: a-z, A-Z, 0-9 and _, and there has to be at least one letter or number."
  227. );
  228. this.$refs.saveButton.saveStatus = "disabled";
  229. return this.socket.dispatch(
  230. "users.updateUsername",
  231. this.userId,
  232. username,
  233. res => {
  234. if (res.status !== "success") {
  235. new Toast(res.message);
  236. this.$refs.saveButton.handleFailedSave();
  237. } else {
  238. new Toast("Successfully changed username");
  239. this.updateOriginalUser({
  240. property: "username",
  241. value: username
  242. });
  243. this.$refs.saveButton.handleSuccessfulSave();
  244. }
  245. }
  246. );
  247. },
  248. removeActivities() {
  249. this.socket.dispatch("activities.removeAllForUser", res => {
  250. new Toast(res.message);
  251. });
  252. },
  253. ...mapActions("settings", ["updateOriginalUser"]),
  254. ...mapActions("modalVisibility", ["openModal"])
  255. }
  256. };
  257. </script>
  258. <style lang="less" scoped>
  259. .control {
  260. margin-bottom: 2px !important;
  261. }
  262. .row {
  263. display: flex;
  264. }
  265. </style>