Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #13: Add play previous song. #34

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/actions/nowPlayingActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export const actionType = {
songAdd: 'SONG_ADD',
songRemove: 'SONG_REMOVE',
playNext: 'PLAY_NEXT',
playPrev: 'PLAY_PREV',
playedNext: 'PLAYED_NEXT',
getSuggestions: 'GET_SUGGESTIONS'
};
Expand All @@ -20,6 +21,10 @@ export function playNext() {
return dispatch => dispatch({type: actionType.playNext});
}

export function playPrev() {
return dispatch => dispatch({ type: actionType.playPrev })
}

export function playedNext(song) {
return dispatch => dispatch({type: actionType.playedNext, song});
}
Expand Down
9 changes: 9 additions & 0 deletions src/components/player/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,15 @@ export default class Player extends React.Component {
case keys.N:
this.props.playNext();
break;
case keys.P:
let existPrevious = this.props.prevSongs.length >= 2;
if(this.audioElement.currentTime <= 5 && existPrevious) {
this.props.playPrev();
this.props.playNext();
} else {
this.audioElement.currentTime = 0;
}
break;
case keys.F:
let searchElement = document.getElementById('search-input');
if (searchElement) {
Expand Down
2 changes: 0 additions & 2 deletions src/components/sideBar/miniCard/miniCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ export default class MiniCard extends React.Component {
videoTitle = videoTitle.substring(0, MAX_TITLE_LENGTH)+'... ';
}

console.log(this.props.name);

return (
<div className='song-card-list uk-margin-small-bottom uk-flex'>
<div className="uk-flex" onClick={this.play.bind(this)}>
Expand Down
3 changes: 2 additions & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export const keys = {
UP_ARROW: 38,
RIGHT_ARROW: 39,
DOWN_ARROW: 40,
ENTER: 13
ENTER: 13,
P: 80
}
8 changes: 5 additions & 3 deletions src/containers/playerContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import {connect} from 'react-redux';

import Player from '../components/player';
import {playSong} from "../actions/playerActions";
import {playNext, getSuggestions, playedNext} from "../actions/nowPlayingActions";
import {playNext, getSuggestions, playedNext, playPrev} from "../actions/nowPlayingActions";
import {activatePlaylist, deactivatePlaylist} from '../actions/playerActions';

function mapStateToProps(state) {
return {
currentSong: state.player.currentSong,
playlistActive: state.player.playlistActive
playlistActive: state.player.playlistActive,
prevSongs: state.nowPlaying.previousSongs
}
}

Expand All @@ -20,6 +21,7 @@ export default connect(
getSuggestions: getSuggestions,
playedNext: playedNext,
activatePlaylist,
deactivatePlaylist
deactivatePlaylist,
playPrev
}
)(Player);
15 changes: 15 additions & 0 deletions src/reducers/nowPlayingReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ export function nowPlaying(state = initialState, action) {
}
return {...state, dispatchNext: false, previousSongs: prev};

case actionType.playPrev:
if(state.previousSongs.length >= 2) {
let prev = state.previousSongs.slice(0, state.previousSongs.length - 2)
let songsToPlay = state.previousSongs.slice(state.previousSongs.length - 2)
let next = state.nextSongs.slice(0)
next = next.filter(item => !songInArray(item, songsToPlay));
next.unshift(...songsToPlay)
return {
...state,
previousSongs: prev,
nextSongs: next
}
}
return state;

case actionType.getSuggestions:
if (action.suggestedSongs) {
suggestions = removeDuplicateIn(removeDuplicateIn(action.suggestedSongs, state.previousSongs), state.nextSongs);
Expand Down