Browse Source

fix(SortablePlaylists): when order changes all socket listeners have their order kept in sync

Signed-off-by: Jonathan <theflametrooper@gmail.com>
Jonathan 4 years ago
parent
commit
ff3aa5d2f4

+ 25 - 0
backend/logic/actions/users.js

@@ -29,6 +29,22 @@ CacheModule.runJob("SUB", {
 	}
 });
 
+CacheModule.runJob("SUB", {
+	channel: "user.updateOrderOfPlaylists",
+	cb: res => {
+		IOModule.runJob("SOCKETS_FROM_USER", { userId: res.userId }, this).then(response => {
+			response.sockets.forEach(socket => {
+				socket.emit("event:user.orderOfPlaylists.changed", res.orderOfPlaylists);
+			});
+		});
+
+		IOModule.runJob("EMIT_TO_ROOM", {
+			room: `profile-${res.userId}`,
+			args: ["event:user.orderOfPlaylists.changed", res.orderOfPlaylists]
+		});
+	}
+});
+
 CacheModule.runJob("SUB", {
 	channel: "user.updateUsername",
 	cb: user => {
@@ -589,6 +605,7 @@ export default {
 						channel: "user.removeSessions",
 						value: userId
 					});
+
 					async.each(
 						keys,
 						(sessionId, callback) => {
@@ -664,6 +681,14 @@ export default {
 					return cb({ status: "failure", message: err });
 				}
 
+				CacheModule.runJob("PUB", {
+					channel: "user.updateOrderOfPlaylists",
+					value: {
+						orderOfPlaylists,
+						userId: session.userId
+					}
+				});
+
 				this.log(
 					"SUCCESS",
 					"UPDATE_ORDER_OF_USER_PLAYLISTS",

+ 16 - 0
frontend/src/pages/Profile.vue

@@ -382,6 +382,22 @@ export default {
 							});
 						});
 
+						this.socket.on(
+							"event:user.orderOfPlaylists.changed",
+							orderOfPlaylists => {
+								const sortedPlaylists = [];
+
+								this.playlists.forEach(playlist => {
+									sortedPlaylists[
+										orderOfPlaylists.indexOf(playlist._id)
+									] = playlist;
+								});
+
+								this.playlists = sortedPlaylists;
+								this.orderOfPlaylists = this.calculatePlaylistOrder();
+							}
+						);
+
 						if (this.user._id === this.userId) {
 							this.socket.emit(
 								"activities.getSet",

+ 16 - 0
frontend/src/pages/Station/components/Sidebar/MyPlaylists.vue

@@ -139,6 +139,22 @@ export default {
 					}
 				});
 			});
+
+			this.socket.on(
+				"event:user.orderOfPlaylists.changed",
+				orderOfPlaylists => {
+					const sortedPlaylists = [];
+
+					this.playlists.forEach(playlist => {
+						sortedPlaylists[
+							orderOfPlaylists.indexOf(playlist._id)
+						] = playlist;
+					});
+
+					this.playlists = sortedPlaylists;
+					this.orderOfPlaylists = this.calculatePlaylistOrder();
+				}
+			);
 		});
 	},
 	beforeDestroy() {