Show.vue 16 KB

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