Profile.vue 18 KB

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