Browse Source

phew... playlist track addition action finally works

miskson 3 years ago
parent
commit
2e53a09406
1 changed files with 16 additions and 26 deletions
  1. 16 26
      hw22-dropzone-uploads/src/App.js

+ 16 - 26
hw22-dropzone-uploads/src/App.js

@@ -148,35 +148,25 @@ const actionAddTrack = file =>
     await dispatch(actionGetTrackById(getState().promise.loadFile.payload?._id))
     await dispatch(actionGetPlaylistById(playlistId))
 
-    let arr = [...getState().promise.track.payload]
-    arr.push(...getState().promise.playlistTracks.payload[0].tracks)
+    let arr = [{_id: getState().promise.loadFile.payload?._id}]
+    getState().promise.playlistTracks.payload[0].tracks.forEach(track => arr.push({_id: track._id}))
     console.log('newarr', arr)
-    //let updated = JSON.stringify(arr)
-    //let trackId = getState().promise?.loadFile?.payload?._id
-
-    // await dispatch(actionPromise('trackToPlaylist', gql(`
-    //   mutation {
-    //     PlaylistUpsert(playlist:{ _id: "${playlistId}", tracks: {_id: "${trackId}"}}) {
-    //       _id, name, tracks { _id, originalFileName }
-    //     }
-    //   }
-    // `)))
-
-  await dispatch(actionPromise('trackToPlaylist', gql(`
-      mutation {
-        PlaylistUpsert(playlist:{ _id: "${playlistId}", tracks: [{_id: "61e4b26ec2305e2f502acaae"}, {_id: "61e4a4cac2305e2f502aca98"}] })] {
-          _id, name, tracks { _id, originalFileName }
+
+  // await dispatch(actionPromise('trackToPlaylist', gql(`
+  //     mutation {
+  //       PlaylistUpsert(playlist:{ _id: "${playlistId}", tracks: [{_id: "61e4b26ec2305e2f502acaae"}, {_id: "61e4a4cac2305e2f502aca98"}] }) {
+  //         _id, name, tracks { _id, originalFileName }
+  //       }
+  //     }
+  //   `)))
+
+    await dispatch(actionPromise('trackToPlaylist', gql(`
+      mutation($playlistId: ID, $newTracks: [TrackInput]) {
+        PlaylistUpsert(playlist:{ _id: $playlistId, tracks: $newTracks}) {
+          _id, name, tracks { _id, originalFileName, }
         }
       }
-    `)))
-
-    // await dispatch(actionPromise('trackToPlaylist', gql(`
-    //   mutation {
-    //     PlaylistUpsert(playlist:{ _id: $playlistId, tracks: $tracks}}) {
-    //       _id, name, tracks { _id, originalFileName }
-    //     }
-    //   }
-    // `, { playlistId:JSON.stringify([{ playlistId }]),  tracks:  JSON.stringify(arr) })))
+    `, { playlistId: playlistId ,  newTracks: arr })))
 
     dispatch(actionGetUserTracks())
   }