News.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <template>
  2. <div class="app">
  3. <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. />
  22. <span :title="new Date(item.createdAt)">
  23. {{
  24. formatDistance(item.createdAt, new Date(), {
  25. addSuffix: true
  26. })
  27. }}
  28. </span>
  29. </div>
  30. </div>
  31. <h3 v-if="news.length === 0" class="has-text-centered">
  32. No news items were found.
  33. </h3>
  34. </div>
  35. </div>
  36. <main-footer />
  37. </div>
  38. </template>
  39. <script>
  40. import { formatDistance } from "date-fns";
  41. import { mapGetters } from "vuex";
  42. import marked from "marked";
  43. import { sanitize } from "dompurify";
  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 is-striped">
  62. <thead>${header}</thead>
  63. <tbody>${body}</tbody>
  64. </table>`;
  65. }
  66. }
  67. });
  68. this.socket.dispatch("news.index", res => {
  69. if (res.status === "success") this.news = res.data.news;
  70. });
  71. this.socket.on("event:admin.news.created", res =>
  72. this.news.unshift(res.data.news)
  73. );
  74. this.socket.on("event:admin.news.updated", res => {
  75. for (let n = 0; n < this.news.length; n += 1) {
  76. if (this.news[n]._id === res.data.news._id) {
  77. this.$set(this.news, n, res.data.news);
  78. }
  79. }
  80. });
  81. this.socket.on("event:admin.news.removed", res => {
  82. this.news = this.news.filter(item => item._id !== res.data.newsId);
  83. });
  84. },
  85. methods: {
  86. marked,
  87. sanitize,
  88. formatDistance
  89. }
  90. };
  91. </script>
  92. <style lang="scss" scoped>
  93. .night-mode {
  94. p {
  95. color: var(--light-grey-2);
  96. }
  97. }
  98. .section {
  99. border: 1px solid var(--light-grey-3);
  100. width: 1000px;
  101. max-width: 100%;
  102. margin-top: 50px;
  103. &:last-of-type {
  104. margin-bottom: 50px;
  105. }
  106. }
  107. </style>