index.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { setTrack } from '../actions'
  2. import { jwtDecode } from '../App'
  3. export function authReducer(state, { type, token }) {
  4. if (!state) {
  5. if (localStorage.authToken) {
  6. type = 'AUTH_LOGIN'
  7. token = localStorage.authToken
  8. } else {
  9. return {}
  10. }
  11. }
  12. if (type === 'AUTH_LOGIN') {
  13. //console.log('TOXEN', jwtDecode(token))
  14. let auth = jwtDecode(token)
  15. if (auth) {
  16. localStorage.authToken = token
  17. return { token, payload: auth }
  18. }
  19. }
  20. if (type === 'AUTH_LOGOUT') {
  21. localStorage.authToken = ''
  22. return {}
  23. }
  24. return state
  25. }
  26. export function promiseReducer(state = {}, { type, name, status, payload, error }) {
  27. if (type === 'PROMISE') {
  28. return {
  29. ...state,
  30. [name]: { status, payload: (state === 'PENDING' && state[name] && state[name].payload) || payload, error }
  31. }
  32. }
  33. return state
  34. }
  35. export function playerReducer(state, { type, track, playlist, duration, currentTime, volume }) {
  36. if (!state || type === 'EJECT_TRACK') {
  37. return {}
  38. }
  39. if (type === 'PLAY_TRACK') {
  40. return { ...state, 'isPlaying': true }
  41. }
  42. if (type === 'PAUSE_TRACK') {
  43. return { ...state, 'isPlaying': false, 'currentTime': currentTime }
  44. }
  45. if (type === 'SET_CURRTIME') {
  46. console.log('set_currtime')
  47. return { ...state, 'currentTime': currentTime }
  48. }
  49. if (type === 'SET_DURATION') {
  50. console.log('SET_DURATION')
  51. return { ...state, 'duration': duration }
  52. }
  53. if (type === 'SET_VOLUME') {
  54. return { ...state, 'volume': volume }
  55. }
  56. if (type === 'SET_TRACK') {
  57. console.log('setTrack')
  58. return {
  59. isPlaying: false,
  60. track: track,
  61. playlist: playlist,
  62. duration: duration, //общая длительность трека
  63. currentTime: currentTime,// текущая позиция в треке
  64. volume: volume,
  65. playlistIndex: playlist.indexOf(track)
  66. }
  67. }
  68. return state
  69. }
  70. export const localStoreReducer = (reducer, localStorageName) => {
  71. return (
  72. (state, action) => {
  73. let newState
  74. if (!state && localStorage.getItem(localStorageName)) {
  75. newState = JSON.parse(localStorage.getItem(localStorageName))
  76. console.log(newState)
  77. } else {
  78. newState = reducer(state, action)
  79. localStorage.setItem(localStorageName, JSON.stringify(newState))
  80. }
  81. return newState
  82. }
  83. )
  84. }