|
@@ -1,34 +1,117 @@
|
|
|
-export function playerReducer(state={},{
|
|
|
- type,
|
|
|
- isPlaying=false,
|
|
|
- isStopped,
|
|
|
- duration,
|
|
|
- track,
|
|
|
- // playlist: {_id, name, tracks},
|
|
|
- playlistIndex,
|
|
|
- currentTime=0,
|
|
|
- volume,
|
|
|
-}) {
|
|
|
-
|
|
|
- if (type === 'TRACK_PLAY') {
|
|
|
+import {store} from "./store";
|
|
|
+import {actionPromise} from "./promiseReducer";
|
|
|
+import {gql} from "../actions";
|
|
|
+
|
|
|
+export const playerReducer = (
|
|
|
+ state = {},
|
|
|
+ {type,track,isPlaying=false,isPaused,duration,
|
|
|
+ playlist=[],playlistIndex,currentTime=0,volume=1,}) => {
|
|
|
+
|
|
|
+ if (type === "TRACK_PLAY") {
|
|
|
return {
|
|
|
...state,
|
|
|
isPlaying,
|
|
|
- isStopped: !isPlaying,
|
|
|
+ isPaused: !isPlaying,
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ if (type === "TRACK_STOP") {
|
|
|
+ return {
|
|
|
+ ...state,
|
|
|
+ isPaused,
|
|
|
+ isPlaying: !isPaused,
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ if (type === "TRACK_VOLUME") {
|
|
|
+ return {
|
|
|
+ ...state,
|
|
|
+ volume,
|
|
|
};
|
|
|
}
|
|
|
- if (type === 'TRACK_STOP') {
|
|
|
+
|
|
|
+ if (type === "TRACK_DURATION") {
|
|
|
+ return {
|
|
|
+ ...state,
|
|
|
+ duration,
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ if (type === "TRACK_CURRENT_TIME") {
|
|
|
return {
|
|
|
...state,
|
|
|
- isStopped,
|
|
|
- isPlaying: !isStopped,
|
|
|
+ currentTime,
|
|
|
};
|
|
|
}
|
|
|
|
|
|
- return state
|
|
|
+ if (type === "SET_PLAYLIST") {
|
|
|
+ return {
|
|
|
+ ...state,
|
|
|
+ playlist,
|
|
|
+ };
|
|
|
+ }
|
|
|
+ return state;
|
|
|
+};
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+const audio = new Audio()
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+export const actionTrackPlay = (audioSrc) => {
|
|
|
+ audio.src = audioSrc
|
|
|
+ audio.load()
|
|
|
+ audio.ondurationchange = function(){actionSetDuration()}
|
|
|
+ audio.ontimeupdate = function(){actionTrackCurrentTime()}
|
|
|
+ audio.play()
|
|
|
+ store.dispatch({type:"TRACK_PLAY",isPlaying:true,isPaused:false})
|
|
|
+ store.dispatch(actionFullSetPlaylist())
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+export const actionTrackStop = () => {
|
|
|
+ audio.pause()
|
|
|
+ store.dispatch({type:"TRACK_STOP",isPlaying:false,isPaused:true})
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+export const actionTrackVolume = (e) => {
|
|
|
+ audio.volume = e
|
|
|
+ store.dispatch({type:"TRACK_VOLUME", volume:e})
|
|
|
}
|
|
|
|
|
|
-export const actionTrackPlay = (track,isPlaying) => ({type: "TRACK_PLAY",isPlaying: track.play()})
|
|
|
-export const actionTrackStop = (track,isStopped) => ({type: "TRACK_PLAY",isStopped: track.pause()})
|
|
|
|
|
|
-//все сломалось
|
|
|
+export const actionSetDuration = () => {
|
|
|
+ let e = audio.duration
|
|
|
+ store.dispatch({type:"TRACK_DURATION",duration:e})
|
|
|
+}
|
|
|
+
|
|
|
+export const actionTrackCurrentTime = () => {
|
|
|
+ let e = audio.currentTime
|
|
|
+ store.dispatch({type:"TRACK_CURRENT_TIME", currentTime:e})
|
|
|
+}
|
|
|
+
|
|
|
+const actionSetPlaylist = (tracks) => {
|
|
|
+ console.log(tracks)
|
|
|
+ store.dispatch({type:"SET_PLAYLIST",playlist:tracks})
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+const actionPlaylistByIdForPlayer = (match) =>
|
|
|
+ actionPromise('playlistByIdForPlayer', gql(`query playlistByIdForPlayer($q: String){
|
|
|
+ PlaylistFindOne(query: $q){
|
|
|
+ _id name owner {login} tracks {_id url originalFileName}
|
|
|
+ }
|
|
|
+}`, { q: JSON.stringify([{ _id: match.params._id}]) }))
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+export const actionFullSetPlaylist = (_id) =>
|
|
|
+ async function i(dispatch) {
|
|
|
+ let playlist = await dispatch(actionPlaylistByIdForPlayer(_id));
|
|
|
+ if (playlist) {
|
|
|
+ dispatch(actionSetPlaylist(playlist));
|
|
|
+ }
|
|
|
+ }
|