2020-12-16 00:31:30 +00:00
|
|
|
import {
|
2020-12-17 06:34:00 +00:00
|
|
|
ALBUMS_FETCH_REQUEST,
|
|
|
|
ALBUMS_FETCH_SUCCESS,
|
|
|
|
ALBUMS_FETCH_FAIL,
|
|
|
|
ALBUMS_CREATE_SUCCESS,
|
|
|
|
ALBUMS_REMOVE_REQUEST,
|
2020-12-18 04:46:37 +00:00
|
|
|
} from '../actions/albums'
|
2020-12-16 00:31:30 +00:00
|
|
|
import { Map as ImmutableMap, List as ImmutableList, fromJS } from 'immutable'
|
|
|
|
|
|
|
|
const initialState = ImmutableMap({
|
|
|
|
items: ImmutableList(),
|
|
|
|
isLoading: false,
|
|
|
|
isFetched: false,
|
|
|
|
isError: false,
|
|
|
|
})
|
|
|
|
|
|
|
|
export default function albums(state = initialState, action) {
|
|
|
|
switch(action.type) {
|
2020-12-17 06:34:00 +00:00
|
|
|
case ALBUMS_FETCH_REQUEST:
|
2020-12-16 00:31:30 +00:00
|
|
|
return state.withMutations((map) => {
|
|
|
|
map.set('isLoading', true)
|
|
|
|
map.set('isFetched', false)
|
|
|
|
map.set('isError', false)
|
|
|
|
})
|
2020-12-17 06:34:00 +00:00
|
|
|
case ALBUMS_FETCH_SUCCESS:
|
2020-12-16 00:31:30 +00:00
|
|
|
return state.withMutations((map) => {
|
2020-12-18 04:46:37 +00:00
|
|
|
map.set('items', fromJS(action.albums))
|
2020-12-16 00:31:30 +00:00
|
|
|
map.set('isLoading', false)
|
|
|
|
map.set('isFetched', true)
|
|
|
|
map.set('isError', false)
|
|
|
|
})
|
2020-12-17 06:34:00 +00:00
|
|
|
case ALBUMS_FETCH_FAIL:
|
2020-12-16 00:31:30 +00:00
|
|
|
return state.withMutations((map) => {
|
|
|
|
map.set('isLoading', false)
|
|
|
|
map.set('isFetched', true)
|
|
|
|
map.set('isError', true)
|
|
|
|
})
|
2020-12-17 06:34:00 +00:00
|
|
|
case ALBUMS_CREATE_SUCCESS:
|
2020-12-18 04:46:37 +00:00
|
|
|
return state.update('items', list => list.push(fromJS(action.albums)))
|
2020-12-17 06:34:00 +00:00
|
|
|
case ALBUMS_REMOVE_REQUEST:
|
2020-12-16 00:31:30 +00:00
|
|
|
return state.update('items', list => list.filterNot((item) => {
|
2020-12-18 04:46:37 +00:00
|
|
|
return item.get('id') === action.albumId
|
2020-12-16 00:31:30 +00:00
|
|
|
}))
|
|
|
|
default:
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
}
|