Skip to content

Instantly share code, notes, and snippets.

@tristola
Created March 27, 2017 04:18
Show Gist options
  • Save tristola/9166637c572fdc2d55491656afea37c5 to your computer and use it in GitHub Desktop.
Save tristola/9166637c572fdc2d55491656afea37c5 to your computer and use it in GitHub Desktop.
songilst redux
import { createReducer, createActions } from 'reduxsauce'
import Immutable from 'seamless-immutable'
/* ------------- Types and Action Creators ------------- */
const { Types, Creators } = createActions({
scanFiles: null,
songlistAdd: ['songs']
})
export const SonglistTypes = Types
export default Creators
export const INITIAL_STATE = Immutable({
songs: [],
genres: []
})
let uniqueItems = songs => songs.filter((elem, pos, arr) => arr.indexOf(elem) === pos)
export const scan = (state, { songs }) => state.merge({ scanning: true })
export const songlistAdd = (state, { songs }) => {
const genres = uniqueItems(songs.map(song => song.metadata.genre))
return state.merge({ songs, genres, scanning: false })
}
export const reducer = createReducer(INITIAL_STATE, {
[Types.SONGLIST_ADD]: songlistAdd,
[Types.SCAN_FILES]: scan
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment