{"size": 1348, "ext": "js", "max_stars_repo_path": "src/store/actions.js", "max_stars_repo_name": "okbug/react-music", "max_stars_repo_stars_event_min_datetime": "2018-04-23T12:08:29.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-08T13:15:52.000Z", "max_issues_repo_path": "src/store/actions.js", "max_issues_repo_name": "okbug/react-music", "max_issues_repo_issues_event_min_datetime": "2019-06-22T11:21:21.000Z", "max_issues_repo_issues_event_max_datetime": "2020-07-28T07:27:57.000Z", "max_forks_repo_path": "src/store/actions.js", "max_forks_repo_name": "okbug/react-music", "max_forks_repo_forks_event_min_datetime": "2018-04-23T12:08:13.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-08T13:17:45.000Z"}
{"max_stars_count": 343, "max_issues_count": 10, "max_forks_count": 92, "avg_line_length": 27.5102040816, "max_line_length": 71, "alphanum_fraction": 0.7381305638}
import * as ActionTypes from './actionTypes'
import { findIndex } from '@/utils/utils'

// 显示Player组件
export function setShowPlayer(showPlayer) {
  return { type: ActionTypes.SET_SHOW_PLAYER, showPlayer }
}

// 设置当前音乐
export function setCurrentMusic(currentMusic) {
  return { type: ActionTypes.SET_CURRENTMUSIC, currentMusic }
}

// 设置当前音乐索引
export function setCurrentIndex(currentIndex) {
  return { type: ActionTypes.SET_CURRENTINDEX, currentIndex }
}

// 设置当前播放列表
export function setPlayList(playList) {
  return { type: ActionTypes.SET_PLAYLIST, playList }
}

// 播放歌曲（替换歌单列表）
export const setAllPlay = ({ playList, currentIndex }) => dispatch => {
  dispatch(setShowPlayer(true))
  dispatch(setPlayList(playList))
  dispatch(setCurrentIndex(currentIndex))
  dispatch(setCurrentMusic(playList[currentIndex]))
}

// 播放歌曲（插入一条到播放列表）
export const addPlay = music => (dispatch, getState) => {
  let playList = [...getState().playList]
  //查询当前播放列表是否有待插入的音乐，并返回其索引
  let index = findIndex(playList, music)
  //当前播放列表有待插入的音乐时，直接改变当前播放音乐的索引
  if (index > -1) {
    dispatch(setCurrentIndex(index))
    dispatch(setCurrentMusic(playList[index]))
  } else {
    index = playList.push(music) - 1
    dispatch(setPlayList(playList))
    dispatch(setCurrentIndex(index))
    dispatch(setCurrentMusic(playList[index]))
  }
  dispatch(setShowPlayer(true))
}
