Преглед на файлове

fix(News): added correct socket events for non-admin pages

Signed-off-by: Jonathan <theflametrooper@gmail.com>
Jonathan преди 3 години
родител
ревизия
6822819fc4
променени са 4 файла, в които са добавени 45 реда и са изтрити 8 реда
  1. 1 0
      backend/logic/actions/apis.js
  2. 16 0
      backend/logic/actions/news.js
  3. 1 1
      frontend/src/pages/Admin/tabs/News.vue
  4. 27 7
      frontend/src/pages/News.vue

+ 1 - 0
backend/logic/actions/apis.js

@@ -125,6 +125,7 @@ export default {
 	joinRoom(session, page, cb) {
 		if (
 			page === "home" ||
+			page === "news" ||
 			page.startsWith("profile.") ||
 			page.startsWith("manage-station.") ||
 			page.startsWith("edit-song.")

+ 16 - 0
backend/logic/actions/news.js

@@ -16,6 +16,12 @@ CacheModule.runJob("SUB", {
 			room: "admin.news",
 			args: ["event:admin.news.created", { data: { news } }]
 		});
+
+		if (news.status === "published")
+			WSModule.runJob("EMIT_TO_ROOM", {
+				room: "news",
+				args: ["event:news.created", { data: { news } }]
+			});
 	}
 });
 
@@ -26,6 +32,11 @@ CacheModule.runJob("SUB", {
 			room: "admin.news",
 			args: ["event:admin.news.removed", { data: { newsId } }]
 		});
+
+		WSModule.runJob("EMIT_TO_ROOM", {
+			room: "news",
+			args: ["event:news.removed", { data: { newsId } }]
+		});
 	}
 });
 
@@ -36,6 +47,11 @@ CacheModule.runJob("SUB", {
 			room: "admin.news",
 			args: ["event:admin.news.updated", { data: { news } }]
 		});
+
+		WSModule.runJob("EMIT_TO_ROOM", {
+			room: "news",
+			args: ["event:news.updated", { data: { news } }]
+		});
 	}
 });
 

+ 1 - 1
frontend/src/pages/Admin/tabs/News.vue

@@ -119,7 +119,7 @@ export default {
 			);
 		},
 		init() {
-			this.socket.dispatch("apis.joinAdminRoom", "news", () => {});
+			this.socket.dispatch("apis.joinAdminRoom", "news");
 		},
 		...mapActions("modalVisibility", ["openModal", "closeModal"]),
 		...mapActions("admin/news", [

+ 27 - 7
frontend/src/pages/News.vue

@@ -44,6 +44,8 @@ import { mapGetters } from "vuex";
 import marked from "marked";
 import { sanitize } from "dompurify";
 
+import ws from "@/ws";
+
 import MainHeader from "@/components/layout/MainHeader.vue";
 import MainFooter from "@/components/layout/MainFooter.vue";
 import UserIdToUsername from "@/components/UserIdToUsername.vue";
@@ -73,24 +75,42 @@ export default {
 		this.socket.dispatch("news.index", res => {
 			if (res.status === "success") this.news = res.data.news;
 		});
-		this.socket.on("event:admin.news.created", res =>
+
+		this.socket.on("event:news.created", res =>
 			this.news.unshift(res.data.news)
 		);
-		this.socket.on("event:admin.news.updated", res => {
+
+		this.socket.on("event:news.updated", res => {
+			if (res.data.news.status === "draft") {
+				this.news = this.news.filter(
+					item => item._id !== res.data.news._id
+				);
+				return;
+			}
+
 			for (let n = 0; n < this.news.length; n += 1) {
-				if (this.news[n]._id === res.data.news._id) {
-					this.$set(this.news, n, res.data.news);
-				}
+				if (this.news[n]._id === res.data.news._id)
+					this.$set(this.news, n, {
+						...this.news[n],
+						...res.data.news
+					});
 			}
 		});
-		this.socket.on("event:admin.news.removed", res => {
+
+		this.socket.on("event:news.removed", res => {
 			this.news = this.news.filter(item => item._id !== res.data.newsId);
 		});
+
+		if (this.socket.readyState === 1) this.init();
+		ws.onConnect(() => this.init());
 	},
 	methods: {
 		marked,
 		sanitize,
-		formatDistance
+		formatDistance,
+		init() {
+			this.socket.dispatch("apis.joinRoom", "news");
+		}
 	}
 };
 </script>