News.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <template>
  2. <div>
  3. <page-metadata title="Admin | News" />
  4. <div class="container">
  5. <table class="table is-striped">
  6. <thead>
  7. <tr>
  8. <td>Status</td>
  9. <td>Title</td>
  10. <td>Author</td>
  11. <td>Markdown</td>
  12. <td>Options</td>
  13. </tr>
  14. </thead>
  15. <tbody>
  16. <tr v-for="news in news" :key="news._id">
  17. <td class="news-item-status">{{ news.status }}</td>
  18. <td>
  19. <strong>{{ news.title }}</strong>
  20. </td>
  21. <td>
  22. <user-id-to-username
  23. :user-id="news.createdBy"
  24. :alt="news.createdBy"
  25. :link="true"
  26. />
  27. </td>
  28. <td class="news-item-markdown">{{ news.markdown }}</td>
  29. <td id="options-column">
  30. <button
  31. class="button is-primary"
  32. @click="edit(news._id)"
  33. >
  34. Edit
  35. </button>
  36. <confirm @confirm="remove(news._id)">
  37. <button class="button is-danger">Remove</button>
  38. </confirm>
  39. </td>
  40. </tr>
  41. </tbody>
  42. </table>
  43. <button class="is-primary button" @click="edit()">
  44. Create News Item
  45. </button>
  46. </div>
  47. <edit-news
  48. v-if="modals.editNews"
  49. :news-id="editingNewsId"
  50. sector="admin"
  51. />
  52. </div>
  53. </template>
  54. <script>
  55. import { mapActions, mapState, mapGetters } from "vuex";
  56. import { defineAsyncComponent } from "vue";
  57. import Toast from "toasters";
  58. import ws from "@/ws";
  59. import Confirm from "@/components/Confirm.vue";
  60. import UserIdToUsername from "@/components/UserIdToUsername.vue";
  61. export default {
  62. components: {
  63. Confirm,
  64. UserIdToUsername,
  65. EditNews: defineAsyncComponent(() =>
  66. import("@/components/modals/EditNews.vue")
  67. )
  68. },
  69. data() {
  70. return {
  71. editingNewsId: ""
  72. };
  73. },
  74. computed: {
  75. ...mapState("modalVisibility", {
  76. modals: state => state.modals
  77. }),
  78. ...mapState("admin/news", {
  79. news: state => state.news
  80. }),
  81. ...mapGetters({
  82. socket: "websockets/getSocket"
  83. })
  84. },
  85. mounted() {
  86. this.socket.on("event:admin.news.created", res =>
  87. this.addNews(res.data.news)
  88. );
  89. this.socket.on("event:admin.news.updated", res =>
  90. this.updateNews(res.data.news)
  91. );
  92. this.socket.on("event:admin.news.deleted", res =>
  93. this.removeNews(res.data.newsId)
  94. );
  95. ws.onConnect(this.init);
  96. },
  97. methods: {
  98. edit(id) {
  99. if (id) this.editingNewsId = id;
  100. else this.editingNewsId = "";
  101. this.openModal("editNews");
  102. },
  103. remove(id) {
  104. this.socket.dispatch(
  105. "news.remove",
  106. id,
  107. res => new Toast(res.message)
  108. );
  109. },
  110. init() {
  111. this.socket.dispatch("news.index", res => {
  112. if (res.status === "success") this.setNews(res.data.news);
  113. });
  114. this.socket.dispatch("apis.joinAdminRoom", "news");
  115. },
  116. ...mapActions("modalVisibility", ["openModal", "closeModal"]),
  117. ...mapActions("admin/news", [
  118. "editNews",
  119. "addNews",
  120. "setNews",
  121. "removeNews",
  122. "updateNews"
  123. ])
  124. }
  125. };
  126. </script>
  127. <style lang="scss" scoped>
  128. .night-mode {
  129. .table {
  130. color: var(--light-grey-2);
  131. background-color: var(--dark-grey-3);
  132. thead tr {
  133. background: var(--dark-grey-3);
  134. td {
  135. color: var(--white);
  136. }
  137. }
  138. tbody tr:hover {
  139. background-color: var(--dark-grey-4) !important;
  140. }
  141. tbody tr:nth-child(even) {
  142. background-color: var(--dark-grey-2);
  143. }
  144. strong {
  145. color: var(--light-grey-2);
  146. }
  147. }
  148. .card {
  149. background: var(--dark-grey-3);
  150. .card-header {
  151. box-shadow: 0 1px 2px rgba(10, 10, 10, 0.8);
  152. }
  153. p,
  154. .label {
  155. color: var(--light-grey-2);
  156. }
  157. }
  158. }
  159. .tag:not(:last-child) {
  160. margin-right: 5px;
  161. }
  162. td {
  163. vertical-align: middle;
  164. & > div {
  165. display: inline-flex;
  166. }
  167. }
  168. .is-info:focus {
  169. background-color: var(--primary-color);
  170. }
  171. .card-footer-item {
  172. color: var(--primary-color);
  173. }
  174. .news-item-status {
  175. text-transform: capitalize;
  176. }
  177. .news-item-markdown {
  178. text-overflow: ellipsis;
  179. white-space: nowrap;
  180. overflow: hidden;
  181. max-width: 400px;
  182. }
  183. #options-column {
  184. display: flex;
  185. button {
  186. margin-right: 5px;
  187. }
  188. }
  189. </style>