News.vue 3.9 KB

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