Account.vue 7.2 KB

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