News.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <template>
  2. <div class="app">
  3. <page-metadata title="News" />
  4. <main-header />
  5. <div class="container">
  6. <div class="content-wrapper">
  7. <h1 class="has-text-centered page-title">News</h1>
  8. <div
  9. v-for="item in news"
  10. :key="item._id"
  11. class="section news-item"
  12. >
  13. <div v-html="sanitize(marked(item.markdown))"></div>
  14. <div class="info">
  15. <hr />
  16. By
  17. <user-id-to-username
  18. :user-id="item.createdBy"
  19. :alt="item.createdBy"
  20. :link="true"
  21. />&nbsp;<span :title="new Date(item.createdAt)">
  22. {{
  23. formatDistance(item.createdAt, new Date(), {
  24. addSuffix: true
  25. })
  26. }}
  27. </span>
  28. </div>
  29. </div>
  30. <h3 v-if="news.length === 0" class="has-text-centered">
  31. No news items were found.
  32. </h3>
  33. </div>
  34. </div>
  35. <main-footer />
  36. </div>
  37. </template>
  38. <script>
  39. import { formatDistance } from "date-fns";
  40. import { mapGetters } from "vuex";
  41. import { marked } from "marked";
  42. import { sanitize } from "dompurify";
  43. import ws from "@/ws";
  44. import MainHeader from "@/components/layout/MainHeader.vue";
  45. import MainFooter from "@/components/layout/MainFooter.vue";
  46. import UserIdToUsername from "@/components/UserIdToUsername.vue";
  47. export default {
  48. components: { MainHeader, MainFooter, UserIdToUsername },
  49. data() {
  50. return {
  51. news: []
  52. };
  53. },
  54. computed: mapGetters({
  55. socket: "websockets/getSocket"
  56. }),
  57. mounted() {
  58. marked.use({
  59. renderer: {
  60. table(header, body) {
  61. return `<table class="table">
  62. <thead>${header}</thead>
  63. <tbody>${body}</tbody>
  64. </table>`;
  65. }
  66. }
  67. });
  68. this.socket.on("event:news.created", res =>
  69. this.news.unshift(res.data.news)
  70. );
  71. this.socket.on("event:news.updated", res => {
  72. if (res.data.news.status === "draft") {
  73. this.news = this.news.filter(
  74. item => item._id !== res.data.news._id
  75. );
  76. return;
  77. }
  78. for (let n = 0; n < this.news.length; n += 1) {
  79. if (this.news[n]._id === res.data.news._id)
  80. this.$set(this.news, n, {
  81. ...this.news[n],
  82. ...res.data.news
  83. });
  84. }
  85. });
  86. this.socket.on("event:news.deleted", res => {
  87. this.news = this.news.filter(item => item._id !== res.data.newsId);
  88. });
  89. ws.onConnect(this.init);
  90. },
  91. methods: {
  92. marked,
  93. sanitize,
  94. formatDistance,
  95. init() {
  96. this.socket.dispatch("news.getPublished", res => {
  97. if (res.status === "success") this.news = res.data.news;
  98. });
  99. this.socket.dispatch("apis.joinRoom", "news");
  100. }
  101. }
  102. };
  103. </script>
  104. <style lang="less" scoped>
  105. .night-mode {
  106. p {
  107. color: var(--light-grey-2);
  108. }
  109. }
  110. .container {
  111. width: calc(100% - 32px);
  112. }
  113. .section {
  114. border: 1px solid var(--light-grey-3);
  115. max-width: 100%;
  116. margin-top: 50px;
  117. &:last-of-type {
  118. margin-bottom: 50px;
  119. }
  120. }
  121. </style>