Browse Source

refactor: started fixing TS errors in AddToPlaylistDropdown

Kristian Vos 1 year ago
parent
commit
fa2b923f93

+ 15 - 6
frontend/src/components/AddToPlaylistDropdown.vue

@@ -2,6 +2,11 @@
 import { ref, onMounted } from "vue";
 import Toast from "toasters";
 import { storeToRefs } from "pinia";
+import {
+	AddSongToPlaylistResponse,
+	IndexMyPlaylistsResponse,
+	RemoveSongFromPlaylistResponse
+} from "@musare_types/actions/PlaylistsActions";
 import { useWebsocketsStore } from "@/stores/websockets";
 import { useUserPlaylistsStore } from "@/stores/userPlaylists";
 import { useModalsStore } from "@/stores/modals";
@@ -31,10 +36,14 @@ const { openModal } = useModalsStore();
 
 const init = () => {
 	if (!fetchedPlaylists.value)
-		socket.dispatch("playlists.indexMyPlaylists", res => {
-			if (res.status === "success")
-				if (!fetchedPlaylists.value) setPlaylists(res.data.playlists);
-		});
+		socket.dispatch(
+			"playlists.indexMyPlaylists",
+			(res: IndexMyPlaylistsResponse) => {
+				if (res.status === "success")
+					if (!fetchedPlaylists.value)
+						setPlaylists(res.data.playlists);
+			}
+		);
 };
 const hasSong = playlist =>
 	playlist.songs.map(song => song.youtubeId).indexOf(props.song.youtubeId) !==
@@ -47,14 +56,14 @@ const toggleSongInPlaylist = playlistIndex => {
 			false,
 			props.song.youtubeId,
 			playlist._id,
-			res => new Toast(res.message)
+			(res: AddSongToPlaylistResponse) => new Toast(res.message)
 		);
 	} else {
 		socket.dispatch(
 			"playlists.removeSongFromPlaylist",
 			props.song.youtubeId,
 			playlist._id,
-			res => new Toast(res.message)
+			(res: RemoveSongFromPlaylistResponse) => new Toast(res.message)
 		);
 	}
 };

+ 18 - 0
types/actions/PlaylistsActions.ts

@@ -0,0 +1,18 @@
+import { PlaylistModel, PlaylistSong } from "../models/Playlist";
+import { BaseResponse } from "./BaseActions";
+
+export type IndexMyPlaylistsResponse = BaseResponse & {
+	data: {
+		playlists: (PlaylistModel & { weight: number })[];
+	};
+};
+export type AddSongToPlaylistResponse = BaseResponse & {
+	data: {
+		songs: PlaylistSong[];
+	};
+};
+export type RemoveSongFromPlaylistResponse = BaseResponse & {
+	data: {
+		songs: PlaylistSong[];
+	};
+};

+ 24 - 0
types/models/Playlist.ts

@@ -0,0 +1,24 @@
+// TODO check if all of these properties are always present
+export type PlaylistSong = {
+	_id: string;
+	youtubeId: string;
+	title: string;
+	artists: string[];
+	duration: number;
+	skipDuration: number;
+	thumbnail: string;
+	verified: boolean;
+};
+
+export type PlaylistModel = {
+	_id: string;
+	displayName: string;
+	songs: PlaylistSong[];
+	createdBy: string;
+	// TODO check if it's a date or a string, might be wrong
+	createdAt: Date;
+	createdFor: string | null;
+	privacy: "public" | "private";
+	type: "user" | "user-liked" | "user-disliked" | "genre" | "station";
+	documentVersion: number;
+};