12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- import { setTrack } from '../actions'
- 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') {
- //console.log('TOXEN', jwtDecode(token))
- 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 }) {
- 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_TRACK') {
- console.log('setTrack')
- return {
- isPlaying: false,
- track: track,
- playlist: playlist,
- duration: duration, //общая длительность трека
- currentTime: currentTime,// текущая позиция в треке
- volume: volume,
- playlistIndex: playlist.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
- }
- )
- }
|