|
@@ -1,4 +1,89 @@
|
|
|
-import { jwtDecode, gql, backendURL } from '../App'
|
|
|
+import { jwtDecode, gql, backendURL, store } from '../App'
|
|
|
+
|
|
|
+const audio = new Audio()
|
|
|
+
|
|
|
+const actionTrackSet = (track, audio, playlist) =>
|
|
|
+({
|
|
|
+ type: 'SET_TRACK',
|
|
|
+ track: track,
|
|
|
+ playlist: playlist,
|
|
|
+ duration: audio.duration,
|
|
|
+ currentTime: audio.currentTime,
|
|
|
+ volume: audio.volume
|
|
|
+})
|
|
|
+
|
|
|
+const actionTrackSetDuration = (time) => ({ type: 'SET_DURATION', duration: time })
|
|
|
+
|
|
|
+const actionTrackSetCurrTime = (time) => ({ type: 'SET_CURRTIME', currentTime: time })
|
|
|
+
|
|
|
+const actionTrackSetVolume = (value) => ({ type: 'SET_VOLUME', volume: value })
|
|
|
+const actionTrackPlay = () => ({ type: 'PLAY_TRACK' })
|
|
|
+const actionTrackPause = (currentTime) => ({ type: 'PAUSE_TRACK', currentTime: currentTime })
|
|
|
+
|
|
|
+const actionPlaylistSet = (playlist) => ({type: 'SET_PLAYLIST', playlist: playlist})
|
|
|
+const actionSetPlaylistIndex = (index) => ({type: 'SET_INDEX', playlistIndex: index})
|
|
|
+
|
|
|
+export const setPlaylist = (playlist) => dispatch => dispatch(actionPlaylistSet(playlist))
|
|
|
+export const setIndex = (index) => dispatch => dispatch(actionSetPlaylistIndex(index))
|
|
|
+
|
|
|
+export const setTrack = (track, playlist) =>
|
|
|
+ dispatch => {
|
|
|
+ audio.src = backendURL + '/' + track.url
|
|
|
+ dispatch(actionTrackSet(track, audio, playlist))
|
|
|
+ }
|
|
|
+
|
|
|
+export const switchTrack = (isForward, currentTrackIndex, playlist) =>
|
|
|
+ dispatch => {
|
|
|
+ let playlistLength = playlist.constructor.name ==='Array'? playlist.length - 1 : playlist.tracks.length - 1
|
|
|
+ let tracks = playlist.constructor.name ==='Array'? playlist : playlist.tracks
|
|
|
+ console.log('stuff', tracks, playlistLength)
|
|
|
+
|
|
|
+ if (isForward ? currentTrackIndex < playlistLength : currentTrackIndex > 0) {
|
|
|
+ dispatch(setTrack(tracks[currentTrackIndex + (isForward ? 1 : -1)], playlist))
|
|
|
+ } else if (currentTrackIndex === (isForward ? playlistLength : 0)) {
|
|
|
+ dispatch(setTrack(tracks[isForward ? 0 : playlistLength], playlist))
|
|
|
+ }
|
|
|
+ dispatch(playTrack())
|
|
|
+ }
|
|
|
+
|
|
|
+export const playTrack = () =>
|
|
|
+ dispatch => {
|
|
|
+ dispatch(actionTrackPlay())
|
|
|
+ audio.play()
|
|
|
+ }
|
|
|
+
|
|
|
+export const pauseTrack = () =>
|
|
|
+ dispatch => {
|
|
|
+ dispatch(actionTrackPause(audio.currentTime))
|
|
|
+ audio.pause()
|
|
|
+ }
|
|
|
+
|
|
|
+export const setTrackVolume = (volume) =>
|
|
|
+ dispatch => {
|
|
|
+ audio.volume = volume
|
|
|
+ dispatch(actionTrackSetVolume(volume))
|
|
|
+ }
|
|
|
+
|
|
|
+export const setNewTrackCurrentTime = (time) =>
|
|
|
+ (dispatch, getState) => {
|
|
|
+ audio.pause()
|
|
|
+ audio.currentTime = time
|
|
|
+ getState().player.isPlaying? audio.play() : audio.pause()
|
|
|
+ dispatch(actionTrackSetCurrTime(time))
|
|
|
+ }
|
|
|
+
|
|
|
+audio.onended = () => store.dispatch(switchTrack(true, store.getState().player.playlistIndex, store.getState().player.playlist))
|
|
|
+audio.ondurationchange = (e) => store.dispatch(actionTrackSetDuration(e.target.duration))
|
|
|
+audio.ontimeupdate = (e) => store.dispatch(actionTrackSetCurrTime(e.target.currentTime))
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
const actionPending = name => ({ type: 'PROMISE', status: 'PENDING', name })
|
|
|
const actionResolved = (name, payload) => ({ type: 'PROMISE', status: 'RESOLVED', name, payload })
|
|
@@ -66,18 +151,18 @@ export const actionGetUserData = () => {
|
|
|
|
|
|
export const actionGetUserPlaylists = () => {
|
|
|
let _id = jwtDecode(localStorage.authToken).sub.id
|
|
|
- return(
|
|
|
+ return (
|
|
|
actionPromise('userPlaylists', gql(`
|
|
|
query getPlaylistByOwnerId($ownerId:String!) {
|
|
|
PlaylistFind(query: $ownerId) {
|
|
|
_id, name
|
|
|
}
|
|
|
}
|
|
|
- `, { ownerId: JSON.stringify([{ ___owner: _id }]) } ))
|
|
|
+ `, { ownerId: JSON.stringify([{ ___owner: _id }]) }))
|
|
|
)
|
|
|
}
|
|
|
|
|
|
-export const actionGetPlaylistById = (_id/*='5fe35e5ce926687ee86b0a4f'*/) =>
|
|
|
+export const actionGetPlaylistById = (_id) =>
|
|
|
actionPromise('playlistTracks', gql(`
|
|
|
query playlistById($playlistId: String!) {
|
|
|
PlaylistFind(query: $playlistId) {
|
|
@@ -94,8 +179,7 @@ export const actionGetPlaylistById = (_id/*='5fe35e5ce926687ee86b0a4f'*/) =>
|
|
|
|
|
|
export const actionGetUserTracks = () => {
|
|
|
let _id = jwtDecode(localStorage.authToken).sub.id
|
|
|
- //let _id = '5fe35e1ce926687ee86b0a3f' //newUserId
|
|
|
- return(
|
|
|
+ return (
|
|
|
actionPromise('userTracks', gql(`
|
|
|
query getUserTracks($ownerId: String!) {
|
|
|
TrackFind(query: $ownerId) {
|
|
@@ -103,11 +187,11 @@ export const actionGetUserTracks = () => {
|
|
|
id3 { title, artist, album }
|
|
|
}
|
|
|
}
|
|
|
- `, { ownerId: JSON.stringify([{ ___owner: _id }]) } ))
|
|
|
+ `, { ownerId: JSON.stringify([{ ___owner: _id }]) }))
|
|
|
)
|
|
|
}
|
|
|
|
|
|
-export const actionAddPlaylist = playlistName =>
|
|
|
+export const actionAddPlaylist = playlistName =>
|
|
|
async dispatch => {
|
|
|
await dispatch(actionPromise('addPlaylist', gql(`
|
|
|
mutation addPlaylist ($playlistName: String!){
|
|
@@ -115,66 +199,70 @@ export const actionAddPlaylist = playlistName =>
|
|
|
_id, name
|
|
|
}
|
|
|
}
|
|
|
- `, {playlistName: playlistName})))
|
|
|
+ `, { playlistName: playlistName })))
|
|
|
dispatch(actionGetUserPlaylists())
|
|
|
}
|
|
|
|
|
|
export const actionLoadFile = (file, type) => {
|
|
|
let fd = new FormData()
|
|
|
console.log('TYPE', type)
|
|
|
- fd.append(type === 'upload'? 'photo' : type, file)
|
|
|
-
|
|
|
+ fd.append(type === 'upload' ? 'photo' : type, file)
|
|
|
+
|
|
|
return (
|
|
|
- actionPromise('loadFile', fetch(backendURL + `/${type}`,{
|
|
|
- method: "POST",
|
|
|
- headers: localStorage.authToken ? {Authorization: 'Bearer ' + localStorage.authToken} : {},
|
|
|
- body: fd
|
|
|
+ actionPromise('loadFile', fetch(backendURL + `/${type}`, {
|
|
|
+ method: "POST",
|
|
|
+ headers: localStorage.authToken ? { Authorization: 'Bearer ' + localStorage.authToken } : {},
|
|
|
+ body: fd
|
|
|
})
|
|
|
- .then(res => res.json())
|
|
|
+ .then(res => res.json())
|
|
|
)
|
|
|
)
|
|
|
}
|
|
|
|
|
|
-export const actionUpdatePlaylist = (playlistId, updPlaylist) => {
|
|
|
- console.log('UPDATING', playlistId, updPlaylist)
|
|
|
- return (
|
|
|
- actionPromise('trackToPlaylist', gql(`
|
|
|
+export const actionUpdatePlaylist = (playlistId, updPlaylist) =>
|
|
|
+ async dispatch => {
|
|
|
+ await dispatch(actionPromise('trackToPlaylist', gql(`
|
|
|
mutation($playlistId: ID, $newTracks: [TrackInput]) {
|
|
|
PlaylistUpsert(playlist:{ _id: $playlistId, tracks: $newTracks}) {
|
|
|
- _id, name, tracks { _id, originalFileName, }
|
|
|
+ _id, name, tracks { _id, url, originalFileName, id3{ title, artist, album } }
|
|
|
}
|
|
|
}
|
|
|
- `, { playlistId: playlistId , newTracks: updPlaylist }))
|
|
|
+ `, { playlistId: playlistId, newTracks: updPlaylist }))
|
|
|
+ )
|
|
|
+ await dispatch(actionGetPlaylistById(playlistId))
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
- )
|
|
|
-}
|
|
|
export const actionUploadUserTrack = (file, playlistId) =>
|
|
|
async (dispatch, getState) => {
|
|
|
await dispatch(actionLoadFile(file, 'track'))
|
|
|
|
|
|
- if(!playlistId) {
|
|
|
- dispatch(actionGetUserTracks())
|
|
|
+ if (!playlistId) {
|
|
|
+ await dispatch(actionGetUserTracks())
|
|
|
+ if(getState().player.track && getState().player.playlist.constructor.name === 'Array') {
|
|
|
+ console.log('HERE', getState().promise.userTracks.payload)
|
|
|
+ dispatch(setPlaylist(getState().promise.userTracks.payload.reverse()))
|
|
|
+ dispatch(setIndex(getState().player.playlist.map(item => item._id).indexOf(getState().player.track._id)))
|
|
|
+ }
|
|
|
} else {
|
|
|
- console.log('UPLOADING TO PLAYLIS')
|
|
|
let updPlaylist = []
|
|
|
let oldPlaylist = getState().promise.playlistTracks.payload[0].tracks
|
|
|
|
|
|
- if(oldPlaylist) {
|
|
|
- //console.log('id pashet', oldPlaylist)
|
|
|
- oldPlaylist.forEach(track => updPlaylist.push({_id: track._id}))
|
|
|
+ if (oldPlaylist) oldPlaylist.forEach(track => updPlaylist.push({ _id: track._id }))
|
|
|
+ updPlaylist.unshift({ _id: getState().promise.loadFile.payload?._id })
|
|
|
+ await dispatch(actionUpdatePlaylist(playlistId, updPlaylist))
|
|
|
+
|
|
|
+ if(getState().player.track && getState().player.playlist._id === playlistId) {
|
|
|
+ dispatch(setPlaylist(getState().promise.trackToPlaylist.payload))
|
|
|
+ dispatch(setIndex(updPlaylist.map(item => item._id).indexOf(getState().player.track._id)))
|
|
|
}
|
|
|
- updPlaylist.push({_id: getState().promise.loadFile.payload?._id})
|
|
|
- console.log('UPDATED PLST', updPlaylist)
|
|
|
-
|
|
|
- await dispatch(actionUpdatePlaylist(playlistId, updPlaylist.reverse()))
|
|
|
- dispatch(actionGetPlaylistById(playlistId))
|
|
|
}
|
|
|
}
|
|
|
|
|
|
export const actionUploadAvatar = (file) =>
|
|
|
async (dispatch, getState) => {
|
|
|
- await dispatch(actionLoadFile(file, 'upload'))
|
|
|
- await dispatch(actionPromise('setAvatar', gql(`
|
|
|
+ await dispatch(actionLoadFile(file, 'upload'))
|
|
|
+ await dispatch(actionPromise('setAvatar', gql(`
|
|
|
mutation {
|
|
|
UserUpsert(user:{_id: "${jwtDecode(localStorage.authToken).sub.id}", avatar: {_id: "${getState().promise?.loadFile?.payload?._id}"}}){
|
|
|
_id, login, avatar{
|
|
@@ -183,5 +271,5 @@ export const actionUploadAvatar = (file) =>
|
|
|
}
|
|
|
}
|
|
|
`)))
|
|
|
- dispatch(actionGetUserData())
|
|
|
+ dispatch(actionGetUserData())
|
|
|
}
|