Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ChatView.vue 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <template lang="pug">
  2. main.view--chat
  3. header.mb6(v-if='profile && grouping')
  4. h3 chatting with: {{ target.profile_id }}
  5. p {{ grouping.profile.user_name }}
  6. p {{ grouping.profile.user_email }}
  7. p {{ grouping.profile }}
  8. //- p subscriptions: {{ profile.chatter.subscriptions }}
  9. .w-flex.column
  10. p reveal: {{ grouping.revealed }}
  11. .w-flex.row
  12. button(@click='reveal(7)') my name
  13. button(@click='reveal(8)') my email
  14. article
  15. template(v-if='isLoading')
  16. w-spinner(bounce)
  17. template(v-if='!isLoading && target')
  18. ul#messages.w-flex.column
  19. template(v-for='chatmessage in messages')
  20. li(
  21. :class='[{ me: chatmessage.publisher == profile.id.value }, chatmessage.publisher]'
  22. v-if='chatmessage.message.description'
  23. )
  24. ChatBubble(
  25. :message='chatmessage.message.description'
  26. :publisher-id='chatmessage.publisher'
  27. :time='chatmessage.timetoken'
  28. )
  29. template(v-else-if='!isLoading && !target')
  30. p No match found between profile {{ $route.params.pid }} and {{ profile.id }}...
  31. footer.w-flex.row
  32. w-input(@keyup.enter='sendMessage' outline v-model='toSend')
  33. w-button(@click='sendMessage')
  34. w-icon mdi mdi-send
  35. MainNav
  36. </template>
  37. <script>
  38. import ChatBubble from '../components/ChatBubble.vue'
  39. import { currentProfile } from '../services'
  40. import { mixins } from '../utils'
  41. export default {
  42. name: 'ProfileView',
  43. components: { ChatBubble },
  44. mixins: [mixins.profileMixin],
  45. data: () => ({
  46. target: null,
  47. toSend: '',
  48. messages: [],
  49. openDrawer: null,
  50. }),
  51. computed: {
  52. grouping() {
  53. return this.profile.groupings.find(profileGrouping =>
  54. profileGrouping.participants.includes(
  55. parseInt(this.$route.params.pid),
  56. ),
  57. )
  58. },
  59. },
  60. watch: {
  61. profile() {
  62. this.loadTargetProfile()
  63. currentProfile.chatter.setOnMessage(this._onMessage)
  64. },
  65. },
  66. created() {
  67. this.loadTargetProfile()
  68. currentProfile.chatter.setOnMessage(this._onMessage)
  69. },
  70. methods: {
  71. reveal(tagId) {
  72. this.grouping.reveal(currentProfile.profile_id, tagId)
  73. },
  74. /**
  75. * Pubnub message callback fires when message event
  76. * is detected. We define is HERE because we need the
  77. * component state and `this` context
  78. */
  79. _onMessage(e) {
  80. if (!e.message) return
  81. this.messages.push(e)
  82. },
  83. // TODO: test this
  84. getGrouping() {
  85. return currentProfile.groupings.find(
  86. g => g.profile.profile_id == this.$route.params.pid,
  87. )
  88. },
  89. sendMessage() {
  90. const grouping = this.getGrouping()
  91. currentProfile.chatter.publish(grouping.grouping_name, this.toSend)
  92. this.toSend = ''
  93. },
  94. // TODO: test this
  95. loadTargetProfile() {
  96. try {
  97. const grouping = this.getGrouping()
  98. if (!grouping) {
  99. throw `no match found for ${currentProfile.id.value}`
  100. }
  101. this.target = grouping.profile
  102. } catch (err) {
  103. console.error(err)
  104. }
  105. },
  106. },
  107. }
  108. </script>
  109. <style lang="sass">
  110. main.view--chat
  111. article ul
  112. list-style: none
  113. > li
  114. font-family: sans-serif
  115. background-color: powderblue
  116. margin: 0 0.5em 1em 2em
  117. &.me
  118. margin: 0 2em 1em 0.5em
  119. background-color: lightgreen
  120. p:first-of-type
  121. font-size: 10px
  122. </style>