Show.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  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_account: "account_circle",
  370. created_station: "radio",
  371. deleted_station: "delete",
  372. created_playlist: "playlist_add_check",
  373. deleted_playlist: "delete_sweep",
  374. liked_song: "favorite",
  375. added_song_to_playlist: "playlist_add",
  376. added_songs_to_playlist: "playlist_add"
  377. };
  378. const activity = {
  379. ...res,
  380. thumbnail: "",
  381. message: "",
  382. icon: ""
  383. };
  384. const plural = activity.payload.length > 1;
  385. activity.icon = icons[activity.activityType];
  386. if (activity.activityType === "created_account") {
  387. activity.message = "Welcome to Musare!";
  388. return cb(activity);
  389. }
  390. if (activity.activityType === "created_station") {
  391. this.socket.emit(
  392. "stations.getStationForActivity",
  393. activity.payload[0],
  394. res => {
  395. if (res.status === "success") {
  396. activity.message = `Created the station <strong>${res.data.title}</strong>`;
  397. activity.thumbnail = res.data.thumbnail;
  398. return cb(activity);
  399. }
  400. activity.message = "Created a station";
  401. return cb(activity);
  402. }
  403. );
  404. }
  405. if (activity.activityType === "deleted_station") {
  406. activity.message = `Deleted a station`;
  407. return cb(activity);
  408. }
  409. if (activity.activityType === "created_playlist") {
  410. this.socket.emit(
  411. "playlists.getPlaylistForActivity",
  412. activity.payload[0],
  413. res => {
  414. if (res.status === "success") {
  415. activity.message = `Created the playlist <strong>${res.data.title}</strong>`;
  416. // activity.thumbnail = res.data.thumbnail;
  417. return cb(activity);
  418. }
  419. activity.message = "Created a playlist";
  420. return cb(activity);
  421. }
  422. );
  423. }
  424. if (activity.activityType === "deleted_playlist") {
  425. activity.message = `Deleted a playlist`;
  426. return cb(activity);
  427. }
  428. if (activity.activityType === "liked_song") {
  429. if (plural) {
  430. activity.message = `Liked ${activity.payload.length} songs.`;
  431. return cb(activity);
  432. }
  433. this.socket.emit(
  434. "songs.getSongForActivity",
  435. activity.payload[0],
  436. res => {
  437. if (res.status === "success") {
  438. activity.message = `Liked the song <strong>${res.data.title}</strong>`;
  439. activity.thumbnail = res.data.thumbnail;
  440. return cb(activity);
  441. }
  442. activity.message = "Liked a song";
  443. return cb(activity);
  444. }
  445. );
  446. }
  447. if (activity.activityType === "added_song_to_playlist") {
  448. this.socket.emit(
  449. "songs.getSongForActivity",
  450. activity.payload[0].songId,
  451. song => {
  452. console.log(song);
  453. this.socket.emit(
  454. "playlists.getPlaylistForActivity",
  455. activity.payload[0].playlistId,
  456. playlist => {
  457. if (song.status === "success") {
  458. if (playlist.status === "success")
  459. activity.message = `Added the song <strong>${song.data.title}</strong> to the playlist <strong>${playlist.data.title}</strong>`;
  460. else
  461. activity.message = `Added the song <strong>${song.data.title}</strong> to a playlist`;
  462. activity.thumbnail = song.data.thumbnail;
  463. return cb(activity);
  464. }
  465. if (playlist.status === "success") {
  466. activity.message = `Added a song to the playlist <strong>${playlist.data.title}</strong>`;
  467. return cb(activity);
  468. }
  469. activity.message = "Added a song to a playlist";
  470. return cb(activity);
  471. }
  472. );
  473. }
  474. );
  475. }
  476. if (activity.activityType === "added_songs_to_playlist") {
  477. activity.message = `Added ${activity.payload.length} songs to a playlist`;
  478. return cb(activity);
  479. }
  480. return false;
  481. },
  482. ...mapActions("modals", ["openModal"]),
  483. ...mapActions("user/playlists", ["editPlaylist"])
  484. }
  485. };
  486. </script>
  487. <style lang="scss" scoped>
  488. @import "styles/global.scss";
  489. .info-section {
  490. width: 912px;
  491. margin-left: auto;
  492. margin-right: auto;
  493. margin-top: 32px;
  494. padding: 24px;
  495. .picture-name-row {
  496. display: flex;
  497. flex-direction: row;
  498. align-items: center;
  499. justify-content: center;
  500. margin-bottom: 24px;
  501. }
  502. .profile-picture {
  503. width: 100px;
  504. height: 100px;
  505. border-radius: 100%;
  506. margin-right: 32px;
  507. }
  508. .name-role-row {
  509. display: flex;
  510. flex-direction: row;
  511. align-items: center;
  512. }
  513. .name {
  514. font-size: 34px;
  515. line-height: 40px;
  516. color: $dark-grey-3;
  517. }
  518. .role {
  519. padding: 2px 24px;
  520. color: $white;
  521. text-transform: uppercase;
  522. font-size: 12px;
  523. line-height: 14px;
  524. height: 18px;
  525. border-radius: 5px;
  526. margin-left: 12px;
  527. &.admin {
  528. background-color: $red;
  529. }
  530. }
  531. .username {
  532. font-size: 24px;
  533. line-height: 28px;
  534. color: $dark-grey;
  535. }
  536. .buttons {
  537. width: 388px;
  538. display: flex;
  539. flex-direction: row;
  540. margin-left: auto;
  541. margin-right: auto;
  542. margin-bottom: 24px;
  543. .button {
  544. flex: 1;
  545. font-size: 17px;
  546. line-height: 20px;
  547. &:nth-child(2) {
  548. margin-left: 20px;
  549. }
  550. }
  551. }
  552. .bio-row,
  553. .date-location-row {
  554. i {
  555. font-size: 24px;
  556. color: $dark-grey-2;
  557. margin-right: 12px;
  558. }
  559. p {
  560. font-size: 17px;
  561. line-height: 20px;
  562. color: $dark-grey-2;
  563. word-break: break-word;
  564. }
  565. }
  566. .bio-row {
  567. max-width: 608px;
  568. margin-bottom: 24px;
  569. margin-left: auto;
  570. margin-right: auto;
  571. display: flex;
  572. width: max-content;
  573. }
  574. .date-location-row {
  575. max-width: 608px;
  576. margin-left: auto;
  577. margin-right: auto;
  578. margin-bottom: 24px;
  579. display: flex;
  580. width: max-content;
  581. margin-bottom: 24px;
  582. > div:nth-child(2) {
  583. margin-left: 48px;
  584. }
  585. }
  586. .date,
  587. .location {
  588. display: flex;
  589. }
  590. }
  591. .bottom-section {
  592. width: 962px;
  593. margin-left: auto;
  594. margin-right: auto;
  595. margin-top: 32px;
  596. padding: 24px;
  597. display: flex;
  598. .buttons {
  599. height: 100%;
  600. width: 250px;
  601. margin-right: 64px;
  602. button {
  603. outline: none;
  604. border: none;
  605. box-shadow: none;
  606. color: $musareBlue;
  607. font-size: 22px;
  608. line-height: 26px;
  609. padding: 7px 0 7px 12px;
  610. width: 100%;
  611. text-align: left;
  612. cursor: pointer;
  613. border-radius: 5px;
  614. background-color: transparent;
  615. &.active {
  616. color: $white;
  617. background-color: $musareBlue;
  618. }
  619. }
  620. }
  621. .content {
  622. width: 600px;
  623. .item {
  624. width: 100%;
  625. height: 72px;
  626. border: 0.5px $light-grey-2 solid;
  627. margin-bottom: 12px;
  628. border-radius: 0 5px 5px 0;
  629. display: flex;
  630. .top-text {
  631. color: $dark-grey-2;
  632. font-size: 20px;
  633. line-height: 23px;
  634. margin-bottom: 0;
  635. }
  636. .bottom-text {
  637. color: $dark-grey-2;
  638. font-size: 16px;
  639. line-height: 19px;
  640. margin-bottom: 0;
  641. margin-top: 6px;
  642. &:first-letter {
  643. text-transform: uppercase;
  644. }
  645. }
  646. .thumbnail {
  647. position: relative;
  648. display: flex;
  649. align-items: center;
  650. justify-content: center;
  651. width: 70.5px;
  652. height: 70.5px;
  653. background-color: #000;
  654. img {
  655. opacity: 0.4;
  656. }
  657. .activity-type-icon {
  658. position: absolute;
  659. color: #fff;
  660. }
  661. }
  662. .left-part {
  663. flex: 1;
  664. padding: 12px;
  665. }
  666. .actions {
  667. display: flex;
  668. align-items: center;
  669. padding: 12px;
  670. .hide-icon {
  671. border-bottom: 0;
  672. display: flex;
  673. i {
  674. color: #bdbdbd;
  675. }
  676. }
  677. }
  678. button {
  679. font-size: 17px;
  680. }
  681. }
  682. }
  683. .playlists-tab > button {
  684. width: 100%;
  685. font-size: 17px;
  686. }
  687. }
  688. .night-mode {
  689. .name,
  690. .username,
  691. .bio-row i,
  692. .bio-row p,
  693. .date-location-row i,
  694. .date-location-row p {
  695. color: $light-grey;
  696. }
  697. }
  698. </style>