Profile.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  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: activeTab === 'recentActivity' }"
  72. @click="switchTab('recentActivity')"
  73. >
  74. Recent activity
  75. </button>
  76. <button
  77. :class="{ active: activeTab === 'playlists' }"
  78. @click="switchTab('playlists')"
  79. >
  80. Playlists
  81. </button>
  82. </div>
  83. <div
  84. class="content recent-activity-tab"
  85. v-if="activeTab === 'recentActivity'"
  86. >
  87. <div v-if="activities.length > 0">
  88. <div
  89. class="item activity"
  90. v-for="activity in sortedActivities"
  91. :key="activity._id"
  92. >
  93. <div class="thumbnail">
  94. <img :src="activity.thumbnail" alt="" />
  95. <i class="material-icons activity-type-icon">{{
  96. activity.icon
  97. }}</i>
  98. </div>
  99. <div class="left-part">
  100. <p
  101. class="top-text"
  102. v-html="activity.message"
  103. ></p>
  104. <p class="bottom-text">
  105. {{
  106. formatDistance(
  107. parseISO(activity.createdAt),
  108. new Date(),
  109. { addSuffix: true }
  110. )
  111. }}
  112. </p>
  113. </div>
  114. <div class="actions">
  115. <a
  116. class="hide-icon"
  117. href="#"
  118. @click="hideActivity(activity._id)"
  119. >
  120. <i class="material-icons">visibility_off</i>
  121. </a>
  122. </div>
  123. </div>
  124. </div>
  125. <div v-else>
  126. <h2>No recent activity.</h2>
  127. </div>
  128. </div>
  129. <div
  130. class="content playlists-tab"
  131. v-if="activeTab === 'playlists'"
  132. >
  133. <div v-if="playlists.length > 0">
  134. <div
  135. class="item playlist"
  136. v-for="playlist in playlists"
  137. :key="playlist._id"
  138. >
  139. <playlist-item
  140. v-if="
  141. playlist.privacy === 'public' ||
  142. (playlist.privacy === 'private' &&
  143. playlist.createdBy === userId)
  144. "
  145. :playlist="playlist"
  146. >
  147. <div v-if="user._id === userId" slot="actions">
  148. <button
  149. class="button is-primary"
  150. @click="editPlaylistClick(playlist._id)"
  151. >
  152. <i
  153. class="material-icons icon-with-button"
  154. >create</i
  155. >Edit
  156. </button>
  157. </div>
  158. </playlist-item>
  159. </div>
  160. <button
  161. v-if="user._id === userId"
  162. class="button is-primary"
  163. @click="
  164. openModal({
  165. sector: 'station',
  166. modal: 'createPlaylist'
  167. })
  168. "
  169. >
  170. Create new playlist
  171. </button>
  172. </div>
  173. <div v-else>
  174. <h2>No playlists here.</h2>
  175. </div>
  176. </div>
  177. </div>
  178. </div>
  179. <main-footer />
  180. </div>
  181. </template>
  182. <script>
  183. import { mapState, mapActions } from "vuex";
  184. import { format, formatDistance, parseISO } from "date-fns";
  185. import Toast from "toasters";
  186. import PlaylistItem from "../components/ui/PlaylistItem.vue";
  187. import MainHeader from "../components/layout/MainHeader.vue";
  188. import MainFooter from "../components/layout/MainFooter.vue";
  189. import io from "../io";
  190. export default {
  191. components: {
  192. MainHeader,
  193. MainFooter,
  194. PlaylistItem,
  195. CreatePlaylist: () => import("../components/modals/CreatePlaylist.vue"),
  196. EditPlaylist: () => import("../components/modals/EditPlaylist.vue")
  197. },
  198. data() {
  199. return {
  200. user: {},
  201. notes: "",
  202. isUser: false,
  203. activeTab: "recentActivity",
  204. playlists: [],
  205. activities: []
  206. };
  207. },
  208. computed: {
  209. ...mapState({
  210. role: state => state.user.auth.role,
  211. userId: state => state.user.auth.userId,
  212. ...mapState("modalVisibility", {
  213. modals: state => state.modals.station
  214. })
  215. }),
  216. sortedActivities() {
  217. const { activities } = this;
  218. return activities.sort(
  219. (x, y) => new Date(y.createdAt) - new Date(x.createdAt)
  220. );
  221. }
  222. },
  223. mounted() {
  224. lofig.get("frontendDomain").then(frontendDomain => {
  225. this.frontendDomain = frontendDomain;
  226. this.notes = encodeURI(`${this.frontendDomain}/assets/notes.png`);
  227. });
  228. io.getSocket(socket => {
  229. this.socket = socket;
  230. this.socket.emit(
  231. "users.findByUsername",
  232. this.$route.params.username,
  233. res => {
  234. if (res.status === "error") this.$router.go("/404");
  235. else {
  236. this.user = res.data;
  237. this.user.createdAt = format(
  238. parseISO(this.user.createdAt),
  239. "MMMM do yyyy"
  240. );
  241. this.isUser = true;
  242. if (this.user._id !== this.userId) {
  243. this.socket.emit(
  244. "apis.joinRoom",
  245. `profile-${res.data._id}`,
  246. () => {}
  247. );
  248. }
  249. this.socket.emit(
  250. "playlists.indexForUser",
  251. this.user._id,
  252. res => {
  253. if (res.status === "success")
  254. this.playlists = res.data;
  255. }
  256. );
  257. this.socket.on("event:playlist.create", playlist => {
  258. this.playlists.push(playlist);
  259. });
  260. this.socket.on("event:playlist.delete", playlistId => {
  261. this.playlists.forEach((playlist, index) => {
  262. if (playlist._id === playlistId) {
  263. this.playlists.splice(index, 1);
  264. }
  265. });
  266. });
  267. this.socket.on("event:playlist.addSong", data => {
  268. this.playlists.forEach((playlist, index) => {
  269. if (playlist._id === data.playlistId) {
  270. this.playlists[index].songs.push(data.song);
  271. }
  272. });
  273. });
  274. this.socket.on("event:playlist.removeSong", data => {
  275. this.playlists.forEach((playlist, index) => {
  276. if (playlist._id === data.playlistId) {
  277. this.playlists[index].songs.forEach(
  278. (song, index2) => {
  279. if (song.songId === data.songId) {
  280. this.playlists[
  281. index
  282. ].songs.splice(index2, 1);
  283. }
  284. }
  285. );
  286. }
  287. });
  288. });
  289. this.socket.on(
  290. "event:playlist.updateDisplayName",
  291. data => {
  292. this.playlists.forEach((playlist, index) => {
  293. if (playlist._id === data.playlistId) {
  294. this.playlists[index].displayName =
  295. data.displayName;
  296. }
  297. });
  298. }
  299. );
  300. this.socket.on("event:playlist.updatePrivacy", data => {
  301. this.playlists.forEach((playlist, index) => {
  302. if (playlist._id === data.playlist._id) {
  303. this.playlists[index].privacy =
  304. data.playlist.privacy;
  305. }
  306. });
  307. });
  308. if (this.user._id === this.userId) {
  309. this.socket.emit(
  310. "activities.getSet",
  311. this.userId,
  312. 1,
  313. res => {
  314. if (res.status === "success") {
  315. for (
  316. let a = 0;
  317. a < res.data.length;
  318. a += 1
  319. ) {
  320. this.formatActivity(
  321. res.data[a],
  322. activity => {
  323. this.activities.unshift(
  324. activity
  325. );
  326. }
  327. );
  328. }
  329. }
  330. }
  331. );
  332. this.socket.on(
  333. "event:activity.create",
  334. activity => {
  335. console.log(activity);
  336. this.formatActivity(activity, activity => {
  337. this.activities.unshift(activity);
  338. });
  339. }
  340. );
  341. }
  342. }
  343. }
  344. );
  345. });
  346. },
  347. methods: {
  348. formatDistance,
  349. parseISO,
  350. switchTab(tabName) {
  351. this.activeTab = tabName;
  352. },
  353. editPlaylistClick(playlistId) {
  354. console.log(playlistId);
  355. this.editPlaylist(playlistId);
  356. this.openModal({ sector: "station", modal: "editPlaylist" });
  357. },
  358. hideActivity(activityId) {
  359. this.socket.emit("activities.hideActivity", activityId, res => {
  360. if (res.status === "success") {
  361. this.activities = this.activities.filter(
  362. activity => activity._id !== activityId
  363. );
  364. } else {
  365. new Toast({ content: res.message, timeout: 3000 });
  366. }
  367. });
  368. },
  369. formatActivity(res, cb) {
  370. console.log("activity", res);
  371. const icons = {
  372. created_account: "account_circle",
  373. created_station: "radio",
  374. deleted_station: "delete",
  375. created_playlist: "playlist_add_check",
  376. deleted_playlist: "delete_sweep",
  377. liked_song: "favorite",
  378. added_song_to_playlist: "playlist_add",
  379. added_songs_to_playlist: "playlist_add"
  380. };
  381. const activity = {
  382. ...res,
  383. thumbnail: "",
  384. message: "",
  385. icon: ""
  386. };
  387. const plural = activity.payload.length > 1;
  388. activity.icon = icons[activity.activityType];
  389. if (activity.activityType === "created_account") {
  390. activity.message = "Welcome to Musare!";
  391. return cb(activity);
  392. }
  393. if (activity.activityType === "created_station") {
  394. this.socket.emit(
  395. "stations.getStationForActivity",
  396. activity.payload[0],
  397. res => {
  398. if (res.status === "success") {
  399. activity.message = `Created the station <strong>${res.data.title}</strong>`;
  400. activity.thumbnail = res.data.thumbnail;
  401. return cb(activity);
  402. }
  403. activity.message = "Created a station";
  404. return cb(activity);
  405. }
  406. );
  407. }
  408. if (activity.activityType === "deleted_station") {
  409. activity.message = `Deleted a station`;
  410. return cb(activity);
  411. }
  412. if (activity.activityType === "created_playlist") {
  413. this.socket.emit(
  414. "playlists.getPlaylistForActivity",
  415. activity.payload[0],
  416. res => {
  417. if (res.status === "success") {
  418. activity.message = `Created the playlist <strong>${res.data.title}</strong>`;
  419. // activity.thumbnail = res.data.thumbnail;
  420. return cb(activity);
  421. }
  422. activity.message = "Created a playlist";
  423. return cb(activity);
  424. }
  425. );
  426. }
  427. if (activity.activityType === "deleted_playlist") {
  428. activity.message = `Deleted a playlist`;
  429. return cb(activity);
  430. }
  431. if (activity.activityType === "liked_song") {
  432. if (plural) {
  433. activity.message = `Liked ${activity.payload.length} songs.`;
  434. return cb(activity);
  435. }
  436. this.socket.emit(
  437. "songs.getSongForActivity",
  438. activity.payload[0],
  439. res => {
  440. if (res.status === "success") {
  441. activity.message = `Liked the song <strong>${res.data.title}</strong>`;
  442. activity.thumbnail = res.data.thumbnail;
  443. return cb(activity);
  444. }
  445. activity.message = "Liked a song";
  446. return cb(activity);
  447. }
  448. );
  449. }
  450. if (activity.activityType === "added_song_to_playlist") {
  451. this.socket.emit(
  452. "songs.getSongForActivity",
  453. activity.payload[0].songId,
  454. song => {
  455. console.log(song);
  456. this.socket.emit(
  457. "playlists.getPlaylistForActivity",
  458. activity.payload[0].playlistId,
  459. playlist => {
  460. if (song.status === "success") {
  461. if (playlist.status === "success")
  462. activity.message = `Added the song <strong>${song.data.title}</strong> to the playlist <strong>${playlist.data.title}</strong>`;
  463. else
  464. activity.message = `Added the song <strong>${song.data.title}</strong> to a playlist`;
  465. activity.thumbnail = song.data.thumbnail;
  466. return cb(activity);
  467. }
  468. if (playlist.status === "success") {
  469. activity.message = `Added a song to the playlist <strong>${playlist.data.title}</strong>`;
  470. return cb(activity);
  471. }
  472. activity.message = "Added a song to a playlist";
  473. return cb(activity);
  474. }
  475. );
  476. }
  477. );
  478. }
  479. if (activity.activityType === "added_songs_to_playlist") {
  480. activity.message = `Added ${activity.payload.length} songs to a playlist`;
  481. return cb(activity);
  482. }
  483. return false;
  484. },
  485. ...mapActions("modalVisibility", ["openModal"]),
  486. ...mapActions("user/playlists", ["editPlaylist"])
  487. }
  488. };
  489. </script>
  490. <style lang="scss" scoped>
  491. @import "../styles/global.scss";
  492. .info-section {
  493. width: 912px;
  494. margin-left: auto;
  495. margin-right: auto;
  496. margin-top: 32px;
  497. padding: 24px;
  498. .picture-name-row {
  499. display: flex;
  500. flex-direction: row;
  501. align-items: center;
  502. justify-content: center;
  503. margin-bottom: 24px;
  504. }
  505. .profile-picture {
  506. width: 100px;
  507. height: 100px;
  508. border-radius: 100%;
  509. margin-right: 32px;
  510. }
  511. .name-role-row {
  512. display: flex;
  513. flex-direction: row;
  514. align-items: center;
  515. }
  516. .name {
  517. font-size: 34px;
  518. line-height: 40px;
  519. color: $dark-grey-3;
  520. }
  521. .role {
  522. padding: 2px 24px;
  523. color: $white;
  524. text-transform: uppercase;
  525. font-size: 12px;
  526. line-height: 14px;
  527. height: 18px;
  528. border-radius: 5px;
  529. margin-left: 12px;
  530. &.admin {
  531. background-color: $red;
  532. }
  533. }
  534. .username {
  535. font-size: 24px;
  536. line-height: 28px;
  537. color: $dark-grey;
  538. margin: 0;
  539. }
  540. .buttons {
  541. width: 388px;
  542. display: flex;
  543. flex-direction: row;
  544. margin-left: auto;
  545. margin-right: auto;
  546. margin-bottom: 24px;
  547. .button {
  548. flex: 1;
  549. font-size: 17px;
  550. line-height: 20px;
  551. &:nth-child(2) {
  552. margin-left: 20px;
  553. }
  554. }
  555. }
  556. .bio-row,
  557. .date-location-row {
  558. i {
  559. font-size: 24px;
  560. color: $dark-grey-2;
  561. margin-right: 12px;
  562. }
  563. p {
  564. font-size: 17px;
  565. line-height: 20px;
  566. color: $dark-grey-2;
  567. word-break: break-word;
  568. }
  569. }
  570. .bio-row {
  571. max-width: 608px;
  572. margin-bottom: 24px;
  573. margin-left: auto;
  574. margin-right: auto;
  575. display: flex;
  576. width: max-content;
  577. }
  578. .date-location-row {
  579. max-width: 608px;
  580. margin-left: auto;
  581. margin-right: auto;
  582. margin-bottom: 24px;
  583. display: flex;
  584. width: max-content;
  585. margin-bottom: 24px;
  586. > div:nth-child(2) {
  587. margin-left: 48px;
  588. }
  589. }
  590. .date,
  591. .location {
  592. display: flex;
  593. }
  594. }
  595. .bottom-section {
  596. width: 962px;
  597. margin-left: auto;
  598. margin-right: auto;
  599. margin-top: 32px;
  600. padding: 24px;
  601. display: flex;
  602. .buttons {
  603. height: 100%;
  604. width: 250px;
  605. margin-right: 64px;
  606. button {
  607. outline: none;
  608. border: none;
  609. box-shadow: none;
  610. color: $musare-blue;
  611. font-size: 22px;
  612. line-height: 26px;
  613. padding: 7px 0 7px 12px;
  614. width: 100%;
  615. text-align: left;
  616. cursor: pointer;
  617. border-radius: 5px;
  618. background-color: transparent;
  619. &.active {
  620. color: $white;
  621. background-color: $musare-blue;
  622. }
  623. }
  624. }
  625. .content {
  626. width: 600px;
  627. .item {
  628. width: 100%;
  629. height: 72px;
  630. border: 0.5px $light-grey-2 solid;
  631. margin-bottom: 12px;
  632. border-radius: 5px;
  633. display: flex;
  634. overflow: hidden;
  635. .top-text {
  636. color: $dark-grey-2;
  637. font-size: 20px;
  638. line-height: 23px;
  639. margin-bottom: 0;
  640. }
  641. .bottom-text {
  642. color: $dark-grey-2;
  643. font-size: 16px;
  644. line-height: 19px;
  645. margin-bottom: 0;
  646. margin-top: 6px;
  647. &:first-letter {
  648. text-transform: uppercase;
  649. }
  650. }
  651. .thumbnail {
  652. position: relative;
  653. display: flex;
  654. align-items: center;
  655. justify-content: center;
  656. width: 70.5px;
  657. height: 70.5px;
  658. background-color: #000;
  659. z-index: -1;
  660. img {
  661. opacity: 0.4;
  662. }
  663. .activity-type-icon {
  664. position: absolute;
  665. color: #fff;
  666. }
  667. }
  668. .left-part {
  669. flex: 1;
  670. padding: 12px;
  671. }
  672. .actions {
  673. display: flex;
  674. align-items: center;
  675. padding: 12px;
  676. .hide-icon {
  677. border-bottom: 0;
  678. display: flex;
  679. i {
  680. color: #bdbdbd;
  681. }
  682. }
  683. }
  684. button {
  685. font-size: 17px;
  686. }
  687. }
  688. }
  689. }
  690. .night-mode {
  691. .name,
  692. .username,
  693. .bio-row i,
  694. .bio-row p,
  695. .date-location-row i,
  696. .date-location-row p,
  697. .item .left-part .top-text,
  698. .item .left-part .bottom-text {
  699. color: $light-grey;
  700. }
  701. }
  702. </style>