import { jwtDecode } from '../App' export function authReducer(state, { type, token }) { if (!state) { if (localStorage.authToken) { type = 'AUTH_LOGIN' token = localStorage.authToken } else { return {} } } if (type === 'AUTH_LOGIN') { let auth = jwtDecode(token) if (auth) { localStorage.authToken = token return { token, payload: auth } } } if (type === 'AUTH_LOGOUT') { localStorage.authToken = '' return {} } return state } export function promiseReducer(state = {}, { type, name, status, payload, error }) { if (type === 'PROMISE') { return { ...state, [name]: { status, payload: (state === 'PENDING' && state[name] && state[name].payload) || payload, error } } } return state } export function playerReducer(state, { type, track, playlist, duration, currentTime, volume, playlistIndex }) { if (!state || type === 'EJECT_TRACK') return {} if (type === 'PLAY_TRACK') return { ...state, 'isPlaying': true } if (type === 'PAUSE_TRACK') return { ...state, 'isPlaying': false, 'currentTime': currentTime } if (type === 'SET_CURRTIME') { console.log('set_currtime') return { ...state, 'currentTime': currentTime } } if (type === 'SET_DURATION') { console.log('SET_DURATION') return { ...state, 'duration': duration } } if (type === 'SET_VOLUME') return { ...state, 'volume': volume } if (type === 'SET_PLAYLIST') { return { ...state, 'playlist': playlist} } if (type === 'SET_INDEX') { return { ...state, 'playlistIndex': playlistIndex} } if (type === 'SET_TRACK') { return { isPlaying: false, track: track, playlist: playlist, duration: duration, //общая длительность трека currentTime: currentTime,// текущая позиция в треке volume: volume, //playlistIndex: playlist.indexOf(track) playlistIndex: playlist.constructor.name === 'Array'? playlist.indexOf(track) : playlist.tracks.indexOf(track) } } return state } export const localStoreReducer = (reducer, localStorageName) => { return ( (state, action) => { let newState if (!state && localStorage.getItem(localStorageName)) { newState = JSON.parse(localStorage.getItem(localStorageName)) console.log(newState) } else { newState = reducer(state, action) localStorage.setItem(localStorageName, JSON.stringify(newState)) } return newState } ) }