Browse Source

Songs live update on edit song/queuesong admin page.

KrisVos130 8 years ago
parent
commit
d2c1c99122

+ 5 - 4
backend/logic/actions/queueSongs.js

@@ -20,9 +20,10 @@ cache.sub('queue.removedSong', songId => {
 	utils.emitToRoom('admin.queue', 'event:admin.queueSong.removed', songId);
 });
 
-cache.sub('queue.updatedSong', songId => {
-	//TODO Retrieve new Song object
-	utils.emitToRoom('admin.queue', 'event:queueSong.updated', { songId });
+cache.sub('queue.update', songId => {
+	db.models.queueSong.findOne({_id: songId}, (err, song) => {
+		utils.emitToRoom('admin.queue', 'event:admin.queueSong.updated', song);
+	});
 });
 
 module.exports = {
@@ -79,7 +80,7 @@ module.exports = {
 				logger.error("QUEUE_UPDATE", `Updating queuesong "${songId}" failed for user ${userId}. "${err.message}"`);
 				return cb({status: 'failure', message: error});
 			}
-			cache.pub('queue.updatedSong', songId);
+			cache.pub('queue.update', songId);
 			logger.success("QUEUE_UPDATE", `User "${userId}" successfully update queuesong "${songId}".`);
 			return cb({status: 'success', message: 'Successfully updated song.'});
 		});

+ 8 - 1
backend/logic/actions/songs.js

@@ -13,11 +13,17 @@ cache.sub('song.removed', songId => {
 });
 
 cache.sub('song.added', songId => {
-	db.models.queueSong.findOne({_id: songId}, (err, song) => {
+	db.models.song.findOne({_id: songId}, (err, song) => {
 		utils.emitToRoom('admin.songs', 'event:admin.song.added', song);
 	});
 });
 
+cache.sub('song.updated', songId => {
+	db.models.song.findOne({_id: songId}, (err, song) => {
+		utils.emitToRoom('admin.songs', 'event:admin.song.updated', song);
+	});
+});
+
 cache.sub('song.like', (data) => {
 	utils.emitToRoom(`song.${data.songId}`, 'event:song.like', {songId: data.songId, likes: data.likes, dislikes: data.dislikes});
 	utils.socketsFromUser(data.userId, (sockets) => {
@@ -68,6 +74,7 @@ module.exports = {
 			if (err) console.error(err);
 			songs.updateSong(songId, (err, song) => {
 				if (err) console.error(err);
+				cache.pub('song.updated', song._id);
 				cb({ status: 'success', message: 'Song has been successfully updated', data: song });
 			});
 		});

+ 8 - 0
frontend/components/Admin/QueueSongs.vue

@@ -97,6 +97,14 @@
 							return song._id !== songId;
 						});
 					});
+					_this.socket.on('event:admin.queueSong.updated', updatedSong => {
+						for (let i = 0; i < _this.songs.length; i++) {
+							let song = _this.songs[i];
+							if (song._id === updatedSong._id) {
+								_this.songs.$set(i, updatedSong);
+							}
+						}
+					});
 				}
 				io.onConnect(() => {
 					_this.init();

+ 8 - 0
frontend/components/Admin/Songs.vue

@@ -100,6 +100,14 @@
 							return song._id !== songId;
 						});
 					});
+					_this.socket.on('event:admin.song.updated', updatedSong => {
+						for (let i = 0; i < _this.songs.length; i++) {
+							let song = _this.songs[i];
+							if (song._id === updatedSong._id) {
+								_this.songs.$set(i, updatedSong);
+							}
+						}
+					});
 				}
 				io.onConnect(() => {
 					_this.init();