You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ChatView.vue 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <template lang="pug">
  2. main.view--chat
  3. header
  4. h2 Chat Page
  5. article(v-if="profile.isLoggedIn && target")
  6. h3 {{ profile.id }}
  7. span chatting with: {{ target.profile_id }}
  8. p subscriptions: {{ profile.chatter.subscriptions }}
  9. ul(id="messages").w-flex.column
  10. li(v-for="message, i in messages" class="pa1")
  11. w-card.w-flex.row
  12. article
  13. p {{ message.message }}
  14. footer
  15. p {{ message.publisher }} | {{ message.timetoken }}
  16. w-button(class="my12" @click="openDrawer = i" text) setting
  17. w-drawer(v-model="openDrawer" absolute width="160px")
  18. p some settings things
  19. input(v-model="toSend" @keyup.enter="sendMessage")
  20. button(@click="sendMessage") send
  21. p(v-else-if="profile.isLoggedIn && !target") No match found between profile {{ $route.params.pid }} and {{profile.id}}...
  22. p(v-else) Loading...
  23. MainNav
  24. </template>
  25. <script>
  26. import { currentProfile } from '../services'
  27. export default {
  28. name: 'ProfileView',
  29. data: () => ({
  30. target: null,
  31. toSend: '',
  32. messages: [],
  33. openDrawer: null,
  34. }),
  35. computed: {
  36. profile: () => currentProfile,
  37. },
  38. watch: {
  39. profile() {
  40. this.loadTargetProfile()
  41. currentProfile.chatter.setOnMessage(this._onMessage)
  42. },
  43. },
  44. created() {
  45. this.loadTargetProfile()
  46. currentProfile.chatter.setOnMessage(this._onMessage)
  47. },
  48. methods: {
  49. /**
  50. * Pubnub message callback fires when message event
  51. * is detected. We define is HERE because we need the
  52. * component state and `this` context
  53. */
  54. _onMessage(e) {
  55. if (!e.message) return
  56. this.messages.push(e)
  57. },
  58. getGrouping() {
  59. return currentProfile.groupings.find(
  60. g => g.profile.profile_id == this.$route.params.tid,
  61. )
  62. },
  63. sendMessage() {
  64. const grouping = this.getGrouping()
  65. currentProfile.chatter.publish(grouping.grouping_name, this.toSend)
  66. this.toSend = ''
  67. },
  68. loadTargetProfile() {
  69. try {
  70. const grouping = this.getGrouping()
  71. if (!grouping) {
  72. throw `no match found for ${currentProfile.id.value}`
  73. }
  74. this.target = grouping.profile
  75. } catch (err) {
  76. console.error(err)
  77. }
  78. },
  79. },
  80. }
  81. </script>