Profile.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  1. <template>
  2. <div v-if="isUser">
  3. <metadata :title="`Profile | ${user.username}`" />
  4. <edit-playlist v-if="modals.editPlaylist" />
  5. <create-playlist v-if="modals.createPlaylist" />
  6. <main-header />
  7. <div class="container">
  8. <div class="info-section">
  9. <div class="picture-name-row">
  10. <img
  11. class="profile-picture"
  12. :src="
  13. user.avatar.url && user.avatar.type === 'gravatar'
  14. ? `${user.avatar.url}?d=${notes}&s=250`
  15. : '/assets/notes.png'
  16. "
  17. onerror="this.src='/assets/notes.png'; this.onerror=''"
  18. />
  19. <div>
  20. <div class="name-role-row">
  21. <p class="name">{{ user.name }}</p>
  22. <span
  23. class="role admin"
  24. v-if="user.role === 'admin'"
  25. >admin</span
  26. >
  27. </div>
  28. <h2 class="username">@{{ user.username }}</h2>
  29. </div>
  30. </div>
  31. <div
  32. class="buttons"
  33. v-if="userId === user._id || role === 'admin'"
  34. >
  35. <router-link
  36. :to="`/admin/users?userId=${user._id}`"
  37. class="button is-primary"
  38. v-if="role === 'admin'"
  39. >
  40. Edit
  41. </router-link>
  42. <router-link
  43. to="/settings"
  44. class="button is-primary"
  45. v-if="userId === user._id"
  46. >
  47. Settings
  48. </router-link>
  49. </div>
  50. <div class="bio-row" v-if="user.bio">
  51. <i class="material-icons">notes</i>
  52. <p>{{ user.bio }}</p>
  53. </div>
  54. <div
  55. class="date-location-row"
  56. v-if="user.createdAt || user.location"
  57. >
  58. <div class="date" v-if="user.createdAt">
  59. <i class="material-icons">calendar_today</i>
  60. <p>{{ user.createdAt }}</p>
  61. </div>
  62. <div class="location" v-if="user.location">
  63. <i class="material-icons">location_on</i>
  64. <p>{{ user.location }}</p>
  65. </div>
  66. </div>
  67. </div>
  68. <div class="bottom-section">
  69. <div class="buttons">
  70. <button
  71. :class="{ active: tab === 'recent-activity' }"
  72. @click="showTab('recent-activity')"
  73. >
  74. Recent activity
  75. </button>
  76. <button
  77. :class="{ active: tab === 'playlists' }"
  78. @click="showTab('playlists')"
  79. >
  80. Playlists
  81. </button>
  82. </div>
  83. <div
  84. class="content recent-activity-tab"
  85. v-if="tab === 'recent-activity'"
  86. >
  87. <div v-if="activities.length > 0">
  88. <h4 class="section-title">Recent activity</h4>
  89. <p class="section-description">
  90. This is a log of all actions
  91. {{
  92. userId === user._id
  93. ? "you have"
  94. : `${user.name} has`
  95. }}
  96. taken recently.
  97. </p>
  98. <hr class="section-horizontal-rule" />
  99. <div
  100. class="item activity-item universal-item"
  101. v-for="activity in sortedActivities"
  102. :key="activity._id"
  103. >
  104. <div class="thumbnail">
  105. <img :src="activity.thumbnail" alt="" />
  106. <i class="material-icons activity-type-icon">{{
  107. activity.icon
  108. }}</i>
  109. </div>
  110. <div class="left-part">
  111. <p
  112. class="item-title"
  113. v-html="activity.message"
  114. ></p>
  115. <p class="item-description">
  116. {{
  117. formatDistance(
  118. parseISO(activity.createdAt),
  119. new Date(),
  120. { addSuffix: true }
  121. )
  122. }}
  123. </p>
  124. </div>
  125. <div class="universal-item-actions">
  126. <a href="#" @click="hideActivity(activity._id)">
  127. <i class="material-icons hide-icon"
  128. >visibility_off</i
  129. >
  130. </a>
  131. </div>
  132. </div>
  133. </div>
  134. <div v-else>
  135. <h3>No recent activity.</h3>
  136. </div>
  137. </div>
  138. <div class="content playlists-tab" v-if="tab === 'playlists'">
  139. <div v-if="playlists.length > 0">
  140. <h4 class="section-title">
  141. {{ user._id === userId ? "My" : null }}
  142. Playlists
  143. </h4>
  144. <p class="section-description">
  145. View
  146. {{
  147. userId === user._id
  148. ? "and manage your personal"
  149. : `${user.name}'s`
  150. }}
  151. playlists.
  152. </p>
  153. <hr class="section-horizontal-rule" />
  154. <draggable
  155. class="menu-list scrollable-list"
  156. v-if="playlists.length > 0"
  157. v-model="playlists"
  158. v-bind="dragOptions"
  159. @start="drag = true"
  160. @end="drag = false"
  161. @change="savePlaylistOrder"
  162. >
  163. <transition-group
  164. type="transition"
  165. :name="
  166. !drag ? 'draggable-list-transition' : null
  167. "
  168. >
  169. <div
  170. class="item item-draggable"
  171. v-for="playlist in playlists"
  172. :key="playlist._id"
  173. >
  174. <playlist-item
  175. v-if="
  176. playlist.privacy === 'public' ||
  177. (playlist.privacy ===
  178. 'private' &&
  179. playlist.createdBy ===
  180. userId)
  181. "
  182. :playlist="playlist"
  183. >
  184. <div slot="actions">
  185. <i
  186. v-if="user._id === userId"
  187. @click="
  188. showPlaylist(playlist._id)
  189. "
  190. class="material-icons edit-icon"
  191. >edit</i
  192. >
  193. <i
  194. v-else
  195. @click="
  196. showPlaylist(playlist._id)
  197. "
  198. class="material-icons view-icon"
  199. >visibility</i
  200. >
  201. </div>
  202. </playlist-item>
  203. </div>
  204. </transition-group>
  205. </draggable>
  206. <button
  207. v-if="user._id === userId"
  208. class="button is-primary"
  209. id="create-new-playlist-button"
  210. @click="
  211. openModal({
  212. sector: 'station',
  213. modal: 'createPlaylist'
  214. })
  215. "
  216. >
  217. Create new playlist
  218. </button>
  219. </div>
  220. <div v-else>
  221. <h3>No playlists here.</h3>
  222. </div>
  223. </div>
  224. </div>
  225. </div>
  226. <main-footer />
  227. </div>
  228. </template>
  229. <script>
  230. import { mapState, mapActions } from "vuex";
  231. import { format, formatDistance, parseISO } from "date-fns";
  232. import Toast from "toasters";
  233. import draggable from "vuedraggable";
  234. import TabQueryHandler from "../mixins/TabQueryHandler.vue";
  235. import PlaylistItem from "../components/ui/PlaylistItem.vue";
  236. import SortablePlaylists from "../mixins/SortablePlaylists.vue";
  237. import MainHeader from "../components/layout/MainHeader.vue";
  238. import MainFooter from "../components/layout/MainFooter.vue";
  239. import io from "../io";
  240. export default {
  241. components: {
  242. MainHeader,
  243. MainFooter,
  244. PlaylistItem,
  245. CreatePlaylist: () => import("../components/modals/CreatePlaylist.vue"),
  246. EditPlaylist: () =>
  247. import("../components/modals/EditPlaylist/index.vue"),
  248. draggable
  249. },
  250. mixins: [SortablePlaylists, TabQueryHandler],
  251. data() {
  252. return {
  253. user: {},
  254. notes: "",
  255. isUser: false,
  256. tab: "recent-activity",
  257. playlists: [],
  258. activities: []
  259. };
  260. },
  261. computed: {
  262. ...mapState({
  263. role: state => state.user.auth.role,
  264. userId: state => state.user.auth.userId,
  265. ...mapState("modalVisibility", {
  266. modals: state => state.modals.station
  267. })
  268. }),
  269. sortedActivities() {
  270. const { activities } = this;
  271. return activities.sort(
  272. (x, y) => new Date(y.createdAt) - new Date(x.createdAt)
  273. );
  274. }
  275. },
  276. mounted() {
  277. if (
  278. this.$route.query.tab === "recent-activity" ||
  279. this.$route.query.tab === "playlists"
  280. )
  281. this.tab = this.$route.query.tab;
  282. lofig.get("frontendDomain").then(frontendDomain => {
  283. this.frontendDomain = frontendDomain;
  284. this.notes = encodeURI(`${this.frontendDomain}/assets/notes.png`);
  285. });
  286. io.getSocket(socket => {
  287. this.socket = socket;
  288. this.socket.emit(
  289. "users.findByUsername",
  290. this.$route.params.username,
  291. res => {
  292. if (res.status === "error") this.$router.go("/404");
  293. else {
  294. this.user = res.data;
  295. this.user.createdAt = format(
  296. parseISO(this.user.createdAt),
  297. "MMMM do yyyy"
  298. );
  299. this.isUser = true;
  300. if (this.user._id !== this.userId) {
  301. this.socket.emit(
  302. "apis.joinRoom",
  303. `profile-${res.data._id}`,
  304. () => {}
  305. );
  306. }
  307. this.socket.emit(
  308. "playlists.indexForUser",
  309. this.user._id,
  310. res => {
  311. if (res.status === "success")
  312. this.playlists = res.data;
  313. this.orderOfPlaylists = this.calculatePlaylistOrder(); // order in regards to the database
  314. }
  315. );
  316. this.socket.on("event:playlist.create", playlist => {
  317. this.playlists.push(playlist);
  318. });
  319. this.socket.on("event:playlist.delete", playlistId => {
  320. this.playlists.forEach((playlist, index) => {
  321. if (playlist._id === playlistId) {
  322. this.playlists.splice(index, 1);
  323. }
  324. });
  325. });
  326. this.socket.on("event:playlist.addSong", data => {
  327. this.playlists.forEach((playlist, index) => {
  328. if (playlist._id === data.playlistId) {
  329. this.playlists[index].songs.push(data.song);
  330. }
  331. });
  332. });
  333. this.socket.on("event:playlist.removeSong", data => {
  334. this.playlists.forEach((playlist, index) => {
  335. if (playlist._id === data.playlistId) {
  336. this.playlists[index].songs.forEach(
  337. (song, index2) => {
  338. if (song.songId === data.songId) {
  339. this.playlists[
  340. index
  341. ].songs.splice(index2, 1);
  342. }
  343. }
  344. );
  345. }
  346. });
  347. });
  348. this.socket.on(
  349. "event:playlist.updateDisplayName",
  350. data => {
  351. this.playlists.forEach((playlist, index) => {
  352. if (playlist._id === data.playlistId) {
  353. this.playlists[index].displayName =
  354. data.displayName;
  355. }
  356. });
  357. }
  358. );
  359. this.socket.on("event:playlist.updatePrivacy", data => {
  360. this.playlists.forEach((playlist, index) => {
  361. if (playlist._id === data.playlist._id) {
  362. this.playlists[index].privacy =
  363. data.playlist.privacy;
  364. }
  365. });
  366. });
  367. this.socket.on(
  368. "event:user.orderOfPlaylists.changed",
  369. orderOfPlaylists => {
  370. const sortedPlaylists = [];
  371. this.playlists.forEach(playlist => {
  372. sortedPlaylists[
  373. orderOfPlaylists.indexOf(playlist._id)
  374. ] = playlist;
  375. });
  376. this.playlists = sortedPlaylists;
  377. this.orderOfPlaylists = this.calculatePlaylistOrder();
  378. }
  379. );
  380. if (this.user._id === this.userId) {
  381. this.socket.emit(
  382. "activities.getSet",
  383. this.userId,
  384. 1,
  385. res => {
  386. if (res.status === "success") {
  387. for (
  388. let a = 0;
  389. a < res.data.length;
  390. a += 1
  391. ) {
  392. this.formatActivity(
  393. res.data[a],
  394. activity => {
  395. this.activities.unshift(
  396. activity
  397. );
  398. }
  399. );
  400. }
  401. }
  402. }
  403. );
  404. this.socket.on(
  405. "event:activity.create",
  406. activity => {
  407. console.log(activity);
  408. this.formatActivity(activity, activity => {
  409. this.activities.unshift(activity);
  410. });
  411. }
  412. );
  413. }
  414. }
  415. }
  416. );
  417. });
  418. },
  419. methods: {
  420. formatDistance,
  421. parseISO,
  422. showPlaylist(playlistId) {
  423. this.editPlaylist(playlistId);
  424. this.openModal({ sector: "station", modal: "editPlaylist" });
  425. },
  426. hideActivity(activityId) {
  427. this.socket.emit("activities.hideActivity", activityId, res => {
  428. if (res.status === "success") {
  429. this.activities = this.activities.filter(
  430. activity => activity._id !== activityId
  431. );
  432. } else {
  433. new Toast({ content: res.message, timeout: 3000 });
  434. }
  435. });
  436. },
  437. formatActivity(res, cb) {
  438. console.log("activity", res);
  439. const icons = {
  440. created_account: "account_circle",
  441. created_station: "radio",
  442. deleted_station: "delete",
  443. created_playlist: "playlist_add_check",
  444. deleted_playlist: "delete_sweep",
  445. liked_song: "favorite",
  446. added_song_to_playlist: "playlist_add",
  447. added_songs_to_playlist: "playlist_add"
  448. };
  449. const activity = {
  450. ...res,
  451. thumbnail: "",
  452. message: "",
  453. icon: ""
  454. };
  455. const plural = activity.payload.length > 1;
  456. activity.icon = icons[activity.activityType];
  457. if (activity.activityType === "created_account") {
  458. activity.message = "Welcome to Musare!";
  459. return cb(activity);
  460. }
  461. if (activity.activityType === "created_station") {
  462. this.socket.emit(
  463. "stations.getStationForActivity",
  464. activity.payload[0],
  465. res => {
  466. if (res.status === "success") {
  467. activity.message = `Created the station <strong>${res.data.title}</strong>`;
  468. activity.thumbnail = res.data.thumbnail;
  469. return cb(activity);
  470. }
  471. activity.message = "Created a station";
  472. return cb(activity);
  473. }
  474. );
  475. }
  476. if (activity.activityType === "deleted_station") {
  477. activity.message = `Deleted a station`;
  478. return cb(activity);
  479. }
  480. if (activity.activityType === "created_playlist") {
  481. this.socket.emit(
  482. "playlists.getPlaylistForActivity",
  483. activity.payload[0],
  484. res => {
  485. if (res.status === "success") {
  486. activity.message = `Created the playlist <strong>${res.data.title}</strong>`;
  487. // activity.thumbnail = res.data.thumbnail;
  488. return cb(activity);
  489. }
  490. activity.message = "Created a playlist";
  491. return cb(activity);
  492. }
  493. );
  494. }
  495. if (activity.activityType === "deleted_playlist") {
  496. activity.message = `Deleted a playlist`;
  497. return cb(activity);
  498. }
  499. if (activity.activityType === "liked_song") {
  500. if (plural) {
  501. activity.message = `Liked ${activity.payload.length} songs.`;
  502. return cb(activity);
  503. }
  504. this.socket.emit(
  505. "songs.getSongForActivity",
  506. activity.payload[0],
  507. res => {
  508. if (res.status === "success") {
  509. activity.message = `Liked the song <strong>${res.data.title}</strong>`;
  510. activity.thumbnail = res.data.thumbnail;
  511. return cb(activity);
  512. }
  513. activity.message = "Liked a song";
  514. return cb(activity);
  515. }
  516. );
  517. }
  518. if (activity.activityType === "added_song_to_playlist") {
  519. this.socket.emit(
  520. "songs.getSongForActivity",
  521. activity.payload[0].songId,
  522. song => {
  523. console.log(song);
  524. this.socket.emit(
  525. "playlists.getPlaylistForActivity",
  526. activity.payload[0].playlistId,
  527. playlist => {
  528. if (song.status === "success") {
  529. if (playlist.status === "success")
  530. activity.message = `Added the song <strong>${song.data.title}</strong> to the playlist <strong>${playlist.data.title}</strong>`;
  531. else
  532. activity.message = `Added the song <strong>${song.data.title}</strong> to a playlist`;
  533. activity.thumbnail = song.data.thumbnail;
  534. return cb(activity);
  535. }
  536. if (playlist.status === "success") {
  537. activity.message = `Added a song to the playlist <strong>${playlist.data.title}</strong>`;
  538. return cb(activity);
  539. }
  540. activity.message = "Added a song to a playlist";
  541. return cb(activity);
  542. }
  543. );
  544. }
  545. );
  546. }
  547. if (activity.activityType === "added_songs_to_playlist") {
  548. activity.message = `Added ${activity.payload.length} songs to a playlist`;
  549. return cb(activity);
  550. }
  551. return false;
  552. },
  553. ...mapActions("modalVisibility", ["openModal"]),
  554. ...mapActions("user/playlists", ["editPlaylist"])
  555. }
  556. };
  557. </script>
  558. <style lang="scss" scoped>
  559. @import "../styles/global.scss";
  560. @media only screen and (max-width: 750px) {
  561. .info-section {
  562. margin-top: 0 !important;
  563. .picture-name-row {
  564. flex-direction: column !important;
  565. }
  566. .name-role-row {
  567. margin-top: 24px;
  568. }
  569. .buttons .button:not(:last-of-type) {
  570. margin-bottom: 10px;
  571. margin-right: 5px;
  572. }
  573. .date-location-row {
  574. flex-direction: column;
  575. width: auto !important;
  576. }
  577. .date-location-row > div:nth-child(2),
  578. .buttons .button:nth-child(2) {
  579. margin-left: 0 !important;
  580. }
  581. }
  582. .bottom-section {
  583. flex-direction: column;
  584. }
  585. .content {
  586. margin: 24px 0;
  587. }
  588. }
  589. .info-section {
  590. width: 912px;
  591. max-width: 100%;
  592. margin-left: auto;
  593. margin-right: auto;
  594. margin-top: 32px;
  595. padding: 24px;
  596. .picture-name-row {
  597. display: flex;
  598. flex-direction: row;
  599. align-items: center;
  600. justify-content: center;
  601. margin-bottom: 24px;
  602. }
  603. .profile-picture {
  604. width: 100px;
  605. height: 100px;
  606. border-radius: 100%;
  607. border: 0.5px solid $light-grey-2;
  608. margin-right: 32px;
  609. }
  610. .name-role-row {
  611. display: flex;
  612. flex-direction: row;
  613. align-items: center;
  614. }
  615. .name {
  616. font-size: 34px;
  617. line-height: 40px;
  618. color: $dark-grey-3;
  619. }
  620. .role {
  621. padding: 2px 24px;
  622. color: $white;
  623. text-transform: uppercase;
  624. font-size: 12px;
  625. line-height: 14px;
  626. height: 18px;
  627. border-radius: 5px;
  628. margin-left: 12px;
  629. &.admin {
  630. background-color: $red;
  631. }
  632. }
  633. .username {
  634. font-size: 24px;
  635. line-height: 28px;
  636. color: $dark-grey;
  637. margin: 0;
  638. }
  639. .buttons {
  640. width: 388px;
  641. max-width: 100%;
  642. display: flex;
  643. flex-direction: row;
  644. margin-left: auto;
  645. margin-right: auto;
  646. margin-bottom: 24px;
  647. .button {
  648. flex: 1;
  649. font-size: 17px;
  650. line-height: 20px;
  651. &:nth-child(2) {
  652. margin-left: 20px;
  653. }
  654. }
  655. }
  656. .bio-row,
  657. .date-location-row {
  658. i {
  659. font-size: 24px;
  660. color: $dark-grey-2;
  661. margin-right: 12px;
  662. }
  663. p {
  664. font-size: 17px;
  665. line-height: 20px;
  666. color: $dark-grey-2;
  667. word-break: break-word;
  668. }
  669. }
  670. .bio-row {
  671. max-width: 608px;
  672. margin-bottom: 24px;
  673. margin-left: auto;
  674. margin-right: auto;
  675. display: flex;
  676. width: max-content;
  677. }
  678. .date-location-row {
  679. max-width: 608px;
  680. margin-left: auto;
  681. margin-right: auto;
  682. margin-bottom: 24px;
  683. display: flex;
  684. width: max-content;
  685. margin-bottom: 24px;
  686. > div:nth-child(2) {
  687. margin-left: 48px;
  688. }
  689. }
  690. .date,
  691. .location {
  692. display: flex;
  693. }
  694. }
  695. .bottom-section {
  696. width: 962px;
  697. max-width: 100%;
  698. margin-left: auto;
  699. margin-right: auto;
  700. padding: 24px;
  701. display: flex;
  702. .buttons {
  703. height: 100%;
  704. width: 250px;
  705. margin-right: 64px;
  706. button {
  707. outline: none;
  708. border: none;
  709. box-shadow: none;
  710. color: $musare-blue;
  711. font-size: 22px;
  712. line-height: 26px;
  713. padding: 7px 0 7px 12px;
  714. width: 100%;
  715. text-align: left;
  716. cursor: pointer;
  717. border-radius: 5px;
  718. background-color: transparent;
  719. &.active {
  720. color: $white;
  721. background-color: $musare-blue;
  722. }
  723. }
  724. }
  725. .content {
  726. width: 600px;
  727. max-width: 100%;
  728. background-color: #fff;
  729. padding: 30px 50px;
  730. border-radius: 3px;
  731. h3 {
  732. font-weight: 400;
  733. }
  734. .item {
  735. overflow: hidden;
  736. &:not(:last-of-type) {
  737. margin-bottom: 10px;
  738. }
  739. /** temp code - will be put into a separate component */
  740. &.activity-item {
  741. height: 72px;
  742. border: 0.5px $light-grey-2 solid;
  743. border-radius: 3px;
  744. .thumbnail {
  745. position: relative;
  746. display: flex;
  747. align-items: center;
  748. justify-content: center;
  749. width: 70.5px;
  750. height: 70.5px;
  751. img {
  752. opacity: 0.4;
  753. }
  754. .activity-type-icon {
  755. position: absolute;
  756. color: $dark-grey;
  757. font-size: 30px;
  758. }
  759. }
  760. .left-part {
  761. flex: 1;
  762. padding: 12px;
  763. .item-title {
  764. margin: 0;
  765. }
  766. }
  767. .universal-item-actions a {
  768. border-bottom: 0;
  769. }
  770. }
  771. }
  772. #create-new-playlist-button {
  773. margin-top: 30px;
  774. width: 100%;
  775. }
  776. }
  777. }
  778. .night-mode {
  779. .name,
  780. .username,
  781. .bio-row i,
  782. .bio-row p,
  783. .date-location-row i,
  784. .date-location-row p,
  785. .item .left-part .top-text,
  786. .item .left-part .bottom-text,
  787. .bottom-section
  788. .content
  789. .item.activity-item
  790. .thumbnail
  791. .activity-type-icon {
  792. color: $light-grey;
  793. }
  794. }
  795. </style>