Profile.vue 19 KB

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