Show.vue 16 KB

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