Created
March 27, 2017 04:18
-
-
Save tristola/9166637c572fdc2d55491656afea37c5 to your computer and use it in GitHub Desktop.
songilst redux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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