Profile.vue 7.2 KB

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