Profile.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <template>
  2. <div class="content profile-tab">
  3. <h4 class="section-title">Change Profile</h4>
  4. <p class="section-description">
  5. Edit your public profile so users can find out more about you.
  6. </p>
  7. <hr class="section-horizontal-rule" />
  8. <div
  9. class="control is-expanded avatar-selection-outer-container"
  10. v-if="modifiedUser.avatar"
  11. >
  12. <label>Avatar</label>
  13. <div id="avatar-selection-inner-container">
  14. <profile-picture
  15. :avatar="modifiedUser.avatar"
  16. :name="
  17. modifiedUser.name
  18. ? modifiedUser.name
  19. : modifiedUser.username
  20. "
  21. />
  22. <div class="select">
  23. <select v-model="modifiedUser.avatar.type">
  24. <option value="gravatar">Using Gravatar</option>
  25. <option value="initials">Based on initials</option>
  26. </select>
  27. </div>
  28. </div>
  29. </div>
  30. <p class="control is-expanded margin-top-zero">
  31. <label for="name">Name</label>
  32. <input
  33. class="input"
  34. id="name"
  35. type="text"
  36. placeholder="Enter name here..."
  37. maxlength="64"
  38. v-model="modifiedUser.name"
  39. />
  40. <span v-if="modifiedUser.name" class="character-counter"
  41. >{{ modifiedUser.name.length }}/64</span
  42. >
  43. </p>
  44. <p class="control is-expanded">
  45. <label for="location">Location</label>
  46. <input
  47. class="input"
  48. id="location"
  49. type="text"
  50. placeholder="Enter location here..."
  51. maxlength="50"
  52. v-model="modifiedUser.location"
  53. />
  54. <span v-if="modifiedUser.location" class="character-counter"
  55. >{{ modifiedUser.location.length }}/50</span
  56. >
  57. </p>
  58. <p class="control is-expanded">
  59. <label for="bio">Bio</label>
  60. <textarea
  61. class="textarea"
  62. id="bio"
  63. placeholder="Enter bio here..."
  64. maxlength="200"
  65. autocomplete="off"
  66. v-model="modifiedUser.bio"
  67. />
  68. <span v-if="modifiedUser.bio" class="character-counter"
  69. >{{ modifiedUser.bio.length }}/200</span
  70. >
  71. </p>
  72. <save-button ref="saveButton" @clicked="saveChanges()" />
  73. </div>
  74. </template>
  75. <script>
  76. import { mapState, mapActions, mapGetters } from "vuex";
  77. import Toast from "toasters";
  78. import ProfilePicture from "@/components/ProfilePicture.vue";
  79. import SaveButton from "@/components/SaveButton.vue";
  80. import validation from "@/validation";
  81. export default {
  82. components: { ProfilePicture, SaveButton },
  83. computed: {
  84. ...mapState({
  85. userId: state => state.user.auth.userId,
  86. originalUser: state => state.settings.originalUser,
  87. modifiedUser: state => state.settings.modifiedUser
  88. }),
  89. ...mapGetters({
  90. socket: "websockets/getSocket"
  91. })
  92. },
  93. watch: {
  94. "modifiedUser.avatar.type": function watchAvatarType(newType, oldType) {
  95. if (
  96. oldType &&
  97. this.modifiedUser.avatar.type !==
  98. this.originalUser.avatar.type &&
  99. newType === "initials"
  100. ) {
  101. const colors = ["blue", "orange", "green", "purple", "teal"];
  102. const color = colors[Math.floor(Math.random() * colors.length)];
  103. this.modifiedUser.avatar.color = color;
  104. }
  105. }
  106. },
  107. methods: {
  108. saveChanges() {
  109. const nameChanged =
  110. this.modifiedUser.name !== this.originalUser.name;
  111. const locationChanged =
  112. this.modifiedUser.location !== this.originalUser.location;
  113. const bioChanged = this.modifiedUser.bio !== this.originalUser.bio;
  114. const avatarChanged =
  115. this.modifiedUser.avatar.type !== this.originalUser.avatar.type;
  116. if (nameChanged) this.changeName();
  117. if (locationChanged) this.changeLocation();
  118. if (bioChanged) this.changeBio();
  119. if (avatarChanged) this.changeAvatarType();
  120. if (
  121. !avatarChanged &&
  122. !bioChanged &&
  123. !locationChanged &&
  124. !nameChanged
  125. ) {
  126. this.$refs.saveButton.handleFailedSave();
  127. new Toast("Please make a change before saving.");
  128. }
  129. },
  130. changeName() {
  131. this.modifiedUser.name = this.modifiedUser.name
  132. .replaceAll(/ +/g, " ")
  133. .trim();
  134. const { name } = this.modifiedUser;
  135. if (!validation.isLength(name, 1, 64))
  136. return new Toast("Name must have between 1 and 64 characters.");
  137. if (!validation.regex.name.test(name))
  138. return new Toast(
  139. "Invalid name format. Only letters, spaces, apostrophes and hyphens are allowed."
  140. );
  141. if (name.replaceAll(/[ .'-]/g, "").length === 0)
  142. return new Toast(
  143. "Invalid name format. Only letters, spaces, apostrophes and hyphens are allowed, and there has to be at least one letter."
  144. );
  145. this.$refs.saveButton.status = "disabled";
  146. return this.socket.dispatch(
  147. "users.updateName",
  148. this.userId,
  149. name,
  150. res => {
  151. if (res.status !== "success") {
  152. new Toast(res.message);
  153. this.$refs.saveButton.handleFailedSave();
  154. } else {
  155. new Toast("Successfully changed name");
  156. this.updateOriginalUser({
  157. property: "name",
  158. value: name
  159. });
  160. this.$refs.saveButton.handleSuccessfulSave();
  161. }
  162. }
  163. );
  164. },
  165. changeLocation() {
  166. const { location } = this.modifiedUser;
  167. if (!validation.isLength(location, 0, 50))
  168. return new Toast(
  169. "Location must have between 0 and 50 characters."
  170. );
  171. this.$refs.saveButton.status = "disabled";
  172. return this.socket.dispatch(
  173. "users.updateLocation",
  174. this.userId,
  175. location,
  176. res => {
  177. if (res.status !== "success") {
  178. new Toast(res.message);
  179. this.$refs.saveButton.handleFailedSave();
  180. } else {
  181. new Toast("Successfully changed location");
  182. this.updateOriginalUser({
  183. property: "location",
  184. value: location
  185. });
  186. this.$refs.saveButton.handleSuccessfulSave();
  187. }
  188. }
  189. );
  190. },
  191. changeBio() {
  192. const { bio } = this.modifiedUser;
  193. if (!validation.isLength(bio, 0, 200))
  194. return new Toast("Bio must have between 0 and 200 characters.");
  195. this.$refs.saveButton.status = "disabled";
  196. return this.socket.dispatch(
  197. "users.updateBio",
  198. this.userId,
  199. bio,
  200. res => {
  201. if (res.status !== "success") {
  202. new Toast(res.message);
  203. this.$refs.saveButton.handleFailedSave();
  204. } else {
  205. new Toast("Successfully changed bio");
  206. this.updateOriginalUser({
  207. property: "bio",
  208. value: bio
  209. });
  210. this.$refs.saveButton.handleSuccessfulSave();
  211. }
  212. }
  213. );
  214. },
  215. changeAvatarType() {
  216. const { avatar } = this.modifiedUser;
  217. this.$refs.saveButton.status = "disabled";
  218. return this.socket.dispatch(
  219. "users.updateAvatarType",
  220. this.userId,
  221. avatar,
  222. res => {
  223. if (res.status !== "success") {
  224. new Toast(res.message);
  225. this.$refs.saveButton.handleFailedSave();
  226. } else {
  227. new Toast("Successfully updated avatar type");
  228. this.updateOriginalUser({
  229. property: "avatar",
  230. value: avatar
  231. });
  232. this.$refs.saveButton.handleSuccessfulSave();
  233. }
  234. }
  235. );
  236. },
  237. ...mapActions("settings", ["updateOriginalUser"])
  238. }
  239. };
  240. </script>
  241. <style lang="scss" scoped>
  242. .content .control {
  243. margin-bottom: 15px;
  244. }
  245. .character-counter {
  246. height: initial;
  247. }
  248. .avatar-selection-outer-container {
  249. display: flex;
  250. flex-direction: column;
  251. align-items: flex-start;
  252. .select:after {
  253. border-color: var(--primary-color);
  254. }
  255. #avatar-selection-inner-container {
  256. display: flex;
  257. align-items: center;
  258. margin-top: 5px;
  259. .profile-picture {
  260. margin-right: 10px;
  261. width: 50px;
  262. height: 50px;
  263. }
  264. /deep/ .profile-picture.using-initials span {
  265. font-size: 20px; // 2/5th of .profile-picture height/width
  266. }
  267. }
  268. }
  269. </style>