123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- 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 }
- if (type === 'SET_CURRTIME') return { ...state, 'currentTime': currentTime }
- if (type === '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') {
- console.log('SEt_INDEX', playlistIndex)
- return { ...state, 'playlistIndex': playlistIndex}
- }
- if (type === 'SET_TRACK') {
- return {
- isPlaying: false,
- track: track,
- playlist: playlist,
- duration: duration,
- currentTime: currentTime,
- volume: volume,
- 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
- }
- )
- }
|